#!/bin/bash
# config-scripts/src/share/svc--qscd-out/qscd-out.sh
# 
#  Copyright: ©2013, Güralp Systems Ltd.
#  License: GPLv3
#

#
#   Note that at present we tie this to operation using strong-motion-expanded
#   DM24mk3 channel names (i.e. ending with ZO, 2S etc.). The scheme is to take
#   the si.2D variable from the config file and use it as the instrument name
#   (without the 2S suffix).

# Handle the navbar query without loading anything
if [ "X$1" == "X--navbar" ]
then
	echo "servicestop	Services"
	echo "servicessub/qscd-out	qscd-out"
	echo "qscd-out/$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_scan() {
    local chan

    gdi-dump -l | grep "32-bit floating point samples, 1 sps:.*2S$" | while read -r chan
    do
        chan="${chan/*1 sps: }"
        chan="${chan:0:${#chan}-2}"
        echo -n "\$I\$${chan}\$L\$"
    done
}



do_read() {
    local i entry line

    # Standard control info
    gcs_svc_read || return 1

    # Now the service type dependant variables

    # Global defaults
    gcs_var d_station_name	"$(hostname | cut -c1-5 | tr a-z A-Z)"

    if [ -r "$CFGFILE" ]
    then
	# Handle the simple vars
	gcs_var_o station_name
	gcs_var_o location_name

	# Channels
	entry="$(gcs_get_varcf "si.2D" "${CFGFILE}")"
	if [ "${entry: -2}" == "2S" ]
	then
	    gcs_var "o_inst" "${entry:0:${#entry}-2}"
	fi

	# Convert push hosts into table
	i=0
	line="$(gcs_get_varcf "push" "$CFGFILE")"
	for entry in $line
	do
	    gcs_var "o_push_host${i}" "${entry/,*}"
	    gcs_var "o_push_port${i}" "${entry/*,}"
	    ((i++))
	done

	gcs_gdi_base_iselect "$(gcs_get_varcf gdi_socket "$CFGFILE")" "sink"
    else
	gcs_gdi_base_iselect "" "sink"
    fi

    # Get list of possible instruments
    entry="$(do_scan)"
    [ -z "${entry}" ] && entry="\$I\$\$B\$No suitably-configured instruments found\$R\$"
    gcs_var "scan_results" "${entry}"
}



do_check() {

    gcs_read_vars

    # Standard service control/info vars
    gcs_svc_check

    # Service dependant variables

    gcs_gdi_base_dereference "sink"

    # Check we have at least one client listed
    check_table_not_empty "push" "push_host" \
			"At least one target host must be specified"
}



do_write() {
    local -i i
    local val
    local val2
    local push_list

    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"

    # Do the simple keywords in place
    gcs_set_varcf "gdi_socket"          "$CFGFILE" "$new_gdi_socket"
    gcs_set_varcf "station_name"        "$CFGFILE" "$new_station_name"
    gcs_set_varcf "location_name"       "$CFGFILE" "$new_location_name"

    # channels
    gcs_set_varcf "minimum.Z"  "${CFGFILE}" "${new_inst}ZO"
    gcs_set_varcf "minimum.N"  "${CFGFILE}" "${new_inst}NO"
    gcs_set_varcf "minimum.E"  "${CFGFILE}" "${new_inst}EO"
    gcs_set_varcf "maximum.Z"  "${CFGFILE}" "${new_inst}ZQ"
    gcs_set_varcf "maximum.N"  "${CFGFILE}" "${new_inst}NQ"
    gcs_set_varcf "maximum.E"  "${CFGFILE}" "${new_inst}EQ"
    gcs_set_varcf "average.Z"  "${CFGFILE}" "${new_inst}ZT"
    gcs_set_varcf "average.N"  "${CFGFILE}" "${new_inst}NT"
    gcs_set_varcf "average.E"  "${CFGFILE}" "${new_inst}ET"
    gcs_set_varcf "pga.2D"     "${CFGFILE}" "${new_inst}2P"
    gcs_set_varcf "pga.3D"     "${CFGFILE}" "${new_inst}3P"
    gcs_set_varcf "si.Z"       "${CFGFILE}" "${new_inst}ZS"
    gcs_set_varcf "si.N"       "${CFGFILE}" "${new_inst}NS"
    gcs_set_varcf "si.E"       "${CFGFILE}" "${new_inst}ES"
    gcs_set_varcf "si.2D"      "${CFGFILE}" "${new_inst}2S"

    # push destinations
    push_list=""
    for (( i = 0 ; i < $new_push_rows ; i++ ))
    do
        val=$(gcs_get_var new_push_host$i)
        [ -z "$val" ] && continue

        val2=$(gcs_get_var new_push_port$i)
        push_list="$push_list ${val},${val2:-9000}"
    done
    gcs_set_varcf "push"                "$CFGFILE" "$push_list"

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


case "X$1" in
X--check)       do_check    ;;
X--write)       do_write    ;;
X--read)        do_read     ;;
*)              exit 1      ;;
esac

gcs_cleanup
exit 0


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