#!/bin/bash
#
# chkconfig: 5 96 4
# description: PCoIP Host Software driver and daemon
#

### BEGIN INIT INFO
# Provides: pcoip_host
# Required-Start: $local_fs $network $syslog pcoip_hostcheck
# Should-Start: $null
# Required-Stop: $null
# Default-Start: 5
# Default-Stop: 0 1 2 3 4 6
# Short-Description: PCoIP Host Software driver and daemon
# Description: This script starts and stops the pcoip_host.ko driver. It
#       creates the pcoip_host0 and pcoip_host1 file descriptors in the
#       /dev directory, which user space applications access to
#       control/interact with the pcoip_host kernel module.
#
#       This script also starts and stops the Host SW daemon (pcoiphostswd).
### END INIT INFO


# Source function library.
if [ -f /lib/lsb/init-functions ]; then
  . /lib/lsb/init-functions
  alias START_DAEMON=start_daemon
  alias STATUS=MyStatus
  alias LOG_SUCCESS=log_success_msg
  alias LOG_FAILURE=log_failure_msg
  alias LOG_WARNING=log_warning_msg
elif [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
  alias START_DAEMON=daemon
  alias STATUS=status
  alias LOG_SUCCESS=success
  alias LOG_FAILURE=failure
  alias LOG_WARNING=passed
else
  echo "Error: platform is not supported by pcoip_host script" > /dev/stderr
  exit 1
fi

DEVICE="pcoip_host"
DEVICE0="pcoip_host0"
DEVICE1="pcoip_host1"

# kernel version, used to look for modules
KERNEL=`uname -r`

# the directory the module pcoip_host.ko should be in
MODDIR="/lib/modules/${KERNEL}/kernel/drivers/input/misc"

# define the owner, group and mode for accessing the file descriptor
OWNER=root
GROUP=root
MODE=666

# macros used within this script file
DAEMON="/usr/bin/pcoiphostswd"
prog="pcoiphostswd"

function check() {
    # Check that we're a privileged user
    [ `id -u` = 0 ] || exit 4

    # Check if Host SW Daemon is executable
    [ -x $DAEMON ] || exit 5
}

function create_files () {
#    echo "create device files - start"
    cd /dev
    mknod ${DEVICE0} c $MAJOR 0
    chown $OWNER ${DEVICE0}
    chgrp $GROUP ${DEVICE0}
    chmod $MODE  ${DEVICE0}
    mknod ${DEVICE1} c $MAJOR 1
    chown $OWNER ${DEVICE1}
    chgrp $GROUP ${DEVICE1}
    chmod $MODE  ${DEVICE1}
#    echo "create device files - end"
}

function remove_files () {
#    echo "remove device files - start"
    cd /dev
    rm -f ${DEVICE0}
    rm -f ${DEVICE1}
#    echo "remove device files - end"
}

function load_device () {
    # load the module if it is not already loaded
    if [ "$(/sbin/lsmod | grep $DEVICE)" == "" ]
    then
        /sbin/insmod $MODDIR/$DEVICE.ko
    fi

    # find the major device number and create a file descriptor for it
    remove_files
    if [ "$(grep $DEVICE /proc/devices)" != "" ]
    then
        MAJOR=`awk "\\$2==\"$DEVICE\" {print \\$1}" /proc/devices`
        create_files
    else
        echo -n "ERROR: verify the Host Driver Function is enabled on PCoIP Host card"
        echo "."
    fi
}

function unload_device () {
    remove_files
}

function start_hostsw_daemon () {
    echo -n $"Starting $prog: "

    # start daemon
    $START_DAEMON $DAEMON
    return 0
}

function stop_hostsw_daemon () {
    check

    # stop daemon
    echo -n $"Stopping $prog: "
    killproc $DAEMON
    return 0
}


case "$1" in
  start)
     check
     load_device
     start_hostsw_daemon
     ;;
  stop)
     check
     echo -n "Stopping daemon and unloading $DEVICE"
     echo "."
     stop_hostsw_daemon
     unload_device
     ;;
  force-reload|restart|reload)
     check
     echo -n "Reloading $DEVICE and restarting daemon"
     echo "."
     stop_hostsw_daemon
     unload_device
     load_device
     start_hostsw_daemon
     ;;
  *)
     echo "Usage: $0 {start|stop|restart|force-reload|reload}"
     exit 1
esac

exit 0
