#!/bin/bash

# sqpnetapply v1.04 (21st June 2012) by Andy Davison
#  Executed by JogglerNetwork - only used on SqueezePlay OS

DEBUG=0

readnetsettings() {
	
	SETTINGS=`cat ~/.squeezeplay/userpath/settings/JogglerNetwork.lua 2>/dev/null`
	LOOPVAR=${SETTINGS}

	if [ "$SETTINGS" != "" ]; then
		while echo $LOOPVAR | grep \, >/dev/null
		do

			LOOPTEMP=${LOOPVAR%%\,*}
			LOOPVAR=${LOOPVAR#*\,}

			VAR=`echo $LOOPTEMP | awk -F\= '{print $1}'`
			VAL=`echo $LOOPTEMP | awk -F\" '{print $2}'`

			[[ "$VAR" =~ "settings" ]] && NIC="$VAL"
			[[ "$VAR" == "ssid" ]] && SSID="$VAL"
			[[ "$VAR" == "psk" ]] && PSK="$VAL"
	
			[ $DEBUG -eq 1 ] && echo "This is the VAR = $VAR"
			[ $DEBUG -eq 1 ] && echo "This is the VAL = $VAL"

		done
	else
		NIC="lo"
	fi

	[ $DEBUG -eq 1 ] && echo
	[ $DEBUG -eq 1 ] && echo "The NIC is $NIC"
	[ $DEBUG -eq 1 ] && echo "The SSID is $SSID"
	[ $DEBUG -eq 1 ] && echo "The PSK is $PSK"
	
}

applynetsettings() {
	
	# Build the interfaces file
	echo "auto lo" > /tmp/interfaces.tmp
	echo "iface lo inet loopback" >> /tmp/interfaces.tmp
	echo >> /tmp/interfaces.tmp
	echo "auto $NIC" >> /tmp/interfaces.tmp
	echo "iface $NIC inet dhcp" >> /tmp/interfaces.tmp
	
	if [ "$NIC" == "wlan0" ]; then
		[ "$SSID" != "" ] && echo "wpa-ssid \"$SSID\"" >> /tmp/interfaces.tmp
		[ "$SSID" != "" ] && echo "wpa-psk \"$PSK\"" >> /tmp/interfaces.tmp
	fi
	
	if [ "$NIC" == "eth0" ]; then
		echo >> /tmp/interfaces.tmp
		echo "auto wlan0" >> /tmp/interfaces.tmp
		echo "iface wlan0 inet dhcp" >> /tmp/interfaces.tmp
	fi
	
	# Overwrite the previous interfaces file
	sudo mv /tmp/interfaces.tmp /etc/network/interfaces
	sudo chown root:root /etc/network/interfaces
	
	if [[ "$NIC" == "wlan0" ]] && [[ "$PSK" != "" ]]; then
		sudo chmod 600 /etc/network/interfaces
	else
		sudo chmod 644 /etc/network/interfaces
	fi
	
}

if [ "$1" == "apply" ]; then

	readnetsettings
	sleep 1
	applynetsettings
	sleep 1
	sudo reboot
	exit 0

elif [ "$1" == "reset" ]; then
	
	rm ~/.squeezeplay/userpath/settings/JogglerNetwork.lua &>/dev/null
	sudo rm /etc/network/joggler-eth
	sudo rm /etc/udev/rules.d/70-persistent-net.rules
	sudo touch /etc/udev/rules.d/70-persistent-net.rules
	NIC="eth0"
	applynetsettings
	sudo chmod 644 /etc/network/interfaces
	exit 0
	
elif [ "$1" == "ip" ]; then
	
	readnetsettings
	if [[ "$NIC" =~ "wlan" ]]; then
		INTERFACE="Wireless"
	elif [[ "$NIC" =~ "eth" ]]; then
		INTERFACE="Ethernet"
	else
		INTERFACE=""
	fi
	IP=`ifconfig $NIC | grep 'inet addr' | awk -F\addr: {'print $2'} | awk -F\  {'print $1'}`
	
	# Be really careful with this output. For any network fail the return MUST begin 'No network' for SetupWelcome applet.
	if [ "$INTERFACE" != "" ] && [ "$IP" != "" ]; then
		echo -n "$INTERFACE interface is active. Current IP: $IP"
	elif [ "$INTERFACE" != "" ]; then
		echo -n "No network address has been assigned to the $INTERFACE interface ($NIC)."
	else
		echo -n "No network has been configured or it is inaccessible."
	fi
	exit 0
	
else

	echo "I've got nothing to do."
	exit 0
	
fi
