#!/bin/sh
# $Id: DT 596 2007-08-31 19:31:06Z mschultheiss@doubletake.com $
# DTMount - This script starts and stops the DT driver
#
# chkconfig: 345 13 87
# description: Data replication and high-availability service driver handler

# Get branding-related environment
# Bug#1757 Changed by @IndusaDevTeam
source /etc/DTBrand.sh

# Source function library. 
# Do not remove under pain of death!
. /lib/lsb/init-functions

### BEGIN INIT INFO
# Provides:          DTMount
# Required-Start:    $local_fs $syslog $network
# Required-Stop:     $local_fs $syslog $network
# X-Start-Before:    DT $remote_fs
# X-Stop-After:      DT $remote_fs 
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: Data replication and high-availability service
# Description:       Start DTMount to replicate data
### END INIT INFO

SCRIPT_NAME=${1##*/}
SERVICE_DISPLAY_NAME="${DTBRAND_DISPLAY_NAME_IN_TEXT_UI}"
SCRIPT="${SERVICE_DISPLAY_NAME} driver mount script"
DAEMON_BINARY_NAME="${DTBRAND_DAEMON_BINARY_NAME}"
INSTALL_SUBDIR="${DTBRAND_PATH_SUBDIR}"
RPM_PACKAGE_NAME="${DTBRAND_RPM_PACKAGE_NAME}"
EULA_FILE="/etc/${INSTALL_SUBDIR}/Read_And_Accepted_Double-Take_Software_End_User_License_Agreement"

PATH="$PATH":/usr/bin:/usr/local/bin
CONFIG_DIR="/etc/${INSTALL_SUBDIR}"

# The next few functions report problems, including the script name and the
# current process ID, to the system log (and sometimes stderr as well).  The
# default facility and priority are "user" and "notice".

grumble () {
    # Complain about the arguments to the system log.
    logger -i -t "$SCRIPT" "$*"
}

squawk () {
    # Screech the arguments to stderr and the system log.
    logger -i -s -t "$SCRIPT" "$*"
}

howl () {
    # Shout the arguments to stderr and the system log with a scary prefix.
    logger -i -s -p user.warning -t "$SCRIPT" "WARNING: $*"
}

load_dt_kernel_modules () {
    DTInit.sh -l
}

load_dtrep_settings () {
    DTInit.sh -s
}

mount_and_register_file_systems () {
    DTInit.sh -m -a
}

unregister_and_unmount_file_systems () {
    DTInit.sh -u -d
}

start () {
        # Start daemon.
        echo -n $"Starting $SERVICE_DISPLAY_NAME drivers: "
        driver_start
        RETVAL=$?
        if [ $RETVAL = 0 ]; then
            touch /var/lock/subsys/${SCRIPT_NAME}
	    log_success_msg
        else
            log_failure_msg
        fi
        echo
}

driver_start () {
    # Load the kernel modules.
    if ! load_dt_kernel_modules; then
        howl "$DAEMON_BINARY_NAME drivers not loaded; replication disabled"
	return 1
    fi

    # Wait for the user/kernel space communications link to come up.
    if ! MSG=$(DTRepWait 2>&1); then
        howl "$MSG; replication disabled"
	return 1
    fi

    # Load dtrep settings
    load_dtrep_settings || :

    # We don't check the return status of the following function because the
    # daemon can work just fine without any replication sources (the desired
    # configuration may have only targets).
    mount_and_register_file_systems
}

stop () {
    # Stop daemon.
    echo -n $"Stopping $SERVICE_DISPLAY_NAME drivers: "
    driver_stop
    RETVAL=$?
    if [ $RETVAL = 0 ]; then
        rm -f /var/lock/subsys/${SCRIPT_NAME} 
        log_success_msg
    else
        log_failure_msg
    fi
}

driver_stop () {
    # Unregister and unmount DTFS file systems.
    if ! unregister_and_unmount_file_systems; then
        howl "$SERVICE_DISPLAY_NAME may already have been stopped; if not, kernel" \
            "modules may not unload due to still-mounted or" \
            "still-registered DTFS file systems"
    fi

    # Unload the kernel modules.  modprobe does not make noise or exit
    # nonzero if it is asked to remove modules that aren't loaded (unlike
    # rmmod).
    #
    # We don't bother to unlink the modules from /lib/modules/$(uname -r) or
    # re-run depmod -a; it doesn't seem necessary and would waste time on
    # shutdown.  Unloading is still useful, though, particularly if the
    # user requests a daemon restart.  (If there was a problem loading them,
    # he or she might have fixed it.)
    #
    # Some kernels don't support unloading kernel modules, but that's not
    # the case for Red Hat Enterprise Linux 4.
    modprobe -r dtrep dtloop dtfs > /dev/null 2>&1 || true
}

status () {
    DTInit.sh -r 
    DTMountStatus=$?
    if [ "0" = "$DTMountStatus" ]; then
       echo $"${SCRIPT_NAME}: drivers loaded and config is correct"
       return 0
    fi
    if [ "1" = "$DTMountStatus" ]; then
       echo $"${SCRIPT_NAME}: drivers loaded but config not complete"
       return 1
    fi
    if [ "2" = "$DTMountStatus" ]; then
       echo $"${SCRIPT_NAME}: drivers loaded but config has problems"
       return 2
    fi
    if [ -f /var/lock/subsys/${SCRIPT_NAME} ]; then
       echo $"${SCRIPT_NAME}: drivers unloaded but subsys locked"
       return 3
    fi
    echo $"${SCRIPT_NAME}: drivers not loaded"
    return 4
}

# See if they have agreed to the EULA and Maintenance conditions
if [ ! -f "$EULA_FILE" ]; then
    squawk "${DTBRAND_PRODUCT_NAME} End User License Agreement not accepted! Run DTSetup."
    exit 0;
fi

# See if the daemon is installed.
if ! find /lib/modules -name DT >/dev/null 2>&1; then
    grumble "$DAEMON_BINARY_NAME drivers are not in install path; exiting"
    exit 0
fi

# Assume success until we learn otherwise.
RETVAL=0

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
       test -e /var/lock/subsys/${SCRIPT_NAME} && "$0" restart
       ;;
    status)
        status
        RETVAL=$?
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status|condrestart}"
        echo
        echo "    start   - initiate drivers and process driver config"
	echo "    stop    - tear down driver config and unload drivers" 
        echo "    restart - perform a stop, followed by a start"
        echo "    status  - outputs whether daemon is running"
        echo "    condrestart - performs a restart if daemon is already running"
        RETVAL=1
        ;;
esac

exit $RETVAL
