#!/bin/bash
# Bash arithmetic functions used so don't substitute plain sh
#
# Guralp Configuration System
#   Copyright (c) 2017 Guralp Systems Ltd. All rights reserved.
#
#   gdi2dbi database client module

# Handle the navbar query without loading anything
if [ "X$1" == "X--navbar" ]
then
	echo "servicestop	Services"
	echo "servicessub/gdi2dbi	gdi2dbi"
	echo "gdi2dbi/$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() {
    # Standard control info
    gcs_svc_read || return 1

    # Now the service type dependant variables

    ## Global defaults
    gcs_var "d_dbi_driver"	"pgsql"

    ## PostgreSQL
    gcs_var "d_pgsql_host"	""
    gcs_var "d_pgsql_password"	""
    gcs_var "d_pgsql_port"	""
    gcs_var "d_pgsql_dbname"	""
    gcs_var "d_pgsql_username"	"postgres"

    if [ -r "$CFGFILE" ]
    then
	## Global
	gcs_var_o2 "dbi_driver"	""

	## PostgreSQL
	gcs_var_o2 "host"	"pgsql"
	gcs_var_o2 "password"	"pgsql"
	gcs_var_o2 "port"	"pgsql"
	gcs_var_o2 "dbname"	"pgsql"
	gcs_var_o2 "username"	"pgsql"

	gcs_gdi_base_iselect "$(gcs_get_varcf2 "gdi_socket" "${CFGFILE}" "")" "sink"
    else
	gcs_gdi_base_iselect "" "sink"
    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"
}



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 "gdi_socket"              "${CFGFILE}" "${new_gdi_socket}"        ""
    gcs_set_varcf2 "dbi_driver"              "${CFGFILE}" "${new_dbi_driver}"        ""

    ## PostgreSQL
    gcs_set_varcf2 "host"                    "${CFGFILE}" "${new_pgsql_host}"        "pgsql"
    gcs_set_varcf2 "password"                "${CFGFILE}" "${new_pgsql_password}"    "pgsql"
    gcs_set_varcf2 "port"                    "${CFGFILE}" "${new_pgsql_port}"        "pgsql"
    gcs_set_varcf2 "dbname"                  "${CFGFILE}" "${new_pgsql_dbname}"      "pgsql"
    gcs_set_varcf2 "username"                "${CFGFILE}" "${new_pgsql_username}"    "pgsql"

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

    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
