#!/bin/sh

# This script is used in MFG to preload HMC code.
#
# First argument is the interface name
# Second argument is the IP address to use
# Third argument is the gateway IP address
# Fourth argument is the netmask IP address
# Fifth argument is the server IP address
# Sixth argument is the directory to mount
# Seventh argument is the nfs version '3' or '4'

if [ $# -eq 0 ]; then
   echo "Usage: checkParms <network interface> <HMC IP address>"
   echo "       <gateway IP address> <network mask IP address>"
   echo "       <server IP address> <directory on server to mount>"
   echo "       <3 or 4, nfs version),optional,default 3>"
   exit 1
fi
#mkdir -p /var/run
#dhclient $1 >>/dev/null 2>&1
#if [ $? -ne 0 ]; then
#   echo "dhclient returns an error...."
#   exit 1
#fi

INTERFACE=$1
TRANSTYPE=nfs
PROTOCOL=static
IPADDR=$2
GATEWAY=$3
NETMASK=$4
SERVERHOST=$5
XDIR=$6
NFSVER="vers=3"

ip link set $INTERFACE up
ip link show $INTERFACE | grep UP
if [ $? -eq 0 ]; then
   ip addr add ${IPADDR}/${NETMASK} br + dev $INTERFACE
fi

#ifconfig $INTERFACE down
#if [ $? -ne 0 ]; then
#   echo "*** Cannot bring down interface "$INTERFACE
#   exit 1
#fi
#ifconfig $INTERFACE up $IPADDR netmask $NETMASK
#if [ $? -ne 0 ]; then
#   echo "*** Cannot bring up interface "$INTERFACE
#   exit 1
#fi

if [ "$GATEWAY" != "" ]; then
   route del default >>/dev/null 2>&1
#   route add default gw $GATEWAY dev $INTERFACE
   ip route add default via $GATEWAY dev $INTERFACE
   if [ $? -ne 0 ]; then
      echo "*** Unable to set gateway"
      exit 1
   fi
fi
ping -c 1 $SERVERHOST >/dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "*** Unable to contact "$SERVERHOST
  exit 1
fi

if [ ! -z "$7" ]; then
    if [ "$7" != "3" -a "$7" != "4" ]; then
        echo "nfs version $7 is invalid, specify 3 or 4, if not specified default is 3"
        exit 1
    fi
    NFSVER="vers=$7"
fi

mount | grep -q -w "/images"
if [ $? -ne 0 ]; then
   mkdir -p /images
   echo "mount -t nfs -o nolock,ro,${NFSVER} ${SERVERHOST}:${XDIR} /images"
   mount -t nfs -o nolock,ro,${NFSVER} ${SERVERHOST}:${XDIR} /images
   if [ $? -ne 0 ]; then
      echo "*** Unable to mount "${XDIR}" from host "${SERVERHOST}
      exit 1
   fi
   echo "mount successful, listing current nfs mounts..."
   mount -t nfs
   echo ""
   
   if [ ! -f /images/disk1.img ]; then
      echo "*** Required file disk1.img not found on server."
      exit 1
   fi
fi
# Now cleanup
umount /images
route del default >>/dev/null 2>&1
#ifconfig $INTERFACE down
ip link set $INTERFACE down
#kill `cat /var/run/dhclient.pid`
exit 0
