#! /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/scripts/sbin/gm_install_drivers,v $

# By default, arrange for the mapper to start in active mode.
gm_mapper_mode=active

################
# Install machine-specific configuration
################

gm_install_gm ()
{
    gm_arch_install_gm && return $?
}

gm_install_ethernet ()
{
    gm_arch_install_ethernet && return $?
}

gm_install_drivers ()
{
    gm_installed_make_id_matches "${prefix}" \
    || gm_die "--prefix=$prefix is not configured by GM_INSTALL."
    
    gm_create_uninstall_file "/sbin/gm_uninstall_drivers" \
    || gm_die "Could not create \"/sbin/gm_uninstall_drivers\""
    
    gm_install_gm \
    || gm_die "Could not install GM drivers"
    
    gm_install_ethernet \
    || gm_die "Could not install ethernet emulation"

    # Make sure gm_arch_install specifies the mapper state directory,
    # to gracefully catch incomplete gm_arch_install scripts.
    test "x${gm_arch_mapper_state_dir}" != x \
    || gm_die "gm_arch_install did not set gm_arch_mapper_state_dir to the name of the directory in which to store gm_mapper daemon state (e.g.: \"/var/run/gm_mapper\" on Linux)."
    # Create the mapper daemon state directory.  This is necessary here
    # only to ensure that the directory is removed when the drivers are
    # uninstalled.
    gm_mkdir "${gm_arch_mapper_state_dir}" \
    || gm_die "Could not create directory \"${gm_arch_mapper_state_dir}\""

    # Create the mapper per-machine settings directory.

    gm_mkdir /etc/gm_mapper \
    || gm_die "Could not create /etc/gm_mapper/."

    # Arrange for the mapper to start appropriately by touching
    # files in /etc/gm_mapper/.

    gm_log_uninstall_command "rm -f /etc/gm_mapper/active" \
    || gm_die "Could not log uninstall command"
    gm_log_uninstall_command "rm -f /etc/gm_mapper/manual" \
    || gm_die "Could not log uninstall command"
    case "${gm_mapper_mode}" in
	active)
	    echo "This file makes the mapper active on this node." \
	    > /etc/gm_mapper/active \
	    || gm_die "Could not create /etc/gm_mapper/active"
	    gm_echo ""
	    gm_echo "The mapper will be started automatically on this node."
	    ;;
	manual)
	    echo "This file makes the mapper not run on this node." \
	    > /etc/gm_mapper/manual \
	    || gm_die "Could not create /etc/gm_mapper/manual"
	    gm_echo ""
	    gm_echo "Remember that you must start the mapper manually with"
	    gm_echo "    ${prefix}/sbin/gm_mapper --map-once --verbose"
	    gm_echo "or similar each time you reboot this node or reload the GM driver."
	    ;;
	passive)
	    gm_echo ""
	    gm_echo "Remember that you must install the drivers with"
	    gm_echo "    $0 --active-mapper"
	    gm_echo "or"
	    gm_echo "    $0 --manual-mapper"
	    gm_echo "on at least one node."
	    # Do nothing
	    ;;
	*)
	    gm_die "Internal error: unsupported mapper mode"
	    ;;
    esac
    
    gm_close_uninstall_log \
    || gm_die "Could not close uninstall script"
}

gm_help_for_install_drivers ()
{
    gm_echo "Syntax: gm_install_drivers <options>"
    gm_echo "Legitimate options are:"
    gm_echo "    --active-mapper This mode is the default."
    gm_echo "                    Arrange for mappers automatically started"
    gm_echo "                    on this node to actively participate in"
    gm_echo "                    network mapping, rather than passively"
    gm_echo "                    waiting for other mappers to configure"
    gm_echo "                    the network."
    gm_echo "    --active-mappers=\"host1 host2 ...\""
    gm_echo "                    If the name of this host is in the list,"
    gm_echo "                    have the same effect as --active-mapper,"
    gm_echo "                    otherwise behave like --passive-mapper."
    gm_echo "    --copy          Copy files instead of linking them."
    gm_echo "                    (Useful if the GM driver must be brought up"
    gm_echo "                    before $prefix is mounted."
    gm_echo "    --help          Print this help information."
    gm_echo "    --link          Link subsequent files instead of copying"
    gm_echo "    --manual-mapper Do not auto-start a mapper on this node."
    gm_echo "    --passive-mapper"
    gm_echo "                    Arrange for mappers automatically started"
    gm_echo "                    on this node to not try to map the network,"
    gm_echo "                    but instead rely on remote mappers to do this."
    gm_echo "    --quiet         Turn off verbose output. [default]"
    gm_echo "    --verbose       Turn on verbose output."
}

# Check preconditions on user actions

gm_running_as_root \
|| gm_die GM drivers must be installed as root.

gm_installed_make_id_matches "${prefix}" \
|| gm_die "${prefix} does not hold a GM_INSTALLed release."

# Note where we can find the libtool script.

gm_libtool="${prefix}/etc/gm/gm_libtool"

# # Try to guess the install directory from whatever context information
# # we have.
# 
# gm_infer_installed_prefix

# Parse args, executing each in turn as a command.

gm_link_not_copy=0
while true
do
    case "$1" in
    	"")
	    break # Exit while loop
	    ;;
	--active-mapper)
	    gm_mapper_mode=active
	    ;;
	--active-mappers=*)
	    gm_mapper_mode=passive
	    gm_active_hosts=`echo "$1" | sed -e 's/--active-mappers=//'`
	    for gm_active_host in $gm_active_hosts
	    do
	      if [ x"$HOSTNAME" = x"${gm_active_host}" ]
	      then
		  gm_mapper_mode=active
		  break
	      fi
	    done
	    ;;
	--copy)
	    gm_link_not_copy=0
	    ;;
    	--help | -h | /?)
	    gm_help_for_install_drivers || exit $?
	    exit 0
    	    ;;
	--link)
	    gm_link_not_copy=1
	    ;;
	--manual-mapper)
	    gm_mapper_mode=manual
	    ;;
	--passive-mapper)
	    gm_mapper_mode=passive
	    ;;
    	--quiet | -q)
    	    gm_verbose=0
    	    ;;
    	--verbose | -v)
    	    gm_verbose=1
    	    ;;
	*)
	    gm_help_for_install_drivers
	    gm_die "Option \"$1\" is not recognized."
    esac
    shift
done

gm_install_drivers || gm_die "Could not install the GM driver."

gm_echo ""
gm_echo "GM drivers are now installed.  Remember to run"
gm_echo "	${gm_arch_driver_start_command}"
gm_echo "to start the GM driver."
