#!/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=`grep netfs-dev /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
hwaddr=`grep netfs-client-hwaddr /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
ipaddress=`grep netfs-client-ipaddr /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
ipnetmask=`grep netfs-client-netmask /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
ipbroadcast=`grep netfs-client-broadcast /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
ipgateway=`grep netfs-client-defgw /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
proto=`grep netfs-proto /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
iptoute=`grep netfs-route /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
ipconf=""
pre=""
post=""
export netfsmount=`grep netfs-server-mount /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
export imgname=`grep iso-prefix /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
if [ "$imgname" = "" ]; then
	export imgname="mondorescue"
fi
export dirimg=`grep netfs-server-path /tmp/mondo-restore.cfg 2> /dev/null | cut -d' ' -f2-`
if [ "$dirimg" = "" ]; then
	export dirimg="/"
fi

# info from cmdline are predominent
for i in `cat /proc/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`
	echo $i | grep -qi iproute= && iproute=`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`
		LogIt "Making DHCP request on $ipdev"
		udhcpc -i $ipdev
	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 stack to wake up (reported by some users)
	sleep 5

	# Handle a potential static route
	if [ _"$iproute" != _"" ]; then
		iproute=`echo $iproute | perl -pi -e 's|:| |g'`
		LogIt "Adding static route with route add $iproute"
		route add $iproute
	fi

	# ping server helps waking interface up
	LogIt "Pinging Remote server..."
	netfs_server_ipaddr=`echo $netfsmount | cut -d: -f1 | cut -d@ -f2`
	ping -c $ipcount $netfs_server_ipaddr 

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

	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" = "smbfs" ]; then
		LogIt "Mounting SMBFS share ($netfsmount) on /tmp/isodir..."
		mount -t cifs $netfsopt $netfsmount /tmp/isodir -o ro 
	elif [ "$proto" != "" ]; then
		if [ -x /sbin/rpcbind ]; then
			"Startng rpcbind daemon..."
			/sbin/rpcbind &
		fi
		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
	
	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

	# 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
