#!/bin/bash
# console-config/src/lib-scripts/serial-gcf-in-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-in-brp.



# display_gcf_in_brp_rewind
# brp_disable_rewind (default false)
# $1 is the title text to display
# $2 is the current value
display_gcf_in_brp_rewind() {
    if [ -z "$2" -o "$2" = "false" ]
    then
        dialog --title "$1" \
               --defaultno --yesno "Disable BRP rewind (for DM24s in adaptive mode etc)" \
               0 0
    else
        dialog --title "$1" \
               --yesno "Disable BRP rewind (for DM24s in adaptive mode etc)" \
               0 0
    fi
    return $?
}



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

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

    # Add the path for the default gdi socket path if it isn't already there
    value="`cfget ${cffile} gdi_socket`"
    if [ -z "${value}" ]
    then
        gdi_get_socket_path default
        cfset ${cffile} gdi_socket="${GDI_SOCKET_PATH}"
    fi
}



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

    GIB_CFG_FILE="${SERIAL_CFDIR}/gcf-in-brp/${port}"

    # Get current values or set defaults if file doesn't already exist
    if [ -r "$GIB_CFG_FILE" ]
    then
        # get the current values for each item
        GIB_BRP_DISABLE_REWIND="`cfget ${GIB_CFG_FILE} brp_disable_rewind`"
        if [ ! -z "${GIB_BRP_DISABLE_REWIND}" ]
        then
            cftrue "${GIB_CFG_FILE}" brp_disable_rewind
            if [ $? -eq 0 ]
            then
                GIB_BRP_DISABLE_REWIND="true"
            else
                GIB_BRP_DISABLE_REWIND="false"
            fi
        fi

    else
        # set empty values for each item
        GIB_BRP_DISABLE_REWIND=""
    fi

    # Now go through the values giving user a choice
    # We always write a new config file.
    titletext="${port} GCF in config"

    # brp_disable_rewind (default false)
    display_gcf_in_brp_rewind "${titletext}" "${GIB_BRP_DISABLE_REWIND}"
    RESULT=$?
    if [ "${RESULT}" -eq 0 ]    # yes selected
    then
        NEW_GIB_BRP_DISABLE_REWIND="true"
    elif [ "${RESULT}" -eq 1 ]    # no selected
    then
        NEW_GIB_BRP_DISABLE_REWIND="false"
    fi

    cconf_create_config_file "${GIB_CFG_FILE}"

    # Now we need to set the values in the config file
    cfset "${GIB_CFG_FILE}" brp_disable_rewind="${NEW_GIB_BRP_DISABLE_REWIND}"

    # Now add the fixed value items if needed
    serial_gcfin_add_fixed_values "${GIB_CFG_FILE}"

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



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