#!/bin/sh
#
# Start the network....
#

INTERFACES="/etc/network/interfaces"

status_grep_autolo=99
status_grep_ifacelo=99
status_all=0
catch_all=""

case "$1" in
  start)
 	echo "Starting network..."
 	fbv -i -d 1 /home/qlx/boot/boot30.png
	# Clear ifstate configuration
	rm -f /var/run/ifstate

	# Check if basic loopback (lo) interface exists
	if [ -e ${INTERFACES} ]
	then
		catch_all=$(/bin/grep "auto lo" ${INTERFACES})
		status_grep_autolo=$?
		catch_all=$(/bin/grep "iface lo inet loopback" ${INTERFACES})
		status_grep_ifacelo=$?
	fi

	let status_all=$status_grep_autolo+$status_grep_ifacelo
	#       echo ""
	#       echo "total = ${status_all}"
	#       echo "STATUSES = ${status_grep_autolo} - ${status_grep_ifacelo}"
	#       echo ""
	
	# If not, recreate a basic interfaces file
	if [ $status_all -gt 0 ]
	then
		echo "# Configure Loopback" > ${INTERFACES}
		echo "auto lo" >> ${INTERFACES}
		echo "iface lo inet loopback" >> ${INTERFACES}
		echo "" >> ${INTERFACES}
		# denotes localhost created from a crash
		touch /tmp/lo_created
		chown qlx.qlx /tmp/lo_created
		#chmod 777 /tmp/lo_created
		sync
	fi

	/sbin/iwconfig wlan0 power off
	
	# Create WIFI direct interface and bring it up.
	/usr/sbin/iw phy phy0 interface add p2p0 type managed
	/sbin/ifup p2p0

	/sbin/ifup -a
	;;
  stop)
	echo -n "Stopping network..."
	/sbin/ifdown -a
	;;
  restart|reload)
	"$0" stop
	"$0" start
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?

