#!/bin/sh -c "echo This script should not be run directly.; exit 1"
################################################################
# Myricom GM networking software and documentation
# Copyright 2001, 2002 by Myricom, Inc.
# All rights reserved.   See the file `COPYING' for copyright notice.
################################################################
# author: glenn@myri.com
# $Source: /repository/gm/drivers/freebsd/gm_arch_install,v $
################################################################
# Required entry points
################################################################

# Make sure we find the right tools on this machine, regardless of
# the user's path.
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin
export PATH

gm_arch_driver_start_command="/usr/local/etc/rc.d/gm start"
gm_arch_install_program=install

gm_freebsd_version_checked=0
gm_debug_arch_stop_gm=0
gm_freebsd_major=201

gm_arch_stop_gm ()
{
    gm_do kldunload gm || \
    gm_do kldunload if_myri  # in case user loaded driver via ifconfig
}

gm_arch_start_gm ()
{
    gm_freebsd_check_version \
    && gm_do sync \
    && gm_do kldload gm
}

gm_arch_stop_ethernet ()
{
    for i in 0 1 2 3
    do
	gm_do /sbin/ifconfig myri${i} down > /dev/null 2>&1
	gm_do /sbin/ifconfig myri${i} delete > /dev/null 2>&1
    done
}

gm_arch_start_ethernet ()
{
    true
}

gm_arch_install_ethernet ()
{
    true
}

gm_arch_install_gm ()
{
    gm_freebsd_check_version \
    && gm_cp ${prefix}/sbin/gm.ko /modules/gm.ko \
    && gm_cp ${prefix}/etc/init.d/gm /usr/local/etc/rc.d/gm \
    && gm_freebsd_make_device_nodes \
    && gm_do sync \
    && gm_do sync \
    && gm_do sync \
    && gm_log_uninstall_command "/usr/local/etc/rc.d/gm stop" \
    && gm_echo "" \
    && gm_echo "To make the driver load at system boot, execute this command:" \
    && gm_echo "       ln -s /usr/local/etc/rc.d/gm /usr/local/etc/rc.d/gm.sh" \
    && gm_echo "" \
    && gm_echo "To make the driver load upon 'ifconfig myriX', execute this command:" \
    && gm_echo "       ln -s /modules/gm.ko  /modules/if_myri.ko" \
    && gm_echo ""

}

################################################################
# FreeBSD-specific functions
################################################################

gm_freebsd_check_version ()
{
    if test $gm_freebsd_version_checked != 0
    then
	return 0
    fi

    version=`uname -r`
    case "$version" in
    [123].* )
	gm_echo FreeBSD-"$version" is not supported by this release.
	status=1
	;;
    esac
    gm_freebsd_version_checked=1
    return 0
}

gm_freebsd_make_device_nodes ()
{
    version=`uname -r`
    case "$version" in
    5.*)
	return 0;     # FreeBSD 5.x always uses devfs, so do nothing here.
	;;
    esac

    # check to see that existing device nodes have correct major number
    if [ -c /dev/gm0 ];
    then
	installed_major=`ls -l /dev/gm0 | awk '{print $5}' |  sed -e 's/\,//'`
	if [ "x$installed_major" = "x$gm_freebsd_major" ]
	then
	    return 0;	# everything OK
	fi
    fi
# re-install the device nodes with the correct major number
# and permissions
    
    for i in 0 1 2 3 ; do
        gm_do rm -f /dev/gm$i /dev/gmp$i
	gm_do mknod /dev/gm$i c $gm_freebsd_major `expr $i "*" 2`
	gm_do chmod a+rw /dev/gm$i
	gm_do mknod /dev/gmp$i c $gm_freebsd_major `expr $i "*" 2 + 1`
	gm_do chmod u+rw /dev/gmp$i
    done

    for i in 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 ; do
        gm_do rm -f /dev/gm$i
	gm_do mknod /dev/gm$i c 201 $i
	gm_do chmod a+rw /dev/gm$i
    done
    return 0
}

