#! /bin/bash
#
# ../scripts/realtime.  Generated from realtime.in by configure. 
# on Wed Aug  2 13:54:53 CEST 2006
#

CheckConfig(){
    RUN_IN_PLACE=yes
    prefix=/usr/local
    exec_prefix=${prefix}
    sysconfdir=${prefix}/etc
    if [ $RUN_IN_PLACE = yes ]; then
        RTAPICONF=
        # check in the emc2 scripts directory
        # $0 is the command to run this script
        # strip the script name to leave the path
        SCRIPT_DIR=${0%/*}
        # the path might be relative
        # convert it to an absolute path
        SCRIPT_DIR=$(cd $SCRIPT_DIR ; pwd -P)
        # now look for rtapi.conf there
        if [ -f $SCRIPT_DIR/rtapi_rt.conf ] ; then
            RTAPICONF=$SCRIPT_DIR/rtapi_rt.conf
        fi
    else
        if [ -f $sysconfdir/emc2/rtapi_rt.conf ]; then
                RTAPICONF=$sysconfdir/emc2/rtapi_rt.conf
        fi
    fi
    if [ -z "$RTAPICONF" ] ; then
	echo "Missing rtapi.conf.  Check your installation." 1>&2
	exit 1
    fi
    INSMOD="/root/emc2/bin/emc_module_helper insert"
    RMMOD="/root/emc2/bin/emc_module_helper remove"

    # Save the system variable MODPATH so that it can be
    # re-exported on exit.
    old_path=$MODPATH
    # Import the config
    source $RTAPICONF
    if [ ! -e $RTLIB_DIR/.runinfo -a -e $RTLIB_DIR/emc2/.runinfo ]; then
        # installed system: we don't want our modules mixed up with rtai
        RTLIB_DIR=$RTLIB_DIR/emc2
    fi
    # Export the module path specified in the config.
    export MODPATH
    # Generate module lists for loading and unloading
    # lists contain RTOS modules plus RTAPI and HAL
    # unload list is in reverse order
    # any module names that are symlinks are resolved to their real names
    MODULES_LOAD=
    MODULES_UNLOAD=
    for  MOD in $MODULES ; do
	# find module
	TMP=`find $MODPATH -name $MOD$MODULE_EXT`
	if test -n "$TMP" ; then
	    # it exists, is it a symlink?
            if test -L "$TMP" ; then
		# it is a symlink, must extract real module name
		# get long directory entry
		TMP=`ls -l $TMP`
		# remove everything except the last token, which
		# should be the destination of the symbolic link
		TMP="${TMP##* }"
		TMP="${TMP##*/}"
		# remove the .o  (or .ko) from the end
		MOD="${TMP%$MODULE_EXT}"
	    fi
            MOD=${MODPATH}/${MOD}${MODULE_EXT}
        else
            MOD=`find /lib/modules/$(uname -r) -name $MOD$MODULE_EXT`
        fi
	MODULES_LOAD="$MODULES_LOAD $MOD"
        MOD="${MOD##*/}"
        MOD="${MOD%$MODULE_EXT}"
	MODULES_UNLOAD="$MOD $MODULES_UNLOAD"
    done
    MODULES_LOAD="$MODULES_LOAD $RTLIB_DIR/rtapi$MODULE_EXT $RTLIB_DIR/hal_lib$MODULE_EXT"
    MODULES_UNLOAD="hal_lib rtapi $MODULES_UNLOAD"
    # find "fuser" - it might be in /bin or /sbin
    if test -x /bin/fuser ; then
	FUSER=/bin/fuser
    elif test -x /sbin/fuser ; then
	FUSER=/sbin/fuser
    else
	echo "ERROR: can't find \"fuser\", used to verify that it is safe to shutdown realtime"
	DoExit
	exit 1
    fi
    case "$MODULES" in
    *rtai_shm*)
        SHM_DEV=/dev/rtai_shm
    ;;
    *mbuff*)
        SHM_DEV=/dev/mbuff
    ;;
    esac

}

CheckStatus(){
    # check loaded/unloaded status of modules
    unset NOTLOADED
    for MOD in $MODULES_UNLOAD ; do
	if /sbin/lsmod | awk '{print $1}' | grep -x $MOD >/dev/null ; then
	    echo "$MOD is loaded"
	else
	    echo "$MOD is not loaded"
            NOTLOADED=NOT
	fi
    done
    if [ -z $NOTLOADED ]; then
        exit 0
    else
        exit 1
    fi
}

CheckMem(){
# check for user space processes using shared memory
    if [ -e /dev/mbuff ] ; then
	# device file exists, check for processes using it
	if $FUSER -s /dev/mbuff 2>/dev/null; then
	    # at least one process is using it
	    echo "ERROR:  Can't remove RTLinux modules, kill the following process(es) first"
	    $FUSER -v /dev/mbuff
	    DoExit
	    exit 1
	fi
    elif [ -e /dev/rtai_shm ] ; then
	# device file exists, check for processes using it
	if $FUSER -s /dev/rtai_shm 2>/dev/null; then
	    # at least one process is using it
	    echo "ERROR:  Can't remove RTAI modules, kill the following process(es) first"
	    $FUSER -v /dev/rtai_shm
	    DoExit
	    exit 1
	fi
    fi
}

Load(){
    for MOD in $MODULES_LOAD ; do
	# check to see if the module is already installed
	if ! /sbin/lsmod | awk '{print $1}' | grep -x $MOD >/dev/null ; then
	    # install the module - use insmod
	    if [ "$VERBOSE" = "1" ] ; then
	        $INSMOD $MOD
	    else
	        $INSMOD $MOD 1>/dev/null
	    fi
        fi
    done
    if [ "$DEBUG" != "" ] && [ -w /proc/rtapi/debug ] ; then
        echo "$DEBUG" > /proc/rtapi/debug
    fi
}

CheckLoaded(){
# checks to see if rtapi and hal_lib modules were loaded
# (would like to check all modules, but some may not actually
#  be required or present on all systems)
    STATUS=
    for module in rtapi hal_lib ; do
	# check to see if the module is installed
	if ! /sbin/lsmod | awk '{print $1}' | grep -x $module >/dev/null ; then
	    echo "ERROR: Could not load '$module'"
	    STATUS=error
	fi
    done
    if [ -n "$STATUS" ] ; then
	DoExit
	exit 1
    fi
    # this abomination is needed because udev sometimes doesn't
    # have the device ready for us in time.
    n=0
    while [ $n -lt 10 ]; do
        [ -w $SHM_DEV ] && return 0
        echo "." 1>&2
        sleep 1
        n=$(($n+1))
    done
    echo "Can't write to $SHM_DEV - aborting" 1>&2
    DoExit
    exit 1
}

Unload(){
    for module in $MODULES_UNLOAD ; do
        if /sbin/lsmod | awk '{print $1}' | grep -x $module >/dev/null ; then
	    $RMMOD $module
	fi
    done
}

CheckUnloaded(){
# checks to see if all modules were unloaded
    STATUS=
    for module in $MODULES_UNLOAD ; do
	# check to see if the module is installed
	if /sbin/lsmod | awk '{print $1}' | grep -x $module >/dev/null ; then
	    echo "ERROR: Could not unload '$module'"
	    STATUS=error
	fi
    done
    if [ -n "$STATUS" ] ; then
	DoExit
	exit 1
    fi
}

DoExit(){
    # Export the original MODPATH
    export MODPATH=$old_path
}

if [ "$1" = "-v" ] ; then
  CMD=$2
  VERBOSE=1
else
  CMD=$1
  VERBOSE=0
fi

case "$CMD" in
  start|load)
	CheckConfig
	Load
	CheckLoaded
	DoExit
	;;
  restart|reload)
	CheckConfig
	CheckMem
	Unload
	CheckUnloaded
	Load
	CheckLoaded
	DoExit
	;;
  stop|unload)
	CheckConfig
	CheckMem
	Unload
	CheckUnloaded
	DoExit
	;;
  status)
	CheckConfig
	CheckStatus
	DoExit
	;;
  *)
	echo "Usage: $0 {start|load|stop|unload|restart|reload|status}" >&2
	exit 1
	;;
esac

exit 0

