#! /bin/sh

function Print()
{
  if [ -n "${VERBOSE}" ]
  then
    echo "$@"
  fi

  if [ -n "${BOOTLOG}" ]
  then
    echo "$@" >> ${BOOTLOG}
  fi
}

function IsModule()
{
  # The module in $1 may not have a .o extension.
  [ -n "`${BIN_MODPROBE} -l $1`" ] || \
    [ -n "`${BIN_MODPROBE} -l $1.o`" ] || false
}

# Usage: ModuleInsert <module[.o]> [<options>...]
function ModuleInsert()
{
  [ -n "$1" ] && IsModule $1 && ${BIN_MODPROBE} "$@"
}

# Usage: ModuleRemove <module[.o]>
function ModuleRemove()
{
  [ -n "$1" ] && IsModule $1 && ${BIN_MODPROBE} -r $1
}

function SendSignal()
{
  signal=$1
  shift
  for dir in $*
  do
    [ -n "`${BIN_LS} $dir`" ] && ${BIN_FUSER} -km $dir -$signal
  done
}

function FindMounts()
{
  list=""

  if [ -z "$1" ]
  then
    # Must be updated if other filesystems are used as local filesystems.
    search="ext2"
  else
    search="$1"
  fi

  if [ -r /proc/mounts ]
  then
    mountfile="/proc/mounts"
  else
    mountfile="/etc/fstab"
  fi

  while read device mtpoint type options n1 n2
  do
    if [ "$mtpoint" = "/" ]
    then
      continue
    fi

    case "$type" in
      $search)
        list="$list $mtpoint"
        ;;
    esac
  done < $mountfile
}

function SleepTerm()
{
  if [ -z "${KILLTIME1}" ]
  then
    ${BIN_SLEEP} 5
  else
    ${BIN_SLEEP} ${KILLTIME1}
  fi
}

function SleepKill()
{
  if [ -z "${KILLTIME2}" ]
  then
    ${BIN_SLEEP} 5
  else
    ${BIN_SLEEP} ${KILLTIME2}
  fi
}

function Terminate()
{
  if [ -f "${RUN}/$1.pid" ]
  then
    # PID file exists.
    kill `${BIN_CAT} "${RUN}/$1.pid"`
    return 0
  fi

  if [ -n "$2" ]
  then
    # Assume PID file exists.
    kill `${BIN_CAT} $2`
  else
    list=`pidof $1`
    for i in $list
    do
      [ $i = $$ ] || kill "$i"
    done
  fi
}

function Usage()
{
  echo "Usage: $1 {start|stop}"
}
