#!/bin/bash

if [ $1 ]; then
        gawk ' BEGIN{
			if ( ARGC > 1 ) {
				if ( index(ARGV[2],"0x") ) { 
					u = strtonum(ARGV[2]); 
				} else { 
					u = ARGV[2];
				}
			}
			ARGC =2;	
		} 
        /^[0-9]/ {
			j = 0;
			#split the net from the subnet
			split($1,A,"/");
			split(A[1],B,".");      
			#calc the net
			I = lshift(lshift(lshift(B[1],8)+B[2],8)+B[3],8)+B[4]; 
			S = 0;
			#calc the subnet
			while (j < A[2]){  
				S = lshift(S,1)+1;
				j++;
			}
			S = lshift(S,(32-A[2]));
			#print the net, subnet & member
			if($2 != 0){
  				printf "0x%08X\t0x00000000\t0x00000000\t0x00000000\t0x%08X\t0x00000000\t0x00000000\t0x00000000\t%s\n",I,S,$2;
			}
			else {
  				printf "0x%08X\t0x00000000\t0x00000000\t0x00000000\t0x%08X\t0x00000000\t0x00000000\t0x00000000\t%s\n",I,S,u++;
        	}	
		}' $1  > $1"_test" 
else
       echo subnet_create whoisfile label
fi

