#!/bin/sh
export PATH=.:$PATH:/usr/bin:/usr/local/bin
gawk --non-decimal-data -v q="'" -v ipaddr=$1 -v userport=$2 \
' \
function setbits_l(mask,ports) \
{ \
  delete portz; \
  split(ports,portz,","); \
  decmask=("0x" mask)+0; \
  for(i in portz) { \
  if((portz[i]!="")&&(portz[i]<=32)) { \
    decmask=or(lshift(1,32-portz[i]),decmask);} \
    } \
  return decmask; \

} \
function setbits_r(mask,ports) \
{ \
  delete portz; \
  split(ports,portz,","); \
  decmask=256*("0x" mask)+0; \
  for(i in portz) { \
  if((portz[i]!="")&&(portz[i]>32)) { \
    decmask=or(lshift(1,32-(portz[i]-32)),decmask);} \
    } \
  return decmask; \

} \
function clearbits_l(mask,ports) \
{ \
  delete portz; \
  split(ports,portz,","); \
  decmask=("0x" mask)+0; \
  for(i in portz) { \
  if((portz[i]!="")&&(portz[i]<=32)) { \
    decmask=and(compl(lshift(1,32-portz[i])),decmask);} \
    } \
  return decmask; \

} \
function clearbits_r(mask,ports) \
{ \
  delete portz; \
  split(ports,portz,","); \
  decmask=256*("0x" mask)+0; \
  for(i in portz) { \
  if((portz[i]!="")&&(portz[i]>32)) { \
    decmask=and(compl(lshift(1,32-(portz[i]-32))),decmask);} \
    } \
  return decmask; \

} \
BEGIN \
{
rem="--! gsub(/,/,\" \",userport); ";

while(getline acl_portmonet>0) { \
	delete acl_string; \
	split(acl_portmonet,acl_string,"\t"); \
	user_ports[acl_string[6]]=user_ports[acl_string[6]] "," acl_string[4];}

while(("snmpwalk -v2c -c mrtg -m " q q " " ipaddr " .1.3.6.1.2.1.17.7.1.4.3.1.1")|getline) { \
	delete vlan_temp; \
	split($1,vlan_temp,"."); \
	vlans_switch[vlan_temp[14]]=vlan_temp[14]} \
} \
END \
{ \
while(getline>0) print ".."; \
for(vlan_index in vlans_switch) { \
	print "VLAN        : " vlan_index; \
	("snmpget -v2c -c mrtg -m " q q " " ipaddr " .1.3.6.1.2.1.17.7.1.4.3.1.4." vlan_index)|getline vut; \
	delete cl;split(vut,cl," "); \
	vlan_untagged[vlan_index]= cl[4] cl[5] cl[6] cl[7] cl[8] cl[9] cl[10]; \
	print "untagged now: " vlan_untagged[vlan_index]; \
	vlan_untagged_set[vlan_index]=sprintf("%.6X%.8X",\
		clearbits_l(substr(vlan_untagged[vlan_index],1,8),userport),\
		clearbits_r(substr(vlan_untagged[vlan_index],9,6),userport)/256); \
	print "to set untag: " vlan_untagged_set[vlan_index]; \
	system("snmpset -v2c -c mrtg -m " q q " " ipaddr " .1.3.6.1.2.1.17.7.1.4.3.1.4." vlan_index " x " \
		vlan_untagged_set[vlan_index] " >/dev/null 2>&1"); \
	("snmpget -v2c -c mrtg -m " q q " " ipaddr " .1.3.6.1.2.1.17.7.1.4.3.1.2." vlan_index)|getline vt; \
	delete cl;split(vt,cl," "); \
	vlan_tagged[vlan_index]= cl[4] cl[5] cl[6] cl[7] cl[8] cl[9] cl[10]; \
	print "tagged . now: " vlan_tagged[vlan_index]; \
	vlan_tagged_set[vlan_index]=sprintf("%.6X%.8X",\
		clearbits_l(substr(vlan_tagged[vlan_index],1,8),userport),\
		clearbits_r(substr(vlan_tagged[vlan_index],9,6),userport)/256); \
	print "to set  tagg: " vlan_tagged_set[vlan_index]; \
	system("snmpset -v2c -c mrtg -m " q q " " ipaddr " .1.3.6.1.2.1.17.7.1.4.3.1.2." vlan_index " x " \
		vlan_tagged_set[vlan_index] " >/dev/null 2>&1"); \
	print "";} \

for(vlan_index in vlans_switch) { \
	print "VLAN : " vlan_index " - " user_ports[vlan_index] " ."; \
	a="print \"tag  : \" vlan_index \" - \" sprintf(\"%.8X\",setbits(vlan_tagged_set[vlan_index],user_ports[vlan_index]));"; \
	system("snmpset -v2c -c mrtg -m " q q " " ipaddr " .1.3.6.1.2.1.17.7.1.4.3.1.2." vlan_index " x " \
		sprintf("%.8X%.6X",\
		setbits_l(substr(vlan_tagged_set[vlan_index],1,8),user_ports[vlan_index]),\
		setbits_r(substr(vlan_tagged_set[vlan_index],9,6),user_ports[vlan_index])/256) " >/dev/null 2>&1"); \
	a="print \"unt  : \" vlan_index \" - \" sprintf(\"%.8X\",setbits(vlan_untagged_set[vlan_index],user_ports[vlan_index]));"; \
	system("snmpset -v2c -c mrtg -m " q q " " ipaddr " .1.3.6.1.2.1.17.7.1.4.3.1.4." vlan_index " x " \
		sprintf("%.8X%.6X",\
		setbits_l(substr(vlan_untagged_set[vlan_index],1,8),user_ports[vlan_index]),\
		setbits_r(substr(vlan_untagged_set[vlan_index],9,6),user_ports[vlan_index])/256) " >/dev/null 2>&1"); \
	} \
}' /tftpboot/$3
