#!/bin/bash
# console-config/src/lib-scripts/common.sh
#
#  Copyright: ©2014, Güralp Systems Ltd.
#  Author: Laurence Withers <lwithers@guralp.com>
#  Author: Kelly Dunlop <kdunlop@guralp.com>
#  License: GPLv3
#


# This is a common file that can be used in different pieces of code.

# Global variables
SERIAL_CFDIR="/etc/conf.d/serial"

declare -a FUNC_LIST=(None 'The port is ignored' \
                      Terminal 'Port used for user access' \
                      'PPP out' 'PPP network connection' \
                      'GCF in' 'Inbound GCF data gathering' \
                      'GCF out' 'Outbound GCF data transmission' \
                      'TCP serial' 'Serial data to TCP link converter' \
                      'MS modbus' 'Modbus interface' \
                      'Modem in' 'Modem answering service' \
                      'Serial Recorder' 'Record info from serial port' \
                      'Fast CD24 in' 'Low latency data acquisition from CD24' \
                      'RTD out' 'USGS RTD format output' \
                      'KVH DSP1500 Digital Gyro' 'Digital gyroscope input' );



# serial_build_port_menu()
#  Builds the array MENU as a menu for dialog. It will preselect any port
#  configured for the function passed in $1, as well as setting the variable
#  MENU_EXTRA_ARGS.
serial_build_port_menu() {
    local TEST_FUNCTION="$1"
    local CF NAME BASENAME FUNCTION

    MENU=()
    MENU_EXTRA_ARGS=""

    # The value for IFS is not set in the standard root login in the current version
    # of bash which means that the default value will be used.  So there is no point
    # trying to save it to put it back after we've done.  I shall just unset IFS
    # afterwards so the default value gets picked up.
    IFS=:
    while read -r BASENAME NAME FUNCTION BAUD
    do
        # If this isn't set then there is something missing from the given line
        if [ -z "${BAUD}" ]
        then
            echo Missing item in filename ${BASENAME}.cf
            sleep 1
        fi

        MENU+=("${BASENAME}")
        MENU+=("${NAME} (currently: ${FUNCTION} : ${BAUD})")
        if [ "${FUNCTION}" = "${TEST_FUNCTION}" ]
        then
            MENU_EXTRA_ARGS="--default-item ${BASENAME}"
        fi
    done < <(display-port-info)
    unset IFS
}



# serial_select_baud()
#  Chooses a baud rate. Returns 0 if accepted or 1 if cancelled. Baud rate is
#  set in SERIAL_BAUD.
#  $1 is the text to use in the menu
#  $2 is the default value to start with.
serial_select_baud() {
    local menu_text default_item

    if [ -z "$1" ]
    then
        menu_text="Select baud rate."
    else
        menu_text="$1"
    fi

    if [ -z "$2" ]
    then
        default_item="1200"
    else
        default_item="$2"
    fi

    dialog --title "Baud rate" \
        --default-item "${default_item}" \
        --menu "${menu_text}" \
        0 ${DIALOG_DEFAULT_W} 0 \
        1200 "1200 baud" \
        4800 "4800 baud" \
        9600 "9600 baud" \
        19200 "19200 baud" \
        38400 "38400 baud" \
        57600 "57600 baud" \
        115200 "115200 baud" \
        230400 "230400 baud" \
        2> "${DIALOG_RESULTFILE}"
    [ $? -eq 0 ] || return 1

    SERIAL_BAUD="`cat ${DIALOG_RESULTFILE}`"
}



# Display a dialog box with yes/no buttons.
#   $1 is the text to display as the title to the box.
#   $2 is the text to display as the question.
#   $3 is the height of the box - may be blank
#   $4 is the width of the box - may be blank
#   $5 is any extra parameters (ie the default setting) - may be blank
#   note the default setting should be --defaultno or ""
# Returns the dialog return value.
display_yesno() {
    if [ -z $3 ]
    then
        height=0
    else
        height=$3
    fi
    if [ -z $4 ]
    then
        width=0
    else
        width=$4
    fi

    dialog --title "$1" $5 --yesno "$2" $height $width 2> "${DIALOG_RESULTFILE}"
    return $?
}



# Display a dialog box with a menu list.
#   $1 is the text to display as the title to the box.
#   $2 is the text to display as the question.
#   $3 is the height of the box
#   $4 is the height of the menu
#   $5 is the default setting
#   The list of items is in the array MENU.
# Returns the dialog return value.
display_menu() {
    local titletext="$1"
    local menutext="$2"
    local height="$3"
    local menuheight="$4"
    local default="$5"

    if [ -z "$default" ]
    then
        # No default item
        dialog --title "$titletext" \
               --menu "$menutext" \
               $height ${DIALOG_DEFAULT_W} $menuheight \
               "${MENU[@]}" 2> "${DIALOG_RESULTFILE}"
    else
        dialog --title "$titletext" \
               --default-item "$default" \
               --menu "$menutext" \
               $height ${DIALOG_DEFAULT_W} $menuheight \
               "${MENU[@]}" 2> "${DIALOG_RESULTFILE}"
    fi
    if [ $? -ne 0 ]
    then
        clear
        echo "Cancelled"
        exit 1
    fi
}



# Display a dialog box for an input value.
#   $1 is the text to display as the title to the box.
#   $2 is the text to display as the question.
#   $3 is the height of the box - may be blank
#   $4 is the width of the box - may be blank
#   $5 is the default setting - may be blank
display_inputbox() {
    local height width

    if [ -z "$3" ]
    then
        height=0
    else
        height=$3
    fi
    if [ -z "$4" ]
    then
        width=0
    else
        width=$4
    fi

    dialog --title "$1" \
           --inputbox "$2" \
           $height $width "$5" 2> "${DIALOG_RESULTFILE}"
    if [ $? -ne 0 ]
    then
        clear
        echo "Cancelled"
        exit 1
    fi
}



# display_gdi_socket
# $1 is the title text to display
# $2 is the current value from the config file
# TODO make source/sink configurable ?
display_gdi_socket() {
    gdi_list_sockets source $2
    if [ -n "$GDI_INSTANCE" ]
    then
        gdiinst=$GDI_INSTANCE
    else
        gdiinst=default
    fi

    # MENU is setup by gdi_list_sockets
    if [ -z "${MENU}" ]
    then
        clear
        echo "No GDI multiplexors found."
        exit 1
    fi

    dialog --title "$1" \
           --default-item $gdiinst \
           --menu "GDI multiplexor" \
           0 ${DIALOG_DEFAULT_W} 0 \
           "${MENU[@]}" \
           2> "${DIALOG_RESULTFILE}"
    if [ $? -ne 0 ]
    then
        clear
        echo "Cancelled"
        exit 1
    fi
}



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