#!/bin/sh
#
# $Id$
#
# This script sets up the network + Network FS environment if needed.
#


# number of ping
ipcount=3

# Get info from config file
ipdev=""
hwaddr=""
ipaddress=""
ipnetmask=""
ipbroadcast=""
ipgateway=""
proto=""
ipconf=""
pre=""
post=""
netfsmount=""
imgname=""
dirimg=""

# Get info from config file
if [ -f $MINDI_CACHE/mondorestore.cfg ]; then
	ipdev=`grep netfs-dev $MINDI_CACHE/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
	hwaddr=`grep netfs-client-hwaddr $MINDI_CACHE/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
	ipaddress=`grep netfs-client-ipaddr $MINDI_CACHE/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
	ipnetmask=`grep netfs-client-netmask $MINDI_CACHE/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
	ipbroadcast=`grep netfs-client-broadcast $MINDI_CACHE/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
	ipgateway=`grep netfs-client-defgw $MINDI_CACHE/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
	proto=`grep netfs-proto $MINDI_CACHE/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
	export netfsmount=`grep netfs-server-mount $MINDI_CACHE/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
	export imgname=`grep iso-prefix $MINDI_CACHE/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
	export dirimg=`grep netfs-server-path $MINDI_CACHE/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
fi

if [ "$imgname" = "" ]; then
	export imgname="mondorescue"
fi
if [ "$dirimg" = "" ]; then
	export dirimg="/"
fi

# info from cmdline are predominent
for i in `cat $CMDLINE` ; do
	echo $i | grep -qi ping= && ipcount=`echo $i | cut -d= -f2`
	echo $i | grep -qi hwaddr= && hwaddr=`echo $i | cut -d= -f2`
	echo $i | grep -qi ipconf= && ipconf=`echo $i | cut -d= -f2`
	echo $i | grep -qi netfsmount= && export netfsmount=`echo $i | cut -d= -f2`
	echo $i | grep -qi netfsopt= && export netfsopt=`echo $i | cut -d= -f2`
	echo $i | grep -qi prefix= && export imgname=`echo $i | cut -d= -f2`
	echo $i | grep -qi netfspath= && export dirimg=`echo $i | cut -d= -f2`
	echo $i | grep -qi pre= && pre=`echo $i | cut -d= -f2`
	echo $i | grep -qi post= && post=`echo $i | cut -d= -f2`
	echo $i | grep -qi proto= && proto=`echo $i | cut -d= -f2`
done

if [ "$ipdev" != "" ] || [ "$ipconf" != "" ]; then
	# Network configuration stored during archive
	# or on cmdline so network wanted => starting it
	LogIt "Starting Network..."

	# Activate loobback
	ifconfig lo 127.0.0.1

	echo "$ipconf" | grep -q "dhcp"
	if [ $? -eq 0 ]; then
		ipdev=`echo $ipconf | cut -d: -f1`
		dhc="/sbin/dhclient"
		if [ ! -x $dhc ]; then
			dhc="/sbin/dhcpcd"
			if [ ! -x $dhc ]; then
				dhc="/sbin/udhcpc"
				if [ ! -x $dhc ]; then
					dhc=""
				else
					dhc="$dhc -i"
				fi
			fi
		fi
		if [ -x $dhc ]; then
			LogIt "Making DHCP request on $ipdev with $dhc"
			$dhc $ipdev
		else
			LogIt "Unable to make a DHCP request on $ipdev - no client found"
		fi
	else
		if [ "$ipconf" != "" ]; then
			ipdev=`echo $ipconf | cut -d: -f1`
			ipaddress=`echo $ipconf | cut -d: -f2`
			ipnetmask=`echo $ipconf | cut -d: -f3`
			ipbroadcast=`echo $ipconf | cut -d: -f4`
			ipgateway=`echo $ipconf | cut -d: -f5`
		fi

		# If same system, map to the right MAC address
		hwaddr_found=`ifconfig $ipdev | /bin/grep HWaddr | awk '{print $NF}'`
		if [ "$hwaddr" != "$hwaddr_found" ]; then
			ipdev_new=`ifconfig -a | /bin/grep $hwaddr | awk '{print $1}'`
			if [ "$ipdev_new" != "" ]; then
				LogIt "Interface $ipdev changed to $ipdev_new (MAC: $hwaddr)"
				ipdev=$ipdev_new
			else
				LogIt "NOTE: Interface $ipdev kept despite it doesn't match the $hwaddr MAC address"
			fi
		fi

		LogIt "Configuring $ipdev statically ($ipaddress/$ipnetmask)"
		ifconfig $ipdev $ipaddress netmask $ipnetmask broadcast $ipbroadcast
		route add default gw $ipgateway

	fi

	# Leave time to the satck to wake up (reported by some users)
	sleep 5

	# ping server helps waking interface up
	netfs_server_ipaddr=`echo $netfsmount | cut -d: -f1 | cut -d@ -f2`
	if [ $netfs_server_ipaddr != "" ]; then
		LogIt "Pinging Remote server $netfs_server_ipaddr ($ipcount times)..."
		ping -c $ipcount $netfs_server_ipaddr 
	fi

	# Which is our protocol for file sharing
	if [ "$proto" = "" ]; then
		LogIt "No protocol specified, defaulting to NFS"
		proto="nfs"
	fi

	if [ "$netfsmount" != "" ]; then
		if [ "$proto" = "sshfs" ]; then
			LogIt "Mounting SSHFS share ($netfsmount) on /tmp/isodir..."
			# We need a correct console for ssh
			ln -sf /dev/console /dev/tty
			sshfs -o ro,StrictHostKeyChecking=no $netfsopt $netfsmount /tmp/isodir
		elif [ "$proto" != "" ]; then
			LogIt "Mounting Network share ($netfsmount) on /tmp/isodir..."
			touch /etc/mtab
			mount -t $proto -o nolock,ro $netfsopt $netfsmount /tmp/isodir
		else
			LogIt "Unknown protocol $proto"
		fi

		# In all these cases we should have an image
		LogIt "Mounting Remote image ${imgname}-1.iso in $dirimg on /mnt/cdrom in loopback"
		mount -o ro,loop -t iso9660 /tmp/isodir/$dirimg/${imgname}-1.iso /mnt/cdrom
	else
		LogIt "No image to mount"
	fi
	
	# Save the scripts on the Network share locally
	if [ "`echo $pre | grep -E '^/tmp/isodir'`" ]; then
		cp $pre /tmp
		LogIt "Copying $pre under /tmp"
	fi
	if [ "`echo $post | grep -E '^/tmp/isodir'`" ]; then
		cp $post /tmp
		LogIt "Copying $post under /tmp"
	fi
fi
