#! /bin/sh -c "echo This script should not be run directly.; exit 1"

gm_service=gm

gm_enumerate_units ()
{
    ${prefix}/bin/gm_board_info \
    | grep '^Board number [0-9]*:$' \
    | sed -e 's/.* \([0-9]*\):$/\1/'
}

# Start the mapper, die on failure.
gm_start_mapper ()
{
    # Create the mapper state directory (since it may not be persistent
    # across reboots).
    if test ! -d "${gm_arch_mapper_state_dir}"
    then
	gm_mkdir "${gm_arch_mapper_state_dir}" \
	|| gm_die "Could not create ${gm_arch_mapper_state_dir}."
    fi

    # Make sure only root can access the mapper state directory, to
    # prevent others from removing the mapper PID file or otherwise
    # tampering with state there.
    chmod go-w "${gm_arch_mapper_state_dir}" \
    || gm_die "Could not protect ${gm_arch_mapper_state_dir}."

    # Return before starting the mapper if the user wants to start the
    # mapper manually.

    if [ -f /etc/gm_mapper/manual ]
    then
	printf "%s" "no mapper... "
	return 0
    fi
	
    # Create a list of boards in the machine.
    units=`gm_enumerate_units` || gm_die "Could not enumarate boards."

    gm_verbose_echo "Installing on units ${units}."

    # Start a mapper for each unit (board) in the system.
    for unit in ${units}
    do
      unit_ext=".${unit}"

      # Determine if the mapper should be passive.  (It should be
      # passive unless file /etc/gm_mapper/active exists.)
      
      if [ -f /etc/gm_mapper/active ]
      then
	  printf "%s" "active mapper... "
	  active_passive=""
      else
	  printf "%s" "passive mapper... "
	  active_passive="--level=0"
      fi
      
      # Start the mapper daemon, which must run on every board
      sleep 2
      gm_log_uninstall_command \
	  "rm -f \"${gm_arch_mapper_state_dir}/pid${unit_ext}\""
      gm_log_uninstall_command \
	  "kill \`cat \"${gm_arch_mapper_state_dir}/pid${unit_ext}\"\`"
      gm_do ${prefix}/sbin/gm_mapper\
          --unit="${unit}"\
	  --daemon-pid-file="${gm_arch_mapper_state_dir}/pid${unit_ext}"\
	  --verbose-file="${gm_arch_mapper_state_dir}/verbose${unit_ext}"\
	  --map-file="${gm_arch_mapper_state_dir}/map${unit_ext}"\
	  ${active_passive}\
	  || gm_die "Could not start ${prefix}/sbin/gm_mapper"
    done
}

# Stop the mapper, returning nonzero if we are certain we failed.
gm_stop_mapper ()
{
    # Kill all running mappers, as specified by their PID files.
    for pid_file in `ls "${gm_arch_mapper_state_dir}"/pid.* 2>/dev/null`
    do
      # Read the stored PID.
      pid=`cat "${pid_file}"` || return $?
      # Kill the mapper.
      kill "${pid}"
      rm "${pid_file}"
    done

    # Pretend to succeed, even if we failed, because it is OK to fail if
    # the mapper has already exitted, and it is hard to know if this
    # is the case.
    #
    # Any gm_stop_* function may return 0 when it fails, but shouldn't do
    # so unnecessarily, for efficiency.
    
    return 0
}
    
gm_start_gm ()
{
    # Return successfully if GM has been uninstalled, as required by
    # Debian policy.

    if test ! -f "${prefix}/bin/gm_board_info"
    then
	gm_echo "GM has been uninstalled."
	return 0
    fi
    
    # If we can run gm_board_info, then an old version of the
    # driver is running, and we cannot start GM.
    gm_do "${prefix}/bin/gm_board_info" > /dev/null 2>&1 \
    && gm_die GM must be stopped first.
    
    gm_arch_start_gm || return $?

    # Make sure that the driver actually initialized an interface by
    # running gm_board_info
    gm_wait_for_driver_to_load

    # Name each NIC using the host name.
    units=`gm_enumerate_units` || gm_die "Could not enumarate boards."
    
    for board in ${units}
    do
      # Get the default hostname
      hostname=`hostname`
      # Allow user file to override this host name
      if [ -f "/etc/gm/hostname" ]
      then
	  hostname=`cat /etc/gm/hostname` \
	  || die "Could not read /etc/gm/hostname"
      fi
      # Append ":<board_num>" to the hostname for boards other than 0.
      if test "${board}" != 0 ; then hostname="${hostname}:${board}" ; fi
      # Allow user file to override the derived host name for each board.
      if [ -f "/etc/gm/hostname.${board}" ]
      then
	  hostname=`cat /etc/gm/hostname.${board}` \
	  || die "Could not read /etc/gm/hostname.${board}"
      fi
      gm_do ${prefix}/bin/gm_set_name \
      --board="${board}" \
      --host-name="${hostname}" \
      2>&1 > /dev/null \
      || gm_die "Could not set host name for board ${board} to \"${hostname}\""
    done

    gm_start_mapper || gm_die "Could not start the mapper."

    return 0
}

gm_stop_gm ()
{
    # Return successfully if GM has been uninstalled, as required by
    # Debian policy.

    if test ! -f "${prefix}/bin/gm_board_info"
    then
	gm_echo "GM has been uninstalled."
	return 0
    fi

    # Stop the mapper daemon.

    gm_stop_mapper || return $?

    # Unload the driver
    
    gm_arch_stop_gm || return $?

    # If we can still run gm_board_info, then we failed to unload the
    # driver.
    gm_do "${prefix}/bin/gm_board_info" > /dev/null 2>&1 \
    && return 1

    return 0
}

# chkconfig: 2345 20 80
# description: Provides GM API support and ethernet emulation for \
#              Myrinet network interface cards.
