#! /bin/sh
#########################################################################
# Myricom GM networking software and documentation                      #
# Copyright (c) 1999 by Myricom, Inc.                                   #
# All rights reserved.  See the file `COPYING' for copyright notice.    #
#########################################################################

PATH=${prefix}/sbin:${prefix}/bin:/usr/bin:/usr/sbin:/sbin:/bin

# Determine where start/stop scripts reside on this system

if test -d /etc/init.d
then
    init_d="/etc/init.d"
elif test -d /etc/rc.d/init.d
then
    init_d="/etc/rc.d/init.d"
else
    gm_die "Cannot find init.d directory."
fi

gm_arch_driver_start_command="${init_d}/gm start"
gm_arch_install_program=install

gm_arch_stop_gm ()
{
    gm_arch_stop_ethernet 
    gm_do rmmod --verbose gm
}

gm_arch_start_gm ()
{
    # Harmless hack to remove pre-GM-1.6 drivers, which could otherwise
    # cause insmod to fail.
    gm_do rmmod --verbose gm > /dev/null 2>&1

    # Install the GM module
    gm_log_uninstall_command "rmmod --verbose gm"
    gm_do insmod --verbose /lib/modules/`uname -r`/gm/gm.o \
    || gm_die "Could not install GM module."
}

gm_arch_stop_ethernet ()
{
    ifconfig -a | grep "^myri[0-9]" | awk '{ print $1 }' | cut -d: -f1 | uniq |
    while read m
    do
	gm_do ifconfig ${m} down 2>&1 > /dev/null
    done
    true
}

gm_arch_start_ethernet ()
{
    true
}

gm_arch_install_ethernet ()
{
    true
}

gm_arch_install_gm ()
{
    # Extract from the driver the kernel release for which it was built.

    . ${prefix}/etc/gm/UTS_RELEASE \
    || gm_die "Could not determine driver's UTS_RELEASE"

    # Confirm that the driver is being installed for a matching kernel.

    uname__r=`uname -r`
    test "${GM_UTS_RELEASE}" = "${uname__r}" \
    || gm_die "The Linux-${GM_UTS_RELEASE} GM driver does not match the running Linux-`uname -r` kernel."

    # Create a directory for the GM module.

    gm_linux_installed_driver_dir="/lib/modules/${GM_UTS_RELEASE}/gm"
    if test ! -d "${gm_linux_installed_driver_dir}"
    then
	mkdir -p "${gm_linux_installed_driver_dir}" \
	|| gm_die "could not create \"${gm_linux_installed_driver_dir}\""
    fi
    gm_linux_installed_driver="${gm_linux_installed_driver_dir}/gm.o"

    # Remove any previously installed GM driver for this kernel version,
    # to ensure that it is not auto-loaded.
    
    gm_do find "/lib/modules/${GM_UTS_RELEASE}" \
	-type f \
	\( -name gm -o -name gm.o \) \
	-exec rm -f {} \; \
    || gm_die "could not remove old GM drivers in \"/lib/modules/${GM_UTS_RELEASE}\""

    # Install files

    gm_cp "${prefix}/sbin/gm.o" "${gm_linux_installed_driver}" \
    || gm_die "Could not copy the GM driver to \"${gm_linux_installed_driver}\""
    
    gm_log_uninstall_command "rm -f /dev/gm[0-9] /dev/gmp[0-9]" \
    || gm_die "Could add device removal commands to uninstall log."

    gm_install_program "${prefix}/etc/init.d/gm" "${init_d}/gm" \
    || gm_die "Could not install ${init_d}/gm"

    gm_install_program "${prefix}/bin/gm_set_name" "/sbin/gm_set_name" \
    || gm_die "Could not install /sbin/gm_set_name"
    
    gm_install_program "${prefix}/sbin/gm_mapper" "/sbin/gm_mapper" \
    || gm_die "Could not install /sbin/gm_mapper"
    
    # Harmless hack to remove pre-GM-1.6 devices, which could
    # otherwise cause mknod to fail or cause problems if devfs is
    # enabled in the kernel.
    for i in 0 1 2 3 4 5 6 7
    do
	rm -f /dev/gm$i /dev/gmp$i > /dev/null 2>&1
    done

    # Create the /dev/gm* devices, unless this system is using devfs,
    # where the driver itself creates the devices.

    if test ! -r /dev/.devfsd
    then
	for i in 0 1 2 3 4 5 6 7
	do
	    mknod /dev/gm$i c 220 `expr $i "*" 2` \
	    && gm_do chmod --verbose 666 /dev/gm$i 2>&1 > /dev/null \
	    && mknod /dev/gmp$i c 220 `expr $i "*" 2 + 1` \
	    && gm_do chmod --verbose 600 /dev/gmp$i 2>&1 > /dev/null \
	    || gm_die "Could not properly create /dev/gm*$i"
	done
    fi

    # Update the kernel module dependency database
    
    gm_do depmod --verbose -a "${GM_UTS_RELEASE}" \
    || gm_warn "depmod failed, but that's probably OK"

    # Flush all writes so far to disk.

    gm_do sync \
    || gm_die "Could not sync filesystem"

    # Arrange for the GM driver to be stopped before being uninstalled.

    gm_log_uninstall_command "${init_d}/gm stop" \
    || gm_die "Problem writing uninstall file"
}
