#! /bin/sh
### BEGIN INIT INFO
# Provides:          maintenance_recordum
# Required-Start:    $local_fs $network postgresql
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Recordum Maintenacne initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Author: Christian Poms <cp@recordum.com>
#

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="recordum maintenance tasks"
NAME=maintenance_recordum
SCRIPTNAME=/etc/init.d/$NAME


. /usr/local/recordum/sys/defaults.include
# Include support for automatic ethernet driver check
. /usr/local/recordum/dataproc/eth_driver_utils.include

INSTALLHDD_DAEMON=$REC_SYSDIR/installhdd
INSTALLHDD_DAEMONARGS=$INSTALLHDD_LOGFILE
#INSTALLHDD_DAEMONARGS=""

. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions






VERBOSE=yes
DO_RESTART=0


get_sysstatus()
{
    SYSSTATUS="ready"
    INSTALL_STATUS="none"
    if [ "$HWSTATUS" != "HDDMOUNTED" ] ; then SYSSTATUS="stopped"; fi;
    if [ -f "$INSTALLHDD_STATUSFILE" ] ; then
        INSTALL_STATUS=`cat $INSTALLHDD_STATUSFILE`
        if [ "$INSTALL_STATUS" != "finished" ] ; then
            SYSSTATUS="stopped"
        fi
        if [ "$INSTALL_STATUS" = "setuphdd" ] ; then
            # Zero length of install logfile (bcecause it's beginning again)
            [ -f "$INSTALLHDD_LOGFILE" ] && echo "" > $INSTALLHDD_LOGFILE
        fi
    fi
}


start_installhdd() {

    touch $INSTALLHDD_LOGFILE

    start-stop-daemon --start --quiet --background --exec $INSTALLHDD_DAEMON --test > /dev/null \
            || return 1
    start-stop-daemon --start --quiet --background --exec $INSTALLHDD_DAEMON -- \
            $INSTALLHDD_DAEMONARGS \
            || return 2
}


do_start()
{
    ret=0

    # Check ethernet driver
    #check_lan_driver
    #[ $ETH_RESTART -eq 1 ] && DO_RESTART=1 && ret=2

    # Set hwstatus file (for user interface)
    HWSTATUS=`$SYSINFO hwstatus`
    echo $HWSTATUS > $HWSTATUS_FILE
    chmod 644 $HWSTATUS_FILE

    get_sysstatus
    echo $SYSSTATUS > $SYSSTATUS_FILE
    chmod 644 $SYSSTATUS_FILE

    if [ "$HWSTATUS" = "HDDMOUNTED" ] && [ "$SYSSTATUS" = "ready" ] ; then
        # Switch index.php file 
        [ -f $WWWDIR/index.php.default ] && cp $WWWDIR/index.php.default $WWWDIR/index.php
    else 
        # Check, if old system hdd is mountable
        if [ "$HWSTATUS" = "HDDATTACHED" ] ; then
            mount $SYSTEMV1_HDD
        fi
        [ -f $WWWDIR/index.php.HWFAILURE ] && cp $WWWDIR/index.php.HWFAILURE $WWWDIR/index.php
    fi
    
    if [ "$INSTALL_STATUS" != "none" ] ; then
        start_installhdd
    fi

    #if [ $DO_RESTART -eq 1 ]; then
    #    echo "Will restart system in 1min!"
    #    shutdown -r +1 &
    #fi
  
    return $ret;   
}


do_stop() {
    return 0;
}





case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop|force-stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop}" >&2
	exit 3
	;;
esac

