#!/bin/bash
# config-scripts/src/share/sys/gdi-base_link.sh
#
#  Copyright 2009-2017 Guralp Systems Limited.
#  Author: Laurence Withers <lwithers@guralp.com>
#  
#   gdi-base configuration link functions
#     These functions simplify configuration of modules which are linked
#     through the gdi-base multiplexor. Rather than have the user specify the
#     full path to the IPC socket, an iselect is used to allow the user to
#     select the multiplexor from a list of modules. This list employs the
#     user's own description, to make the selection more user-friendly.
#



# gcs_gdi_base_iselect()
#  This function prints the choices of GDI multiplexor module (gdi-base) in a manner suitable for
#  an iselect, along with choosing a suitable default value for the gdi_socket variable.
#   $1 -> current setting of "gdi_socket"
#   $2 -> "source" or "sink"
#  Exports:
#   GDI_CONFIG_PATH - full path to config file of current instance
#   GDI_SOCKET_PATH - full path to socket of current instance
gcs_gdi_base_iselect() {
    local i default module gdicfg socket desc default_cf current_cf default_sock current_sock default_cf current_cf

    current_socket="$1"
    source_or_sink="$2"

    i=0
    for gdicfg in "${CONFIGDIR}/gdi-base/"*.local
    do
        # pull out the socket path, skipping the file if one isn't present
        socket="$(gcs_get_varcf "${source_or_sink}_socket_path" "${gdicfg}")"
        [ -z "${socket}" ] && continue

        # get the user-friendly instance name
        desc="$(gcs_get_varcf "application_description" "${gdicfg}")"
        [ -z "${desc}" ] && desc="${socket}"

        # choose a default
        module="$(basename "${gdicfg}" ".local")"
        [ -z "${default}" ] && default="${module}"
        [ "${module}" == "default" ] && default="${module}" # always prefer the default instance
        if [ "${default}" == "${module}" ]
        then
            default_sock="${socket}"
            default_cf="${gdicfg}"
        fi

        # see if the user has set this one
        if [ "${socket}" == "${current_socket}" ]
        then
            gcs_var "o_gdi_socket" "${module}"
            current_sock="${socket}"
            current_cf="${gdicfg}"
        fi
        
        # update the iselect
        echo "select_gdi${i}=${module} ${desc}"
        ((++i))
    done

    if [ -z "${default}" ]
    then
        gcs_err fatal "No gdi-base instances configured."
    else
        gcs_var "d_gdi_socket" "${default}"
    fi

    export GDI_SOCKET_PATH="${current_sock:-${default_sock}}"
    export GDI_CONFIG_PATH="${current_cf:-${default_cf}}"
}



# gcs_gdi_base_dereference()
#  Checks the value of "new_gdi_socket" to be sure it is valid, and dereferences it to the actual
#  socket path.
#   $1 -> "source" or "sink"
#  Exports:
#   GDI_CONFIG_PATH - full path to config file of current instance
gcs_gdi_base_dereference() {
    local gdicfg socket

    source_or_sink="$1"

    gdicfg="${CONFIGDIR}/gdi-base/${new_gdi_socket}.local"
    if [ ! -r "${gdicfg}" ]
    then
        gcs_err fatal "No matching gdi-base module (${new_gdi_socket}) found."
        return 1
    fi

    socket="$(gcs_get_varcf "${source_or_sink}_socket_path" "${gdicfg}")"
    if [ -z "${socket}" ]
    then
        gcs_err fatal "gdi-base module ${new_gdi_socket} has no socket_path."
        return 1
    fi

    export new_gdi_socket="${socket}"
    export GDI_CONFIG_PATH="${gdicfg}"
}



# gcs_gdi_base_get_metadata
#  Retrieves the current value of a piece of metadata for a given channel. Uses GDI_CONFIG_PATH,
#  so it must be called after gcs_gdi_base_iselect() or gcs_gdi_base_dereference().
#   $1 -> channel name
#   $2 -> metadata field...
gcs_gdi_base_get_metadata() {
    gdi-metadata-get -C "${GDI_CONFIG_PATH}" "$@"
}



# Flag set if a metadata change is made
gcs_gdi_base_metadata_changed=""

# gcs_gdi_base_set_metadata
#  Sets new metadata value, but only if it has changed. Uses GDI_CONFIG_PATH, so it must be called
#  after gcs_gdi_base_iselect() or gcs_gdi_base_dereference().
#   $1 -> channel name
#   $2 -> metadata field
#   $3 -> metadata value
gcs_gdi_base_set_metadata() {
    orig="$(gcs_gdi_base_get_metadata "$1" "$2")"
    if [ "${orig}" != "$3" ]
    then
        gdi-metadata-set -C "${GDI_CONFIG_PATH}" "$1" "$2" "$3"
	gcs_gdi_base_metadata_changed=true
    fi
}



# Inform gdi-base of any metadata changes
gcs_gdi_base_metadata_reload() {

    if [ ! -z "${gcs_gdi_base_metadata_changed}" ]
    then
	gdi-metadata-reload "${new_gdi_socket/sink/source}" > /dev/null 2>&1
    fi
}
