#!/bin/sh

# Command to print an error message and exit

warn ()
{
    stars=`echo $* | sed -e 's/./#/g'`
    echo ${stars}
    echo $*
    echo ${stars}
}

die ()
{
    warn $*
    exit 1
}

# warn the user if she is not root.

if [ x`whoami` != xroot ]
then
  warn You are not installing as root.  This could be a problem.
fi

# unload any loaded GM drivers
if sysctl net.gm 2>&1 | grep -q gm 
then
    echo Taking down ethernet interfaces.
    unit=`sysctl net.gm.gm_base_en_unit | awk '{print $2}'`
    maxunit=`echo $unit | awk '{print 4 + $1}'`
    while [ $unit -le $maxunit ]
    do
	device="en$unit"
# ifconfig does not delete ipv6 addresses, even though
# it adds one automatically when an ipv4 address is added.
	if [ -x /usr/sbin/ip6 ]
	then
	    /usr/sbin/ip6 -d $device >/dev/null 2>&1
	fi
	ifconfig $device down delete >/dev/null 2>&1
	unit=`echo $unit | awk '{print 1 + $1}'`
    done
    echo "Removing installed gm drivers."
    kextunload -m com.Myricom.iokit.gm  >/dev/null 2>&1
fi


# Determine where to install from the command line args.

prefix="${1-.}"

test -e ${prefix} || die ${prefix} does not exist.
test -d ${prefix} || die ${prefix} is not a directory.
test -r ${prefix} || die ${prefix} is not readable.
test -x ${prefix} || die Cannot change directory to ${prefix}.

# Start logging, if possible, being careful to handle the case where the
# log file cannot be written because the install is occuring in-place
# in a non-writable directory.

log_file="${prefix}/.gm_install_log_for_`hostname`"
# Remove the log file since root can often do this even when the file
# is not writable by root if the directory is writable.
rm -f ${log_file} >/dev/null 2>&1
# Create the log file using touch instead of ">" since using ">" can
# result in a "Permission denied" message that is hard to supress.
touch ${log_file} >/dev/null 2>&1
# Empty the file by overwriting it.  This is important because the "rm"
# above may fail if the file is writable but the directory is not.  However,
# take care to use ">" only if the file is writable to prevent an error
# message.
test -w ${log_file} && ( echo Starting install as `whoami` > ${log_file} )
gm_install_log ()
{
    # Log only if the log file exists and is writable.
    test -w ${log_file} && ( "$*" >> ${log_file} 2>&1 )
}
gm_install_log date

# Copy the files to the user specified install directory, or leave them
# here if no install directory is specified.
pwd=`pwd` || die "Could not determine path."
prefix=`cd ${prefix} && pwd` || die "Could not canonicalize ${prefix}."
if [ ${pwd} != ${prefix} ]
then
  test -w ${prefix} || die ${prefix} is not writable.
  echo "Copying files to ${prefix}"
  cp -rf ./* ${prefix}/. || die Could not copy files.

  # run ranlib on the libgm.a library so that applications can link to it
  # you have to love mach-o.  
  RANLIB=/usr/bin/ranlib
  if [ -e $RANLIB ]
  then
    $RANLIB ${prefix}/lib/libgm.a
  else
    warn ranlib not found on your system, you may have trouble
    warn linking to libgm.a
   fi
else
  prefix=`dirname $0`
fi


echo "Adding new GM driver."
sync # just in case
# Grr..paranoid MacOSX 10.2 requires modules be owned by user root, group wheel
# in order to be loaded,  make it so
rm -rf /var/run/gm.kext
cp -rp ${prefix}/sbin/gm.kext /var/run
chown -R root:wheel /var/run/gm.kext
kextload /var/run/gm.kext || die Error installing GM driver module.
echo " "
# Go to sleep for a few seconds while module is loading
sleep 5

echo "dmesg | tail -15"
dmesg | tail -15 

# Record the details of the installation in the gm installation log
gm_install_log echo "# dmesg"
gm_install_log dmesg

echo " "
echo "Done"
exit 0

