

post_install() 
{
# Need to figure out the networking parameters from gnxid and iproute 
 # and apply them to the templates
echo "This is the network post-install"

cat <<EOF > /tmp/gnxid-net.sh


generate_wan()
{
	echo "We are now generating WAN templates"
	echo "GNX_WAN=\$GNX_WAN"
	
	if [ -v GNX_WAN ]; then
		echo "WAN on \$GNX_WAN"
		sed -i s%Interface=.*%Interface=\$GNX_WAN% /etc/netctl/wan
		sed -i s%BindsToInterfaces=.*%BindsToInterfaces=\$GNX_WAN% /etc/netctl/wan
	fi
	
	if [ -v GNX_VLAN ]; then
		echo "WAN on VLAN \$GNX_VLAN"
		sed -i s%BindsToInterfaces=.*%BindsToInterfaces=\$GNX_VLAN% /etc/netctl/wan
		sed -i s%#VLANID=.*%VLANID=3% /etc/netctl/wan
		sed -i s%Connection=.*%Connection=vlan% /etc/netctl/wan
	fi
	if [ -v GNX_WAN ]; then
	   netctl enable wan
	else
	   rm /etc/netctl/wan
	fi
	
}

generate_usb()
{
	echo "We are now generating USB templates"
	echo "GNX_USB=\$GNX_USB"
	
	if [ -v GNX_USB ]; then
		echo "USB on \$GNX_USB"
		sed -i s%Interface=.*%Interface=\$GNX_USB% /etc/netctl/usb
		sed -i s%BindsToInterfaces=.*%BindsToInterfaces=\$GNX_USB% /etc/netctl/usb
		netctl enable usb
		systemctl enable gnx-usb-ifplugd@usb0.service
		systemctl enable gnx-usb-gadget-init@usb0.service
		systemctl enable getty@ttyGS0.service
	else
	    rm 	/etc/netctl/usb
	fi
	
}

generate_lan()
{
	echo "We are now generating LAN templates"
	echo "GNX_LAN=\$GNX_LAN"
	
	if [ -v GNX_LAN ]; then
		echo "LAN on \$GNX_LAN"
		sed -i s%Interface=.*%Interface=\$GNX_LAN% /etc/netctl/lan
		sed -i s%BindsToInterfaces=.*%BindsToInterfaces=\$GNX_LAN% /etc/netctl/lan
	fi
	
	if [ -v GNX_VLAN ]; then
		echo "LAN on VLAN \$GNX_VLAN"
		sed -i s%BindsToInterfaces=.*%BindsToInterfaces=\$GNX_VLAN% /etc/netctl/lan
		sed -i s%#VLANID=.*%VLANID=4% /etc/netctl/lan
		sed -i s%Connection=.*%Connection=vlan% /etc/netctl/lan
	fi
	
	if [ -v GNX_LAN ]; then
		netctl enable lan
	else
	    rm /etc/netctl/lan
	fi
	
}


generate_gsm()
{
	echo "We are now generating GSM templates"
	echo "GNX_GSM=\$GNX_GSM"
	
	if [ -v GNX_GSM ]; then
	    cfg=(\${GNX_GSM//:/ })
		echo "GSM on \${cfg[0]}"
		cfg_if=\$(basename \${cfg[0]}) 
		sed -i s%Interface=.*%Interface=\${cfg_if}% /etc/netctl/gsm
		echo "#!/usr/bin/sh" > /etc/netctl/options/gsm.hwinit
		echo "hwctl init \${cfg[1]}" >> /etc/netctl/options/gsm.hwinit
		if [ -v GNX_WWAN ]; then
         	echo "GSM on \$GNX_WWAN"
  		    sed -i s%Interface=.*%Interface=\$GNX_WWAN% /etc/netctl/wwan
  		    sed -i s%ExecUpPre=.*%ExecUpPre="'hwctl init \${cfg[1]}'"% /etc/netctl/wwan
		else
		    rm 	/etc/netctl/wwan
		fi
	else
	    rm 	/etc/netctl/gsm
	    rm 	/etc/netctl/wwan
	fi
	
	
}



generate_wan
generate_lan
generate_usb
generate_gsm

EOF

gnxid -s /tmp/gnxid-net.sh

}



