#!/bin/bash
# console-config/src/sbin-scripts/timing-setup
#
#  Copyright: ©2013–2014, Güralp Systems Ltd.
#  Author: Laurence Withers <lwithers@guralp.com>
#  License: GPLv3
#
# This script allows the user to configure system timing. It supports the
# following scenarios:
#  - CD24/DM24 connected to GPS (mode: "dm24_gps")
#  - NTP via network, possibly output to CD24/DM24 (mode: "ntp")
#  - NTP synced to GPS, acts as NTP server (mode: "direct_gps")
#  - time-of-day input string connected to EAM, PPS connected to DM24, EAM
#    synthesises NMEA for DM24 (mode: "dm24_xlate")
#
# The CMG-DAS varies in that "direct_gps" is also used as the sample clock, and
# it will support PTP as well.
#



# Important global variables
TIMING_CONFIG="/etc/conf.d/timing.local"
NTP_CONFIG="/etc/ntp.conf.local"



# The various routines are split into several modules under /usr/lib, and these
# are all sourced into this file. This aids programming and maintenance.
for LIB in /usr/lib/console-config/timing-*.sh
do
    . "${LIB}"
done


. /usr/lib/console-config/common.sh



#
# Top-level configuration of system timing mode. Once a mode is chosen, calls
# a mode-specific setup script for further configuration.
#

# System capabilities
. "/etc/system_type"

case "${BUILD_MACHINE}" in
CMG-DCM-mk2x | CMG-DCM-mk4-eabi)
    AVAILABLE_MODES="dm24_gps ntp manual"
    EXTRA_TIMING_TEXT="The option dm24_gps is the normal configuration \
for a system with an EAM, one or more CD24 or DM24 digitisers or digital \
sensors, and a GPS receiver."

    if [ -r "/etc/conf.local/hw_config" ]
    then
        . "/etc/conf.local/hw_config"
        case "${HW_GPIO_SET}" in
        neptune | ASE-DCM-0026)
            AVAILABLE_MODES="${AVAILABLE_MODES} dm24_xlate"
            ;;
        esac
    fi
    ;;

CMG-NAM | CMG-NAM-mk2)
    AVAILABLE_MODES="ntp direct_gps dm24_gps manual"
    ;;

CMG-DAS)
    #AVAILABLE_MODES="direct_gps ntp ptp manual"
    AVAILABLE_MODES="direct_gps ntp manual"
    ;;
*)
    assert "Unknown build machine ${BUILD_MACHINE}"
    ;;
esac

# For testing and potentially work-arounds in-field
if [ -e "/etc/conf.d/serial/timestamp-in/.allow" ]
then
    AVAILABLE_MODES="${AVAILABLE_MODES} dm24_xlate"
fi

# Current user configuration
CURRENT_MODE="`cfget "${TIMING_CONFIG}" mode`"
[ -z "${CURRENT_MODE}" ] && CURRENT_MODE="${AVAILABLE_MODES/ *}"



# select_mode()
#  This displays a dialog box allowing the user to choose which of the
#  available timing modes they would like to configure. It exits the program
#  if the user cancels the dialog. Otherwise, it leaves the chosen mode in
#  DIALOG_RESULTFILE.
select_mode() {
    local MODE DEFAULT_ITEM

    MENU=()

    for MODE in ${AVAILABLE_MODES}
    do
        case "${MODE}" in
        ptp)
            DESCRIPTION="IEEE1588 precision time protocol"
            ;;
        ntp)
            DESCRIPTION="Network time protocol"
            ;;
        direct_gps)
            DESCRIPTION="Directly synchronised to GPS"
            ;;
        dm24_gps)
            DESCRIPTION="CD24/DM24 connected to GPS"
            ;;
        dm24_xlate)
            DESCRIPTION="Time-of-day string translated to NMEA"
            ;;
        manual)
            DESCRIPTION="Manually set"
            ;;
        esac

        MENU+=("${MODE}")
        MENU+=("${DESCRIPTION}")
        [ "${MODE}" = "${CURRENT_MODE}" ] && DEFAULT_ITEM="--default-item ${MODE}"
    done

    dialog ${DEFAULT_ITEM} \
        --title "Select timing mode" \
        --menu "Select the way this system will receive its timing reference. \
            ${EXTRA_TIMING_TEXT}" \
        0 "${DEFAULT_DIALOG_W}" 0 \
        "${MENU[@]}" \
        2> "${DIALOG_RESULTFILE}"
    if [ "$?" -ne 0 ]
    then
        clear
        echo "Cancelled."
        exit 1
    fi
}



# get_mode_settings()
#  A dispatcher which calls a mode-specific dialog. If that mode-specific dialog
#  is cancelled then it will return, and we will also return. Otherwise, it will
#  have called exit, and we don't regain control.
get_mode_settings() {
    local MODE="$1"

    case "${MODE}" in
    ptp)
        ptp_setup
        ;;
    ntp)
        ntp_setup
        ;;
    direct_gps)
        direct_gps_setup
        ;;
    dm24_gps)
        dm24_setup
        ;;
    dm24_xlate)
        dm24_xlate_setup
        ;;
    manual)
        manual_setup
        ;;
    *)
        assert "Unknown top-level mode \"${MODE}\""
        ;;
    esac

    return 0
}



# main loop
#  If the user cancels mode-specific settings then we go back to the mode
#  selection dialog.
while true
do
    select_mode
    get_mode_settings "`cat ${DIALOG_RESULTFILE}`"
done

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