#!/bin/sh
# alerts        Start and Stops Alerts Services
#
# Authors:      Matthew Kruer <matthew.kruer@ericsson.com>
#
# description:  Ericsson / SA Media Alert Server Control Start Up Script
# processname:  org.openorb.ins.Server (java)
# processname:  com.n2bb.SSModule.SettingsServer (java)
# processname:  com.n2bb.SSModule.AlertServer (java)
# config:       NONE
# pidfile:      NONE

### BEGIN INIT INFO
# Should-Start: $network
# Provides: alerts
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: WatchPoint Alerts Server
### END INIT INFO

# Checks to see if the license key is installed
licensekey_check() 
{
    if [[ ! -f "/opt/tandbergtv/cms/conf/workflow/licensekey.lic" ]] ; then
        printf "  CMS license key not found; Aborting start up of Alerts. \n"
        exit 1
    fi
}

nameserver() 
{
    pid=$(ps axww | grep "[o]rg.openorb.ins.Server" | sed -e 's/^\s*//' | cut -d' ' -f1)
    if [[ -z "${pid}" ]] && [[ "start" = $1 ]] ; then
        printf "  Starting Name Service"
        sleep_dots 0
        /usr/local/n2bb/alerts/scripts/NameService_OpenORB $1 >/dev/null 2>&1
    elif [[ -n "${pid}" ]] && [[ "stop" = $1 ]] ; then 
        printf "  Stopping Name Service"
        /usr/local/n2bb/alerts/scripts/NameService_OpenORB $1 >/dev/null 2>&1
        sleep_dots 0
    elif [[ -n "${pid}" ]] && [[ "status" = $1 ]]; then
        printf "  Name Service (pid "$pid") is running \n"
    elif [[ -z "${pid}" ]] && [[ "status" = $1 ]]; then
        printf "  Name Service is stopped \n"
    else
        exit 1
    fi
}

settingsserver() 
{
    pid=$(ps axww | grep "[c]om.n2bb.SSModule.SettingsServer" | sed -e 's/^\s*//' | cut -d' ' -f1)
    if [[ -z "${pid}" ]] && [[ "start" = $1 ]] ; then
        printf "  Starting Settings Server"
        /usr/local/n2bb/SettingsServer/scripts/SettingsServer $1 >/dev/null 2>&1
        sleep_dots 0
    elif [[ -n "${pid}" ]] && [[ "stop" = $1 ]] ; then 
        printf "  Stopping Settings Server"
        /usr/local/n2bb/SettingsServer/scripts/SettingsServer $1 >/dev/null 2>&1
        sleep_dots 0
    elif [[ -n "${pid}" ]] && [[ "status" = $1 ]]; then
        printf "  Settings Server (pid "$pid") is running \n"
    elif [[ -z "${pid}" ]] && [[ "status" = $1 ]]; then
        printf "  Settings Server is stopped \n"
    else
        exit 1
    fi
}

alerts() 
{
    pid=$(ps axww | grep "[c]om.n2bb.AlertsModule.AlertServer" | sed -e 's/^\s*//' | cut -d' ' -f1)
    if [[ -z "${pid}" ]] && [[ "start" = $1 ]] ; then
        printf "  Starting Alerts"
        /usr/local/n2bb/alerts/scripts/Alerts $1 >/dev/null 2>&1
        check_alerts $1
    elif [[ -n "${pid}" ]] && [[ "stop" = $1 ]] ; then 
        printf "  Stopping Alerts"
        /usr/local/n2bb/alerts/scripts/Alerts $1 >/dev/null 2>&1
        sleep_dots 3
    elif [[ -n "${pid}" ]] && [[ "status" = $1 ]]; then
        printf "  Alerts (pid "$pid") is running \n"
    elif [[ -z "${pid}" ]] && [[ "status" = $1 ]]; then
        printf "  Alerts is stopped\n"
    else
        exit 1
    fi
}

check_alerts() 
{
    count=0
    delay=10
    timeout=100
    while [ $count -le "$timeout" ] ; do
        for (( start = 1; start <= $delay; start++ )); do 
            printf "."
            sleep 1
        done
        count=$(( $count + $delay ))
        pid=$(ps axww | grep "[c]om.n2bb.AlertsModule.AlertServer" | sed -e 's/^\s*//' | cut -d' ' -f1)
        if [[ -z "$pid" ]] && [[ $count -ge $timeout ]] ; then 
            printf " [FAILED]\n  Unable to Start Alerts after $count Seconds. Verify that the database is available and restart the Alerts Service. \n" 
            exit 1
        elif [[ -z "$pid" ]] || [ $count -eq 0 ]  ; then 
            /usr/local/n2bb/alerts/scripts/Alerts $1 >/dev/null 2>&1
        else 
            printf "\n  Done [Start Up Duration $count Seconds] \n"
            count=$(( $timeout + $delay ))
        fi
    done
}

sleep_dots() 
{
    for (( start = 1; start <= $1; start++ )); do
        printf "."
        sleep 1
    done
    printf "\n"
}

case "$1" in
    start)
        printf "Starting Alerts Services: \n"
        licensekey_check
        nameserver $1
        settingsserver $1
        alerts $1
        $0 status
        ;;
    stop)
        printf "Stopping Alerts Services: \n"
        alerts $1
        settingsserver $1
        nameserver $1
        $0 status
        ;;
    status)
        printf " Status of Alerts Services: \n"
        nameserver $1
        settingsserver $1
        alerts $1
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        printf "Usage: $0 ( start | stop | status | restart ) \n"
        exit 1
        ;;
esac
exit 0

