#!/bin/bash
# svc--gdi2gcf/gdi2gcf.sh
# 
#  Copyright: ©2014, Güralp Systems Ltd.
#  Author: Laurence Withers <lwithers@guralp.com>
#  License: GPLv3
#

# Handle the navbar query without loading anything
if [ "$1" = "--navbar" ]
then
    echo "servicestop Services"
    echo "servicessub/gdi2gcf gdi2gcf"
    echo "gdi2gcf/$3 $3"
    exit 0
fi

# Load support functions
script_dir="`dirname $0`"
. "${script_dir}/functions.sh"
. "${script_dir}/svc_funcs.sh"
. "${script_dir}/gdi-base_link.sh"

name="$2"
sel="$3"

CFGFILE="${CONFIGDIR}/${name}/${sel}.local"
CTLFILE="${CONFIGDIR}/${name}/${sel}.ctl.local"
SRVBASE="${SERVDIR}/${name}"


do_read() {
    local mapnum entry have_channel_map gdi_name gcf_name

    # Standard control info
    gcs_svc_read || return 1



    gcs_var "d_dbdir"           "/var/lib/${name}.${sel}"
    gcs_var "d_buffer_size"     "1"
    gcs_var "d_socket_group"    "daemon"
    gcs_var "d_socket_mode"     "0666"

    have_channel_map="0"
    if [ -r "${CFGFILE}" ]
    then
        gcs_var_o2 "dbdir"          ""
        gcs_var_o2 "buffer_size"    ""

        # do we have a channel map?
        for entry in `gcs_list_varcf2_sections "${CFGFILE}"`
        do
            if [ "${entry}" = "channel_map" ]
            then
                have_channel_map="1"
                break
            fi
        done

        gcs_gdi_base_iselect "`gcs_get_varcf2 "gdi_socket" "${CFGFILE}" ""`" "sink"
    else
        gcs_gdi_base_iselect "" "sink"
    fi



    #
    # Read in configured channel map if we have it
    #
    mapnum="0"
    ignore_unmapped="0"
    if [ "${have_channel_map}" -eq 0 ]
    then
        gcs_var "o_channel_map_mode" "automatic"
    else
        entry="`gcs_get_varcf2 "reject_unfiltered_channels" "${CFGFILE}" ""`"
        if gcs_truefalse "${entry}"
        then
            gcs_var "o_channel_map_mode" "manual"
            ignore_unmapped="1"
        else
            gcs_var "o_channel_map_mode" "semi"
        fi

        while read -r gdi_name gcf_name
        do
            gcs_var "o_channel_map_src${mapnum}" "${gdi_name}"
            gcs_var "o_channel_map_dest${mapnum}" "${gcf_name}"
            ((++mapnum))
        done < <(gcs_list_varcf2 "${CFGFILE}" "channel_map")
    fi



    #
    # Suggest mapping for other channels (except in manual mode)
    #
    if [ "${ignore_unmapped}" -eq 0 ]
    then
        while read -r gdi_name gcf_name
        do
            # if we already mapped this channel, don't use auto name
            entry="`gcs_get_varcf2 "${gdi_name}" "${CFGFILE}" "channel_map"`"
            [ -z "${entry}" ] || continue

            gcs_var "o_channel_map_src${mapnum}" "${gdi_name}"
            gcs_var "o_channel_map_dest${mapnum}" "${gcf_name}"
            ((++mapnum))
        done < <(gdi2gcf-channelmap "${GDI_SOCKET_PATH}")
    fi
}



do_check() {
    gcs_read_vars

    # Standard service control/info vars
    gcs_svc_check

    # Service dependant variables
    # Most of the checks are done by the main engine

    gcs_gdi_base_dereference "sink"

    if [ "${new_channel_map_mode}" == "manual" ]
    then
        check_table_not_empty "channel_map" "channel_map_src" \
            "No channels have been mapped."
    fi
}



do_write_channel_map() {
    local -i i
    local gdi_name gcf_name

    for (( i = 0 ; i < ${new_channel_map_rows} ; i++ ))
    do
        eval gdi_name="\${new_channel_map_src${i}}"
        [ -z "${gdi_name}" ] && continue
        eval gcf_name="\${new_channel_map_dest${i}}"
        gcs_set_varcf2 "${gdi_name}" "${CFGFILE}" "${gcf_name}" "channel_map"
    done
}

do_write() {
    do_check
    if (( gcs_errors > 0 ))
    then
        return
    fi

    if gcs_truefalse "${new_delete:-false}"
    then
        gcs_svc_delete "${name}" "${sel}"
    fi

    # Standard service controls
    gcs_ensure_cfgfile_exists "${CTLFILE}"
    gcs_set_varf "DESC" "${CTLFILE}" "${desc}"

    # Now the service type dependant variables
    gcs_ensure_cfgfile_exists "${CFGFILE}"

    ## Global
    gcs_set_varcf2 "application_description"    "${CFGFILE}" "${new_desc}"              ""
    gcs_set_varcf2 "dbdir"                      "${CFGFILE}" "${new_dbdir}"             ""
    gcs_set_varcf2 "socket_group"               "${CFGFILE}" "${new_socket_group}"      ""
    gcs_set_varcf2 "socket_mode"                "${CFGFILE}" "${new_socket_mode}"       ""
    gcs_set_varcf2 "buffer_size"                "${CFGFILE}" "${new_buffer_size}"       ""
    gcs_set_varcf2 "gdi_socket"                 "${CFGFILE}" "${new_gdi_socket}"        ""

    ## Channel map
    gcs_clear_varcf2_section "${CFGFILE}" "channel_map"

    case "${new_channel_map_mode}" in
    automatic)
        gcs_set_varcf2 "reject_unfiltered_channels" "${CFGFILE}" "" ""
        ;;
    semi)
        gcs_set_varcf2 "reject_unfiltered_channels" "${CFGFILE}" "false" ""
        do_write_channel_map
        ;;
    manual)
        gcs_set_varcf2 "reject_unfiltered_channels" "${CFGFILE}" "true" ""
        do_write_channel_map
        ;;
    esac

    # Update the service script
    gcs_update_svc "${name}" "${sel}" "${enable}" "${desc}" "${CFGFILE}" \
        "${CTLFILE}" "${SRVBASE}" "${new_socket}"
    
    gcs_svc_reload "${name}" "${sel}"
}



case "$1" in
--check)
    do_check
    ;;

--write)
    do_write
    ;;

--read)
    do_read
    ;;

*)
    exit 1
    ;;
esac

gcs_cleanup
exit 0

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