#!/bin/bash
#
# oops_dump_reg  
#
# chkconfig: 3 40 60
# description: Starts and stops oops_dump_reg driver
#

#insmod_flags="-f"
action() {
  STRING=$1
  echo -n "$STRING"
  shift
  $*
  rc=$?
  if test $rc -ne 0
  then
    echo " - Failure: $rc"
  else
    echo " - Success"
  fi
  return $rc
}
echo_success() {
    echo " - Success"
}
echo_failure() {
    echo " - Failure: $?"
}
failure() {
  echo -n "$* - Failure"
}

RETVAL=0

KMODULE=oops_dump_reg
KMOD_PRNT_NAME='kernel reg dump'

# Main function
start()
{
    echo "    Starting ${KMOD_PRNT_NAME}: "

    # Load all related modules

    if  lsmod | grep ${KMODULE} > /dev/null
    then
        echo
        echo "    WARNING: ${KMOD_PRNT_NAME} module already loaded"
        echo
    fi

    action "Loading ${KMOD_PRNT_NAME} module" modprobe ${insmod_flags} ${KMODULE}

}

stop()
{
    echo "Stopping ${KMOD_PRNT_NAME}"

    rmmod ${KMODULE} > /dev/null 2>&1
}

print_status()
{
    # Check modules
    if  lsmod | grep ${KMODULE} > /dev/null
    then
        echo "    ${KMOD_PRNT_NAME} module loaded"
    else
        echo "    ${KMOD_PRNT_NAME} module is not loaded"
    fi
}


# See how we were called.
case "$1" in
    help)
        cat <<END

   start

       Load module

   stop

       Stop module

   restart

       Restart module

   status

       Print current status of module load

END
        ;;
    start)
        if [ `id -u` -ne 0 ]; then
            echo "You must be root to do that"
            exit 1
        fi
        start
        ;;
    stop)
        if [ `id -u` -ne 0 ]; then
            echo "You must be root to do that"
            exit 1
        fi
        stop
        ;;
    status)
        print_status
        ;;
    restart)
        if [ `id -u` -ne 0 ]; then
            echo "You must be root to do that"
            exit 1
        fi
        stop
        start
        ;;
    *)
        echo "Usage:"
        echo "    $0 {start|stop|status|restart|help}"
        echo
        echo "Type \"$0 help\" for detailed help"
        RETVAL=1
esac
exit $RETVAL
