#!/bin/bash
#
# Guralp Configuration System
#   Copyright (c) 2017 Guralp Systems Ltd. All rights reserved.
#
#   Guralp responder (Discovery) daemon

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

# Load support functions
script_dir=$(dirname $0)
. $script_dir/functions.sh
. $script_dir/svc_funcs.sh


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

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


do_read() {
    local i entry line reject

    # Standard control info
    gcs_svc_read || return 1

    # Now the service type dependant variables
    gcs_var "d_hostlabel"		"Platinum"
    gcs_var "d_enable_registry"		"false"
    gcs_var "d_registry_address"	"52.34.40.123"
    gcs_var "d_cloud_registry_id"	".*"

    if [ -r "$CFGFILE" ]
    then
        gcs_var_o2 "hostlabel"		""
        gcs_var_o2 "enable_registry"	""
        gcs_var_o2 "registry_address"	""
        gcs_var_o2 "cloud_registry_id"	""
    fi
}



do_check() {
    gcs_read_vars

    # Standard service control/info vars
    gcs_svc_check

    # Service dependant variables
}



do_write() {
    local i line entry reject

    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 "hostlabel" "${CFGFILE}" "${new_hostlabel}"
    gcs_set_varcf "enable_registry" "${CFGFILE}" "${new_enable_registry}"
    gcs_set_varcf "registry_address" "${CFGFILE}" "${new_registry_address}"
    gcs_set_varcf "cloud_registry_id" "${CFGFILE}" "${new_cloud_registry_id}"

    # 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
