#!/sbin/svc
# /etc/init.d/timing
# 
#  Copyright: ©2013–2015, Güralp Systems Ltd.
#  Author: Laurence Withers <lwithers@guralp.com>
#  License: GPLv3
#
# Control for timing processes. This is a single global "switch" that allows us
# to start, stop or restart all processes connected with timing on the Linux
# system. It is responsible for determining which services to start, stop or
# restart as appropriate.
#
# Note that this is written to work even if the config file is updated between
# start and stop, and it does not use the service monitor's dependency
# mechanism, avoiding bugs and allowing a user to individually start/stop/
# restart a more specific service such as ntpd.
#

MYLISTING="System timing services"
CONTROL_GROUP="conf"
MYCFG="/etc/conf.d/timing.local"
MYVERSION="2.0"



stop_if_running() {
    local NAME="$1"
    [ -r "${VARDIR}/${NAME}.started" ] && svc "${NAME}" stop >& /dev/null
    return 0
}



start_or_restart() {
    local NAME="$1"
    if [ -r "${VARDIR}/${NAME}.started" ]
    then
        svc "${NAME}" restart >& /dev/null
    else
        svc "${NAME}" start >& /dev/null
    fi
    return 0
}



start() {
    local start_ntp="`cfget "${MYCFG}" "start_ntp"`"
    local start_ntpshmgsl="`cfget "${MYCFG}" "start_ntpshmgsl"`"
    local start_gpsstatusd="`cfget "${MYCFG}" "start_gpsstatusd"`"
    local start_ptp="`cfget "${MYCFG}" "start_ptp"`"
    [ -z "${start_ptp}" ] && start_ptp="false"

    if gcs_truefalse "${start_ntp}"
    then
        start_or_restart ntpd
        start_or_restart ntpstatusd
    fi

    if gcs_truefalse "${start_ntpshmgsl}"
    then
        start_or_restart ntpshmgsl
    fi

    if gcs_truefalse "${start_gpsstatusd}"
    then
        start_or_restart gpsstatusd
    fi

    if gcs_truefalse "${start_ptp}"
    then
        start_or_restart ptp-client
        start_or_restart ptp-phc2sys
    fi
}

stop() {
    stop_if_running ntpd
    stop_if_running ntpstatusd
    stop_if_running ntpshmgsl
    stop_if_running gpsstatusd
}

restart() {
    local start_ntp="`cfget "${MYCFG}" "start_ntp"`"
    local start_ntpshmgsl="`cfget "${MYCFG}" "start_ntpshmgsl"`"
    local start_gpsstatusd="`cfget "${MYCFG}" "start_gpsstatusd"`"
    local start_ptp="`cfget "${MYCFG}" "start_ptp"`"
    [ -z "${start_ptp}" ] && start_ptp="false"

    if gcs_truefalse "${start_ntp}"
    then
        start_or_restart ntpd
        start_or_restart ntpstatusd
    else
        stop_if_running ntpd
        stop_if_running ntpstatusd
    fi

    if gcs_truefalse "${start_ntpshmgsl}"
    then
        start_or_restart ntpshmgsl
    else
        stop_if_running ntpshmgsl
    fi

    if gcs_truefalse "${start_gpsstatusd}"
    then
        start_or_restart gpsstatusd
    else
        stop_if_running gpsstatusd
    fi

    if gcs_truefalse "${start_ptp}"
    then
        start_or_restart ptp-client
        start_or_restart ptp-phc2sys
    else
        stop_if_running ptp-client
        stop_if_running ptp-phc2sys
    fi
}



# vim: ts=4:sw=4:expandtab:syntax=sh
