#!/bin/bash
# console-config/src/lib-scripts/serial-kvh-dsp1500.sh
#
#  Copyright: ©2014, Güralp Systems Ltd.
#  Author: Kelly Dunlop <kdunlop@guralp.com>
#  License: GPLv3
#
# Setup the parameters for a serial port configured for a KVH DSP1500 gyroscope.



# display_kvhdsp1500_station_name
# station_name (default hostname shortened to 5 chars and capitalised)
# $1 is the title text to display
# $2 is the current value from the config file
# $3 is the default value
display_kvhdsp1500_station_name() {
    local default

    if [ -z "$2" ]
    then
        default="$3"
    else
        default="$2"
    fi
    display_inputbox "$1" "SEED station name. Must be specified. 1 to 5 characters (A-Z, 0-9)." \
           0 0 "$default"
}



# display_kvhdsp1500_network_name
# network_name (default "")
# $1 is the title text to display
# $2 is the current value from the config file
# $3 is the default value
display_kvhdsp1500_network_name() {
    local default

    if [ -z "$2" ]
    then
        default="$3"
    else
        default="$2"
    fi
    display_inputbox "$1" "SEED network name. Optional. 1 or 2 characters (A-Z, 0-9)." \
           0 0 "$default"
}



# display_kvhdsp1500_location_name
# location_name (default "")
# $1 is the title text to display
# $2 is the current value from the config file
# $3 is the default value
display_kvhdsp1500_location_name() {
    local default

    if [ -z "$2" ]
    then
        default="$3"
    else
        default="$2"
    fi
    display_inputbox "$1" "SEED location name. Optional. 1 or 2 characters (A-Z, 0-9)." \
           0 0 "$default"
}



# display_kvhdsp1500_upside_down
# upside_down (default unset => false)
# $1 is the title text to display
# $2 is the current value from the config file
display_kvhdsp1500_upside_down() {
    if [ -z "$2" -o "$2" = "false" ]
    then
        dialog --title "$1" \
               --defaultno \
               --yesno "Select yes here if the equipment is mounted upside down" \
               0 0 2> "${DIALOG_RESULTFILE}"
    else
        dialog --title "$1" \
               --yesno "Select yes here if the equipment is mounted upside down" \
               0 0 2> "${DIALOG_RESULTFILE}"
    fi
    return $?
}



# Go through the list of things that the user can change in the KVH DSP1500 digital gyro config
# file.
#   $1 - portname
# If something is Cancelled or there is an error then this can exit.
serial_kvhdsp1500_parameters() {
    local port=$1
    local titletext

    KVH_CFG_FILE="${SERIAL_CFDIR}/kvh-dsp1500/${port}"

    # Set default values
    DEF_KVH_STATION_NAME="$(hostname | cut -b1-5 | tr a-z A-Z)"
    DEF_KVH_NETWORK_NAME=""
    DEF_KVH_LOCATION_NAME=""

    # Get current values or set defaults if file doesn't already exist
    if [ -r ${KVH_CFG_FILE} ]
    then
        # get the current values for each item
        KVH_STATION_NAME="`cfget ${KVH_CFG_FILE} station_name`"
        KVH_NETWORK_NAME="`cfget ${KVH_CFG_FILE} network_name`"
        KVH_LOCATION_NAME="`cfget ${KVH_CFG_FILE} location_name`"
        KVH_UPSIDE_DOWN="`cfget ${KVH_CFG_FILE} upside_down`"
        KVH_GDI_SOCKET="`cfget ${KVH_CFG_FILE} gdi_socket`"
    else
        # set empty values for each item
        KVH_STATION_NAME=""
        KVH_NETWORK_NAME=""
        KVH_LOCATION_NAME=""
        KVH_UPSIDE_DOWN=""
        KVH_GDI_SOCKET=""
    fi

    # Now go through the values giving user a choice
    titletext="${port} Digital Gyro config"

    # station_name (default hostname shortened to 5 chars and capitalised)
    display_kvhdsp1500_station_name "${titletext}" "${KVH_STATION_NAME}" "${DEF_KVH_STATION_NAME}"
    NEW_KVH_STATION_NAME="`cat ${DIALOG_RESULTFILE}`"

    # network_name (default "")
    display_kvhdsp1500_network_name "${titletext}" "${KVH_NETWORK_NAME}" "${DEF_KVH_NETWORK_NAME}"
    NEW_KVH_NETWORK_NAME="`cat ${DIALOG_RESULTFILE}`"

    # location_name (default "")
    display_kvhdsp1500_location_name "${titletext}" "${KVH_LOCATION_NAME}" "${DEF_KVH_LOCATION_NAME}"
    NEW_KVH_LOCATION_NAME="`cat ${DIALOG_RESULTFILE}`"

    # upside_down (default unset => false)
    display_kvhdsp1500_upside_down "${titletext}" "${KVH_UPSIDE_DOWN}"
    RESULT=$?
    if [ ${RESULT} -eq 0 ]    # yes selected
    then
        NEW_KVH_UPSIDE_DOWN="true"
    elif [ ${RESULT} -eq 1 ]    # no selected
    then
        NEW_KVH_UPSIDE_DOWN="false"
    fi

    # gdi_socket default /var/run/gdi-base.default.source
    display_gdi_socket "${titletext}" ${KVH_GDI_SOCKET}
    newgdiinst="`cat "${DIALOG_RESULTFILE}"`"
    gdi_get_socket_path $newgdiinst
    NEW_KVH_GDI_SOCKET="${GDI_SOCKET_PATH}"

    cconf_create_config_file ${KVH_CFG_FILE}

    # Now we need to make the updates that have been done here before returning
    cfset ${KVH_CFG_FILE} station_name="${NEW_KVH_STATION_NAME}"
    cfset ${KVH_CFG_FILE} network_name="${NEW_KVH_NETWORK_NAME}"
    cfset ${KVH_CFG_FILE} location_name="${NEW_KVH_LOCATION_NAME}"
    cfset ${KVH_CFG_FILE} upside_down="${NEW_KVH_UPSIDE_DOWN}"
    cfset ${KVH_CFG_FILE} gdi_socket="${NEW_KVH_GDI_SOCKET}"

    # Now we've finished making changes reset the permission of the config file
    cconf_set_perms_and_group "${KVH_CFG_FILE}" 664 "conf"
}



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