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


# display_gcf_out_brp_acknak
# acknak_timeout (default 150)
# $1 is the title text to display
# $2 is the current value
# $3 is the default value
display_gcf_out_brp_acknak() {
    local default

    if [ -z "$2" ]
    then
        default=$3
    else
        default=$2
    fi

    display_inputbox "$1" "Time to wait for ACK/NAK before transmitting next block (in milliseconds)." \
        0 0 $default
}



# display_gcf_out_brp_allow_terminal
# allow_terminal (default true)
# $1 is the title text to display
# $2 is the current value
# Returns the return value from the dialog.
display_gcf_out_brp_allow_terminal() {
    if [ -z "$2" -o "$2" = "true" ]
    then
        dialog --title "$1" \
               --yesno "Allow access to configuration terminal through this link." \
               0 0 2> "${DIALOG_RESULTFILE}"
    else
        dialog --title "$1" \
               --defaultno \
               --yesno "Allow access to configuration terminal through this link." \
               0 0 2> "${DIALOG_RESULTFILE}"
   fi
   return $?
}



# display_gcf_out_brp_filter_type
# filter_type (default none)
# $1 is the title text to display
# $2 is the current value
# values are none, status, sample_rate, channel_name
display_gcf_out_brp_filter_type() {
    MENU=( "all" "Transmit all blocks" \
           "status" "Transmit only status blocks" \
           "sample_rate" "Discard blocks above sample rate" \
           "channel_name" "Transmit only blocks matching channel names" )

    display_menu "$1" "Select which types of block to transmit" 0 0 "$2"
}



# display_gcf_out_brp_sample_rate
# filter_sample_rate (default "")
# $1 is the title text to display
# $2 is the current value (may be "")
display_gcf_out_brp_sample_rate() {
    display_inputbox "$1" "If filtering by sample rate, the maximum sample rate to send" 0 0 "$2"
}



# display_gcf_out_brp_channels
# filter_channels (default "")
# $1 is the title text to display
# $2 is the current values
# Do we need to check what we are given ???
display_gcf_out_brp_channels() {
    display_inputbox "$1" "Enter the names of the channels to be transmitted, separated by \
commas. The exact name of the channel must be given, in the format SYSID-STRID." \
           0 0 "$2"
}



# display_gcf_out_brp_gcfout_dbdir
# gcfout_dbdir (default 262144)
# $1 is the title text to display
# $2 is the current value from the config file
display_gcf_out_brp_gcfout_dbdir() {
    local gdi2gcfinst

    gdi2gcf_list_sockets $2
    if [ -z "$GDI2GCF_INSTANCE" ]
    then
        gdi2gcfinst=default
    else
        gdi2gcfinst=$GDI2GCF_INSTANCE
    fi

    # MENU is setup by gdi2gcf_list_sockets
    if [ -z "${MENU}" ]
    then
        clear
        echo "No GDI2GCF converters found."
        exit 1
    fi

    display_menu "$1" "Select GDI2GCF converter" 0 0 $gdi2gcfinst
}



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

    if [ -z "$2" ]
    then
        default="$3"
    else
        default="$2"
    fi

    display_inputbox "$1" "State directory" 0 0 "$default"
}



# This displays the General settings for gcf-out-brp config.
# $1 is the titletext to display
gcf_out_brp_section_general() {
    local titletext="$1"

    # acknak_timeout (default 150)
    display_gcf_out_brp_acknak "${titletext} - General" "${GOB_ACKNAK_TIMEOUT}" "${DEF_GOB_ACKNAK_TIMEOUT}"
    NEW_GOB_ACKNAK_TIMEOUT="`cat "${DIALOG_RESULTFILE}"`"

    # allow_terminal (default true)
    display_gcf_out_brp_allow_terminal "${titletext} - General" "${GOB_ALLOW_TERMINAL}"
    RESULT=$?
    if [ ${RESULT} -eq 0 ]    # yes selected
    then
        NEW_GOB_ALLOW_TERMINAL="true"
    elif [ ${RESULT} -eq 1 ]    # no selected
    then
        NEW_GOB_ALLOW_TERMINAL="false"
    fi

    # gcfout_dbdir (default /var/lib/gdi2gcf.instance)
    display_gcf_out_brp_gcfout_dbdir "${titletext} - General" "${GOB_GCFOUT_DBDIR}"
    newgdi2gcfinst="`cat "${DIALOG_RESULTFILE}"`"
    gdi2gcf_get_socket_path $newgdi2gcfinst
    NEW_GOB_GCFOUT_DBDIR="${GDI2GCF_DBDIR}"

    # brp_dbdir (default /var/lib/gcf-out-brp.portname)
    display_gcf_out_brp_dbdir "${titletext} - General" "${GOB_BRP_DBDIR}" "${DEF_GOB_BRP_DBDIR}"
    NEW_GOB_BRP_DBDIR="`cat "${DIALOG_RESULTFILE}"`"
}



gcf_out_brp_section_filtering() {
    local titletext="$1"
    local type

    # filter_type (default none)
    display_gcf_out_brp_filter_type "${titletext} - Filtering" "${GOB_FILTER_TYPE}"
    NEW_GOB_FILTER_TYPE="`cat ${DIALOG_RESULTFILE}`"
    type=${NEW_GOB_FILTER_TYPE}

    # Now we need to do different things depending on the value of the filter type
    case "${type}" in
    all)
        # Just need to set the actual filter type to none because that is what it
        # needs in the config file.
        NEW_GOB_FILTER_TYPE=none
        ;;
    status)
        # nothing else needed
        ;;
    sample_rate)
        # need to get value for sample rate
        display_gcf_out_brp_sample_rate "${titletext} - Filtering" "${GOB_FILTER_SAMPLE_RATE}"
        NEW_GOB_FILTER_SAMPLE_RATE="`cat ${DIALOG_RESULTFILE}`"

        # Check sample rate is actually set to something
        if [ -z "${NEW_GOB_FILTER_SAMPLE_RATE}" ]
        then
            clear
            echo "Cannot select filter by sample rate without setting a sample rate."
            exit 1
        fi

        ;;
    channel_name)
        # need to get value for channels
        display_gcf_out_brp_channels "${titletext} - Filtering" "${GOB_FILTER_CHANNELS}"
        NEW_GOB_FILTER_CHANNELS="`cat ${DIALOG_RESULTFILE}`"

        # Check something is set for the channel name(s)
        if [ -z "${NEW_GOB_FILTER_CHANNELS}" ]
        then
            clear
            echo "Cannot select filter by channel name without setting a channel name."
            exit 1
        fi
        ;;
    esac
}



# Add fixed values first checking that they are not already there.
# $1 is the config file name
# $2 is the user friendly name of the port
serial_gcfout_add_fixed_values() {
    local value
    local cffile="$1"

    value="`cfget ${cffile} application_description`"
    if [ -z "${value}" ]
    then
        cfset ${cffile} application_description="$2"
    fi

    value="`cfget ${cffile} mode`"
    if [ -z "${value}" ]
    then
        cfset ${cffile} mode="direct"
    fi

    value="`cfget ${cffile} filter_type`"
    if [ -z "${value}" ]
    then
        cfset ${cffile} filter_type=none
    fi
}




# Go through the list of things that the user can change in the gcf-out-brp config
# files.
#   $1 - portname
#   $2 - user friendly name of port
# If something is Cancelled or there is an error then this can exit.
serial_gcfout_parameters() {
    local port="$1"
    local portname="$2"
    local titletext

    GOB_CFG_FILE="${SERIAL_CFDIR}/gcf-out-brp/${port}"

    # Set default values
    DEF_GOB_BRP_DBDIR="/var/lib/gcf-out-brp."${port}
    DEF_GOB_ACKNAK_TIMEOUT="150"

    # Get current values or set empty values if file doesn't already exist
    # Need to set defaults if the value isn't set in the config file too.
    if [ -r "${GOB_CFG_FILE}" ]
    then
        # get current values for each item
        GOB_ACKNAK_TIMEOUT="`cfget ${GOB_CFG_FILE} acknak_timeout`"

        GOB_ALLOW_TERMINAL="`cfget ${GOB_CFG_FILE} allow_terminal`"
        if [ ! -z "${GOB_ALLOW_TERMINAL}" ]
        then
            cftrue ${GOB_CFG_FILE} allow_terminal
            if [ $? -eq 0 ]
            then
                GOB_ALLOW_TERMINAL="true"
            else
                GOB_ALLOW_TERMINAL="false"
            fi
        fi

        GOB_FILTER_TYPE="`cfget ${GOB_CFG_FILE} filter_type`"
        GOB_FILTER_SAMPLE_RATE="`cfget ${GOB_CFG_FILE} filter_sample_rate`"
        GOB_FILTER_CHANNELS="`cfget ${GOB_CFG_FILE} filter_channels`"
        GOB_GCFOUT_DBDIR="`cfget ${GOB_CFG_FILE} gcfout_dbdir`"
        GOB_BRP_DBDIR="`cfget ${GOB_CFG_FILE} brp_dbdir`"
    else
        GOB_ACKNAK_TIMEOUT=""
        GOB_ALLOW_TERMINAL=""
        GOB_FILTER_TYPE=""
        GOB_FILTER_SAMPLE_RATE=""
        GOB_FILTER_CHANNELS=""
        GOB_GCFOUT_DBDIR=""
        GOB_BRP_DBDIR=""
    fi

    # Now need to go through the list of values for the user to set/change.
    # Always write a new config file and always restart the service.  
    titletext="${port} GCF out config"

    gcf_out_brp_section_general "${titletext}"

    gcf_out_brp_section_filtering "${titletext}"

    cconf_create_config_file "${GOB_CFG_FILE}"

    # Now we need to set the values in the config file
    cfset "${GOB_CFG_FILE}" acknak_timeout="${NEW_GOB_ACKNAK_TIMEOUT}"
    cfset "${GOB_CFG_FILE}" allow_terminal="${NEW_GOB_ALLOW_TERMINAL}"
    cfset "${GOB_CFG_FILE}" filter_type="${NEW_GOB_FILTER_TYPE}"
    cfset "${GOB_CFG_FILE}" filter_sample_rate="${NEW_GOB_FILTER_SAMPLE_RATE}"
    cfset "${GOB_CFG_FILE}" filter_channels="${NEW_GOB_FILTER_CHANNELS}"
    cfset "${GOB_CFG_FILE}" gcfout_dbdir="${NEW_GOB_GCFOUT_DBDIR}"
    cfset "${GOB_CFG_FILE}" brp_dbdir="${NEW_GOB_BRP_DBDIR}"

    # Now add the fixed value items if needed
    serial_gcfout_add_fixed_values "${GOB_CFG_FILE}" "${portname}"

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



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