Difference between revisions of "OS4X Core init script"

From OS4X
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
For runlevel scripts, this script can be used as a basis for further needs. It works good in an OS4XBox environment.
+
For runlevel scripts, this script can be used as a basis for further needs. It works good in an OS4XBox environment. Actual versions of init-scripts can be found here: https://www.os4x.com/downloads/
 
<pre>
 
<pre>
#! /bin/sh
+
#!/bin/bash
 
### BEGIN INIT INFO
 
### BEGIN INIT INFO
# Provides:         OS4X
+
# Provides: os4x
# Required-Start:   mysql
+
# Required-Start: mysql
# Should-Start: mysql
+
# Required-Stop: mysql
# Default-Start:     3 5
+
# Default-Start: 2 3 5
# Default-Stop:     0 1 2 6
+
# Default-Stop: 0 1 6
# Short-Description: OS4X
+
# Description: Start or stop OS4X
# Description:       Start and stop OS4X daemons
 
 
### END INIT INFO
 
### END INIT INFO
 +
 +
 +
#
 +
# OS4X
 +
#
  
 
# Check for existence of needed config file and read it
 
# Check for existence of needed config file and read it
Line 21: Line 25:
 
. $OS4X_CONFIG
 
. $OS4X_CONFIG
  
# retrieve backup directory
+
# calculate MySQL client parameters
OS4X_BINDIR=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='bin_directory'" | $MYSQLCLIENT --silent --user=$DB_USER --password=$DB_PASS --host=$DB_HOST $DB_NAME`
+
MYSQLCLIENT_PARAMS="--silent --user=$DB_USER --password=$DB_PASS --host=$DB_HOST $DB_NAME"
OS4X_ENTERPRISE=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='os4x_enterprise'" | $MYSQLCLIENT --silent --user=$DB_USER --password=$DB_PASS --host=$DB_HOST $DB_NAME`
+
if [ ! -z "$DB_TLS_VERSION" ]
 +
then
 +
MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --tls-version=$DB_TLS_VERSION"
 +
fi
 +
if [ ! -z "$DB_TLS_CIPHER" ]
 +
then
 +
MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-cipher=$DB_TLS_CIPHER"
 +
fi
 +
if [ ! -z "$DB_TLS_KEY" ]
 +
then
 +
MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-key=$DB_TLS_KEY"
 +
fi
 +
if [ ! -z "$DB_TLS_CERT" ]
 +
then
 +
MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-cert=$DB_TLS_CERT"
 +
fi
 +
if [ ! -z "$DB_TLS_CA" ]
 +
then
 +
MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-ca=$DB_TLS_CA"
 +
fi
 +
if [ ! -z "$DB_TLS_CAPATH" ]
 +
then
 +
MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-capath=$DB_TLS_CAPATH"
 +
fi
 +
 
 +
# retrieve directories and settings
 +
OS4X_BINDIR=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='bin_directory'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`
 +
OS4X_TMPDIR=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='tmp_directory'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`
 +
OS4X_IS_PORTAL=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='enterprise_is_this_portal_server'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`
 +
 
 +
if [ -z "${OS4X_IS_PORTAL}" ]
 +
then
 +
OS4X_IS_PORTAL=0
 +
fi
 +
 
 +
# check license
 +
if [ -e ${OS4X_BINDIR}/license.key ]
 +
then
 +
LICFUNCTIONS=`${OS4X_BINDIR}/os4x_licinfo -A 2>/dev/null`
 +
else
 +
LICFUNCTIONS=0
 +
fi
 +
 
 +
if [ $? -ne 0 ]
 +
then
 +
LICFUNCTIONS=0
 +
fi
 +
 
 +
(((${LICFUNCTIONS}&1)==1)) && LIC_OS4XCORE=1 || LIC_OS4XCORE=0
 +
(((${LICFUNCTIONS}&2)==2)) && LIC_OS4XENTERPRISE_LITE=1 || LIC_OS4XENTERPRISE_LITE=0
 +
(((${LICFUNCTIONS}&4)==4)) && LIC_OS4XENTERPRISE_ALL=1 || LIC_OS4XENTERPRISE_ALL=0
 +
(((${LICFUNCTIONS}&8)==8)) && LIC_OS4X_AS2=1 || LIC_OS4X_AS2=0
 +
(((${LICFUNCTIONS}&16)==16)) && LIC_OS4X_PORTAL=1 || LIC_OS4X_PORTAL=0
 +
 
 +
if [ "${OS4X_IS_PORTAL}" -eq 1 -a ${LIC_OS4X_PORTAL} -eq 1 ]
 +
then
 +
LIC_OS4X_PORTAL=0
 +
LIC_OS4XENTERPRISE_ALL=1
 +
fi
 +
 
 +
OS4X_WEBINTERFACE=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='webinterface_path'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`
  
 
# Check for missing binaries (stale symlinks should not happen)
 
# Check for missing binaries (stale symlinks should not happen)
 
# Note: Special treatment of stop for LSB conformance
 
# Note: Special treatment of stop for LSB conformance
 
OS4XRD_BIN=$OS4X_BINDIR/os4xrd
 
OS4XRD_BIN=$OS4X_BINDIR/os4xrd
test -x $OS4XRD_BIN || { echo "$OS4XRD_BIN not installed";
 
        if [ "$1" = "stop" ]; then exit 0;
 
        else exit 5; fi; }
 
 
 
OS4XSQD_BIN=$OS4X_BINDIR/os4xsqd
 
OS4XSQD_BIN=$OS4X_BINDIR/os4xsqd
test -x $OS4XSQD_BIN || { echo "$OS4XSQD_BIN not installed";
+
OS4XCLIENTD_BIN=$OS4X_BINDIR/os4xclientd
        if [ "$1" = "stop" ]; then exit 0;
+
OS4XAS2_BIN=$OS4X_BINDIR/os4x_as2
        else exit 5; fi; } 
+
OS4XPOLL_BIN=$OS4X_BINDIR/os4xpoll
 +
OS4XPORTAL_BIN=$OS4X_BINDIR/os4xportald
  
OS4XDEBUGD_BIN=$OS4X_BINDIR/os4xdebugd
+
# sanity checks for daemon existance
test -x $OS4XDEBUGD_BIN || { echo "$OS4XDEBUGD_BIN not installed";
+
if [ ! -e "$OS4XCLIENTD_BIN" ]
        if [ "$1" = "stop" ]; then exit 0;
+
then
        else exit 5; fi; } 
+
LIC_OS4XENTERPRISE_ALL=0
 
+
LIC_OS4XENTERPRISE_LITE=0
if [ $OS4X_ENTERPRISE -gt 0 ]
+
fi
 +
if [ ! -e "$OS4XPORTAL_BIN" ]
 +
then
 +
LIC_OS4X_PORTAL=0
 +
fi
 +
if [ ! -e "$OS4XAS2_BIN" ]
 
then
 
then
        OS4XCLIENTD_BIN=$OS4X_BINDIR/os4xclientd
+
LIC_OS4X_AS2=0
        test -x $OS4XCLIENTD_BIN || { echo "$OS4XCLIENTD_BIN not installed";
 
                if [ "$1" = "stop" ]; then exit 0;
 
                else exit 5; fi; }
 
 
fi
 
fi
 +
 +
function killDaemon
 +
{
 +
if [ -f ${OS4X_TMPDIR}/${1}.pid ]
 +
then
 +
kill `cat ${OS4X_TMPDIR}/${1}.pid`
 +
else
 +
killall ${1} >/dev/null 2>&1
 +
fi
 +
}
  
 
case "$1" in
 
case "$1" in
 +
    stop)
 +
echo -n "Shutting down OS4X "
 +
pidof os4xrd >/dev/null 2>&1 
 +
if [ $? -eq 0 ]
 +
then
 +
echo -n "os4xrd... "
 +
killDaemon os4xrd
 +
fi
 +
pidof os4xsqd >/dev/null 2>&1 
 +
if [ $? -eq 0 ]
 +
then
 +
echo -n "os4xsqd... "
 +
killDaemon os4xsqd
 +
fi
 +
pidof os4xdebugd >/dev/null 2>&1 
 +
if [ $? -eq 0 ]
 +
then
 +
echo -n "os4xdebugd... "
 +
killDaemon os4xdebugd
 +
fi
 +
pidof os4xclientd >/dev/null 2>&1 
 +
if [ $? -eq 0 ]
 +
then
 +
echo -n "os4xclientd... "
 +
killDaemon os4xclientd
 +
fi
 +
pidof os4x_as2_master >/dev/null 2>&1 
 +
if [ $? -eq 0 ]
 +
then
 +
echo -n "os4x_as2... "
 +
killDaemon os4x_as2
 +
fi
 +
pidof os4xportald >/dev/null 2>&1 
 +
if [ $? -eq 0 ]
 +
then
 +
echo -n "os4xportald... "
 +
killDaemon os4xportald
 +
fi
 +
echo OK
 +
;;
 
     start)
 
     start)
        echo -n "Starting OS4X "
+
    # pre-check lockfile
        echo -n "os4xrd... "
+
    if [ -d $OS4X_WEBINTERFACE ]
        $OS4XRD_BIN
+
    then
        echo -n "os4xsqd... "
+
      if [ -e ${OS4X_WEBINTERFACE}/blank_install ]
        $OS4XSQD_BIN
+
      then
        echo -n "os4xdebugd... "
+
         # try to recover installation
        $OS4XDEBUGD_BIN
+
         if [ -e /sd_card/os4x.*.sql.gz ]
        if [ $OS4X_ENTERPRISE -gt 0 ]
 
        then
 
                echo -n "os4xclientd... "
 
                $OS4XCLIENTD_BIN
 
        fi
 
        echo "OK"
 
        ;;
 
    stop)
 
        echo -n "Shutting down OS4X "
 
        echo -n "os4xrd... "
 
        killall os4xrd 1> /dev/null 2>1
 
        echo -n "os4xsqd... "
 
         killall os4xsqd 1> /dev/null 2>1
 
        echo -n "os4xdebugd... "
 
        killall os4xdebugd 1> /dev/null 2>1
 
         if [ $OS4X_ENTERPRISE -gt 0 ]
 
        then
 
                echo -n "os4xclientd... "
 
                killall os4xclientd 1> /dev/null 2>1
 
        fi
 
        echo OK
 
        ;;
 
    status)
 
        echo -n "Checking for service OS4X "
 
        pidof os4xrd > /dev/null
 
        if [ $? -ne 0 ]
 
        then
 
          echo -n "... os4xrd is not running"
 
        else
 
          echo -n "... os4xrd OK "
 
        fi
 
        pidof os4xsqd > /dev/null
 
        if [ $? -ne 0 ]
 
 
         then
 
         then
           echo -n "... os4xsqd is not running"
+
           # backupfile exists, restore complete system
 +
          /opt/os4x/scripts/systemrestore_from_sd.sh && rm -f ${OS4X_WEBINTERFACE}/blank_install
 
         else
 
         else
           echo -n "... os4xsqd OK "
+
           echo "not starting OS4X: blank installation, please configure OS4X"
 +
          exit
 
         fi
 
         fi
        pidof os4xdebugd > /dev/null
+
      fi
        if [ $? -ne 0 ]
+
    fi
        then
+
 
          echo "... os4xdebugd is not running"
+
echo -n "Starting OS4X "
        else
+
if [ ${LIC_OS4XCORE} -eq 1 ]
          echo -n "... os4xdebugd OK "
+
then
        fi
+
echo -n "os4xrd... "
        if [ $OS4X_ENTERPRISE -gt 0 ]
+
$OS4XRD_BIN
        then
+
                if [ $? -eq 0 ]; then
                 pidof os4xclientd > /dev/null
+
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Receive daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                 if [ $? -ne 0 ]
+
                else
                then
+
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Receive daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                  echo "... os4xclientd is not running"
+
                fi
 +
echo -n "os4xsqd... "
 +
$OS4XSQD_BIN
 +
                if [ $? -eq 0 ]; then
 +
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Send queue daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
 +
                else
 +
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Send queue daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
 +
                fi
 +
fi
 +
if [ ${LIC_OS4XENTERPRISE_LITE} -eq 1 -o ${LIC_OS4XENTERPRISE_ALL} -eq 1 ]
 +
then
 +
echo -n "os4xclientd... "
 +
$OS4XCLIENTD_BIN
 +
                if [ $? -eq 0 ]; then
 +
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Client daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
 +
                else
 +
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Client daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
 +
                fi
 +
fi
 +
if [ ${LIC_OS4X_AS2} -eq 1 -a -e $OS4XAS2_BIN ]
 +
then
 +
echo -n "os4x_as2... "
 +
$OS4XAS2_BIN
 +
                if [ $? -eq 0 ]; then
 +
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'AS2 daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
 +
                 else
 +
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of AS2 daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
 +
                 fi
 +
fi
 +
if [ ${LIC_OS4X_PORTAL} -eq 1 -a -e $OS4XPORTAL_BIN ]
 +
then
 +
echo -n "os4xportald... "
 +
$OS4XPORTAL_BIN
 +
                if [ $? -eq 0 ]; then
 +
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Portal daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
 
                 else
 
                 else
                  echo "... os4xclientd OK "
+
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Portal daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
 
                 fi
 
                 fi
        else
+
fi
                echo ""
+
echo "OK"
        fi
+
;;
         ;;
+
    status)
 +
echo -n "Checking for service OS4X "
 +
if [ ${LIC_OS4XCORE} -eq 1 ]
 +
then
 +
pidof os4xrd >/dev/null 2>&1 
 +
if [ $? -ne 0 ]
 +
then
 +
  echo -n "... os4xrd is not running"
 +
else
 +
  echo -n "... os4xrd OK "
 +
fi
 +
pidof os4xsqd >/dev/null 2>&1 
 +
if [ $? -ne 0 ]
 +
    then
 +
  echo -n "... os4xsqd is not running"
 +
else
 +
  echo -n "... os4xsqd OK "
 +
    fi
 +
fi
 +
if [ ${LIC_OS4XENTERPRISE_LITE} -eq 1 -o ${LIC_OS4XENTERPRISE_ALL} -eq 1 ]
 +
then
 +
pidof os4xclientd >/dev/null 2>&1 
 +
    if [ $? -ne 0 ]
 +
    then
 +
    echo -n "... os4xclientd is not running"
 +
  else
 +
    echo -n "... os4xclientd OK "
 +
    fi
 +
  fi
 +
if [ ${LIC_OS4X_AS2} -eq 1 ]
 +
then
 +
pidof os4x_as2 >/dev/null 2>&1 
 +
if [ $? -ne 0 ]
 +
    then
 +
  echo -n "... os4x_as2 is not running"
 +
else
 +
  echo -n "... os4x_as2 OK "
 +
    fi
 +
fi
 +
if [ ${LIC_OS4X_PORTAL} -eq 1 ]
 +
then
 +
pidof os4xportald >/dev/null 2>&1 
 +
if [ $? -ne 0 ]
 +
    then
 +
  echo -n "... os4xportald is not running"
 +
else
 +
  echo -n "... os4xportald OK "
 +
    fi
 +
fi
 +
pidof os4xdebugd > /dev/null 2>&1
 +
if [ $? -ne 0 ]
 +
    then
 +
  echo "... os4xdebugd is not running"
 +
else
 +
  echo "... os4xdebugd OK"
 +
    fi
 +
;;
 +
    update)
 +
echo -n "Initiating OS4X update... "
 +
         $OS4XPOLL_BIN OS4X-Update
 +
echo "OK"
 +
;;
 +
restart)
 +
$0 stop ; sleep 2; $0 start
 +
;;
 
     *)
 
     *)
        echo "Usage: $0 {start|stop|status}"
+
echo "Usage: $0 {start|stop|restart|status|update}"
        exit 1
+
exit 1
        ;;
+
;;
 
esac
 
esac
 
# rc_exit
 
# rc_exit
 +
 
</pre>
 
</pre>

Latest revision as of 14:22, 10 October 2024

For runlevel scripts, this script can be used as a basis for further needs. It works good in an OS4XBox environment. Actual versions of init-scripts can be found here: https://www.os4x.com/downloads/

#!/bin/bash 
### BEGIN INIT INFO
# Provides: os4x
# Required-Start: mysql
# Required-Stop: mysql
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Description: Start or stop OS4X
### END INIT INFO


#
# OS4X
#

# Check for existence of needed config file and read it
OS4X_CONFIG=/etc/os4x.conf
test -r $OS4X_CONFIG || { echo "$OS4X_CONFIG not existing";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }  

# first, source the config file for database parameters
. $OS4X_CONFIG

# calculate MySQL client parameters
MYSQLCLIENT_PARAMS="--silent --user=$DB_USER --password=$DB_PASS --host=$DB_HOST $DB_NAME"
if [ ! -z "$DB_TLS_VERSION" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --tls-version=$DB_TLS_VERSION"
fi
if [ ! -z "$DB_TLS_CIPHER" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-cipher=$DB_TLS_CIPHER"
fi
if [ ! -z "$DB_TLS_KEY" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-key=$DB_TLS_KEY"
fi
if [ ! -z "$DB_TLS_CERT" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-cert=$DB_TLS_CERT"
fi
if [ ! -z "$DB_TLS_CA" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-ca=$DB_TLS_CA"
fi
if [ ! -z "$DB_TLS_CAPATH" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-capath=$DB_TLS_CAPATH"
fi

# retrieve directories and settings
OS4X_BINDIR=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='bin_directory'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`
OS4X_TMPDIR=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='tmp_directory'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`
OS4X_IS_PORTAL=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='enterprise_is_this_portal_server'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`

if [ -z "${OS4X_IS_PORTAL}" ]
then
	OS4X_IS_PORTAL=0
fi

# check license
if [ -e ${OS4X_BINDIR}/license.key ]
then
	LICFUNCTIONS=`${OS4X_BINDIR}/os4x_licinfo -A 2>/dev/null`
else
	LICFUNCTIONS=0
fi

if [ $? -ne 0 ]
then
	LICFUNCTIONS=0
fi

(((${LICFUNCTIONS}&1)==1)) && LIC_OS4XCORE=1 || LIC_OS4XCORE=0
(((${LICFUNCTIONS}&2)==2)) && LIC_OS4XENTERPRISE_LITE=1 || LIC_OS4XENTERPRISE_LITE=0
(((${LICFUNCTIONS}&4)==4)) && LIC_OS4XENTERPRISE_ALL=1 || LIC_OS4XENTERPRISE_ALL=0
(((${LICFUNCTIONS}&8)==8)) && LIC_OS4X_AS2=1 || LIC_OS4X_AS2=0
(((${LICFUNCTIONS}&16)==16)) && LIC_OS4X_PORTAL=1 || LIC_OS4X_PORTAL=0

if [ "${OS4X_IS_PORTAL}" -eq 1 -a ${LIC_OS4X_PORTAL} -eq 1 ]
then
	LIC_OS4X_PORTAL=0
	LIC_OS4XENTERPRISE_ALL=1
fi

OS4X_WEBINTERFACE=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='webinterface_path'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
OS4XRD_BIN=$OS4X_BINDIR/os4xrd
OS4XSQD_BIN=$OS4X_BINDIR/os4xsqd
OS4XCLIENTD_BIN=$OS4X_BINDIR/os4xclientd
OS4XAS2_BIN=$OS4X_BINDIR/os4x_as2
OS4XPOLL_BIN=$OS4X_BINDIR/os4xpoll
OS4XPORTAL_BIN=$OS4X_BINDIR/os4xportald

# sanity checks for daemon existance
if [ ! -e "$OS4XCLIENTD_BIN" ]
then
	LIC_OS4XENTERPRISE_ALL=0
	LIC_OS4XENTERPRISE_LITE=0
fi
if [ ! -e "$OS4XPORTAL_BIN" ]
then
	LIC_OS4X_PORTAL=0
fi
if [ ! -e "$OS4XAS2_BIN" ]
then
	LIC_OS4X_AS2=0
fi

function killDaemon
{
	if [ -f ${OS4X_TMPDIR}/${1}.pid ]
	then
		kill `cat ${OS4X_TMPDIR}/${1}.pid`
	else
		killall ${1} >/dev/null 2>&1
	fi
}

case "$1" in
    stop)
	echo -n "Shutting down OS4X "
	pidof os4xrd >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4xrd... "
		killDaemon os4xrd
	fi
	pidof os4xsqd >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4xsqd... "
		killDaemon os4xsqd
	fi
	pidof os4xdebugd >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4xdebugd... "
		killDaemon os4xdebugd
	fi
	pidof os4xclientd >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4xclientd... "
		killDaemon os4xclientd
	fi
	pidof os4x_as2_master >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4x_as2... "
		killDaemon os4x_as2
	fi
	pidof os4xportald >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4xportald... "
		killDaemon os4xportald
	fi
	echo OK
	;;
    start)
    # pre-check lockfile
    if [ -d $OS4X_WEBINTERFACE ]
    then
      if [ -e ${OS4X_WEBINTERFACE}/blank_install ]
      then
        # try to recover installation
        if [ -e /sd_card/os4x.*.sql.gz ]
        then
          # backupfile exists, restore complete system
          /opt/os4x/scripts/systemrestore_from_sd.sh && rm -f ${OS4X_WEBINTERFACE}/blank_install
        else
          echo "not starting OS4X: blank installation, please configure OS4X"
          exit
        fi
      fi
    fi

	echo -n "Starting OS4X "
	if [ ${LIC_OS4XCORE} -eq 1 ]
	then
	 	echo -n "os4xrd... "
		$OS4XRD_BIN
                if [ $? -eq 0 ]; then
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Receive daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                else
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Receive daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                fi
		echo -n "os4xsqd... "
		$OS4XSQD_BIN
                if [ $? -eq 0 ]; then
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Send queue daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                else
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Send queue daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                fi
	fi
	if [ ${LIC_OS4XENTERPRISE_LITE} -eq 1 -o ${LIC_OS4XENTERPRISE_ALL} -eq 1 ]
	then
		echo -n "os4xclientd... "
		$OS4XCLIENTD_BIN
                if [ $? -eq 0 ]; then
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Client daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                else
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Client daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                fi
	fi
	if [ ${LIC_OS4X_AS2} -eq 1 -a -e $OS4XAS2_BIN ]
	then
	 	echo -n "os4x_as2... "
		$OS4XAS2_BIN
                if [ $? -eq 0 ]; then
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'AS2 daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                else
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of AS2 daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                fi
	fi
	if [ ${LIC_OS4X_PORTAL} -eq 1 -a -e $OS4XPORTAL_BIN ]
	then
	 	echo -n "os4xportald... "
		$OS4XPORTAL_BIN
                if [ $? -eq 0 ]; then
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Portal daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                else
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Portal daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                fi
	fi
	echo "OK"
	;;
    status)
	echo -n "Checking for service OS4X "
	if [ ${LIC_OS4XCORE} -eq 1 ]
	then
		pidof os4xrd >/dev/null 2>&1  
		if [ $? -ne 0 ]
		then
		  echo -n "... os4xrd is not running"
		else
		  echo -n "... os4xrd OK "
		fi 
		pidof os4xsqd >/dev/null 2>&1  
		if [ $? -ne 0 ]
	    then
		  echo -n "... os4xsqd is not running"
		else
		  echo -n "... os4xsqd OK "
	    fi
	fi
	if [ ${LIC_OS4XENTERPRISE_LITE} -eq 1 -o ${LIC_OS4XENTERPRISE_ALL} -eq 1 ]
	then
		pidof os4xclientd >/dev/null 2>&1  
    	if [ $? -ne 0 ]
    	then
   		   echo -n "... os4xclientd is not running"
   		else
   		   echo -n "... os4xclientd OK "
    	fi
   	fi
	if [ ${LIC_OS4X_AS2} -eq 1 ]
	then
		pidof os4x_as2 >/dev/null 2>&1  
		if [ $? -ne 0 ]
	    then
		  echo -n "... os4x_as2 is not running"
		else
		  echo -n "... os4x_as2 OK "
	    fi
	fi
	if [ ${LIC_OS4X_PORTAL} -eq 1 ]
	then
		pidof os4xportald >/dev/null 2>&1  
		if [ $? -ne 0 ]
	    then
		  echo -n "... os4xportald is not running"
		else
		  echo -n "... os4xportald OK "
	    fi
	fi
	pidof os4xdebugd > /dev/null 2>&1 
	if [ $? -ne 0 ]
    then
	  echo "... os4xdebugd is not running"
	else
	  echo "... os4xdebugd OK"
    fi
	;;
    update)
	echo -n "Initiating OS4X update... "
        $OS4XPOLL_BIN OS4X-Update
	echo "OK"
	;;
	restart)
	$0 stop ; sleep 2; $0 start
	;;
    *)
	echo "Usage: $0 {start|stop|restart|status|update}"
	exit 1
	;;
esac
# rc_exit