#!/bin/ksh
#       Install Almaden Research Center Local Customizations
#
#  This script will configure TCP/IP with a mktcpip command.

# Define a function to handle errors
function Error
{
  print "Error in configuration process; configuration aborted" | alog -t $alogtype
  exit 1
}

# Print a message to screen and to logfile
function Message
{
  print - "[$(date +%H:%M:%S)]" $@ | alog -t $alogtype
}

# Print a message to log file only
function Log
{
  print - "[$(date +%H:%M:%S)]" $@ | alog -t $alogtype -q
}

# Enable error trap
trap Error ERR

# Progress will be logged to the bosinst alog type, which translates to
# the file /var/adm/ras/bosinstlog.  One can alog -t bosinst -o to see it.
alogtype=bosinst

# Make sure we are running as root
if    [[ $(whoami) != root ]]
then  Message "You must be logged in as root to run this command"
      exit 1
fi

# The real work starts here
Message "setupnet script started on $(date)"

# Get some machine-specific info in order to build the mktcpip command.
# There's some question whether the hostname command will work correctly
# if this is a mksysb install.  Perhaps a better way to get the intended
# hostname for this machine in that case (not the hostname of the machine
# that did the mksysb) is to use the $NIM_NAME environment variable,
# which NIM conveniently sets up for us if this is a bos_inst.
# The problem is, I don't think $NIM_NAME is set up if this script is
# run via an allocate & cust.
machname="$(hostname)"
ip_addr="$(host $machname)"
# Response could be simple (e.g. "nemesis.almaden.ibm.com is 129.33.40.14")
# or complex (e.g. "nameserver2.almaden.ibm.com is 129.33.10.4, \
#                   Aliases: nameserver2.almaden.ibm.com")
# either way, all we want is the dotted decimal portion.
ip_addr=${ip_addr##* is }   # Strip off "such.and.such.a.name is " leader
ip_addr=${ip_addr%%,*}      # Strip off everything past the first comma (if any)

# Parse ip_addr to get subnet and use that to get default route
subnet="${ip_addr%.*}"      # Strip off last .xx 
subnet="${subnet##*.}"      # Strip off leading xxx.xxx.

case "$subnet" in
10|12|13|14|15|22|32|40) gateway=129.33.$subnet.253       # Ethernet - Old network
                         nameserver=129.33.24.254
                         mask=255.255.255.0  ;;
24|25|26|27|28|29|30|31) gateway=129.33.24.253            # Ethernet - New network
                         nameserver=129.33.24.254
                         mask=255.255.248.0  ;;
60|62|63|64|65|68|82|160) gateway=129.33.$subnet.253      # Token Ring - Old network
                          nameserver=129.33.72.254
                          mask=255.255.255.0  ;;
72|73|74|75|76|77|78|79) gateway=129.33.72.253            # Token Ring - New network
                         nameserver=129.33.72.254
                         mask=255.255.248.0  ;;
*) Message "Unknown subnet ($subnet).  Please contact the AIX Help Desk at 7-2525 Option 1 to fix the Do-net script on nemesis."
   Error  ;;
esac
    
msg="Configuring TCP/IP with machine name=$machname, IP address=$ip_addr, nameserver=$nameserver, domain=almaden.ibm.com, gateway=$gateway, mask=$mask,"
mktcpip_parms="-h $machname -a $ip_addr -n $nameserver -d almaden.ibm.com -g $gateway -m $mask -s"

# An alternative to hardcoding "dix" in here would be to do a nimclient -l -l $machname
# command and parse out the cable_type1 (if ethernet) or ring_speed1 (if token ring) parm.
# I was lazy.  At Almaden, all we have is dix-type ethernet (no bnc) or 16 megabit (no 4)
# token ring.  The interesting question is how will ATM show up and be defined?  I dunno.
if    [[ $subnet < 55 ]]
then  msg="$msg and dix-type ethernet."
      mktcpip_parms="$mktcpip_parms -i en0 -t dix"
else  msg="$msg and 16-Mbps token ring."
      mktcpip_parms="$mktcpip_parms -i tr0 -r 16"
fi

Message $msg
mktcpip $mktcpip_parms
Message "TCP/IP configured."
