#!/bin/bash
#
# Guralp Configuration System
#   Copyright (c) 2008-2010 Guralp Systems Ltd. All rights reserved.
#
#   CD1.1 receiver config

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

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


do_read() {
	# Standard control info
	gcs_svc_read || return 1

	# Now the service type dependant variables

	# Fold name to uppercase and restict to 8 characters
	gcs_var d_receiver_name	\
		"$(hostname | tr a-z A-Z | sed -e 's/^\(........\).*/\1/')"

	gcs_var d_station_type	"IMS"
	gcs_var d_bind_host	"0.0.0.0"
	gcs_var d_bind_service	"8000"
	gcs_var d_connect_host	""
	gcs_var d_connect_service	"8000"
	gcs_var d_senders	""
	gcs_var d_data_frames_reset_timeout "false"

	if [ -r "$CFGFILE" ]
	then
	    gcs_var_o receiver_name
	    gcs_var_o station_type
	    gcs_var_o bind_host
	    gcs_var_o bind_service
	    gcs_var_o connect_host
	    gcs_var_o connect_service
	    gcs_var_o senders
            gcs_var_o data_frames_reset_timeout

	    gcs_cd11mux_iselect "$(gcs_get_varcf "mux_path" "${CFGFILE}")" "source"
	else
	    gcs_cd11mux_iselect "" "source"
	fi
}


do_check() {
	gcs_read_vars

	# Standard service control/info vars
	gcs_svc_check

	# Service dependent variables

	gcs_cd11mux_dereference "source"
}


do_write() {
	local -i i
	local val

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

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

	gcs_ensure_cfgfile_exists $CFGFILE

	# Standard path controls
	gcs_set_varf DESC	$CTLFILE "$desc"

	# Do the simple keywords in place
	gcs_set_varcf application_description "${CFGFILE}"	"${new_desc}"
	gcs_set_varcf receiver_name	"$CFGFILE"	"$new_receiver_name"
	gcs_set_varcf station_type	"$CFGFILE"	"$new_station_type"
	gcs_set_varcf bind_host		"$CFGFILE"	"$new_bind_host"
	gcs_set_varcf bind_service	"$CFGFILE"	"$new_bind_service"
	gcs_set_varcf connect_host	"$CFGFILE"	"$new_connect_host"
	gcs_set_varcf connect_service	"$CFGFILE"	"$new_connect_service"
	gcs_set_varcf clientdb_dir	"$CFGFILE"	"/var/lib/${name}.${sel}"
	gcs_set_varcf clientdb_dir_group	"$CFGFILE"	"data"
	gcs_set_varcf clientdb_dir_mode	"$CFGFILE"	"02775"
	gcs_set_varcf senders		"$CFGFILE"	"$new_senders"
	gcs_set_varcf mux_path		"$CFGFILE"	"$new_mux_path"
	gcs_set_varcf data_frames_reset_timeout "${CFGFILE}" "${new_data_frames_reset_timeout}"

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


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

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


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

gcs_cleanup
exit 0
