#!/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

echo Taking down ethernet interfaces.
/sbin/ifconfig myri0 down >/dev/null 2>&1

if /sbin/lsmod | grep ^gm >/dev/null 2>&1
then
    echo "Removing installed gm drivers."
    /sbin/rmmod gm || die "Could not remove installed GM module."
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.
fi

# Make devices

echo "Removing device files in /dev."
for i in 0 1 2 3 4 5 6 7 ; do
  (
    /bin/rm -f /dev/gm$i /dev/gmp$i
  ) || die Could not delete a device.
done

sync
sync

# Go to sleep for 1 second while module is removing
sleep 1

echo "Adding new GM driver."
sync # just in case
/sbin/insmod ${prefix}/sbin/gm
insmod_status=$?
echo " "
echo "dmesg | tail -10"
/bin/dmesg | tail -10 
test $insmod_status = 0 || die Error installing GM driver module.

echo "Making device files in /dev."
if test -r /dev/.devfsd ; then : 
else
  for i in 0 1 2 3 4 5 6 7 ; do
    (
      mknod /dev/gm$i c 220 `expr $i "*" 2`\
      && chmod 666 /dev/gm$i\
      && mknod /dev/gmp$i c 220 `expr $i "*" 2 + 1`\
      && chmod 600 /dev/gmp$i\
    ) || die Could not create a device.
  done
fi

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

echo " "
echo "Done"
exit 0
