#!/bin/bash
#  Configuration file manipulation
#
#   Copyright (c) 2009 Guralp Systems Ltd. All rights reserved.
#

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

# Load support functions
script_dir="$(dirname "$0")"
source "${script_dir}/functions.sh"
source "${script_dir}/svc_funcs.sh"



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

        # Now the service type dependant variables

        # Print some sensible defaults
	gcs_var d_pppd_startup_delay		"30"
	gcs_var d_test_command			"/bin/ping -c 5 gstm.guralp.com"
	gcs_var d_time_between_tests		"30"
	gcs_var d_kill_command			"/usr/bin/killall pppd"
	gcs_var d_kill9_fail_count		"3"
	gcs_var d_kill9_command			"/usr/bin/killall -9 pppd"
	gcs_var d_reboot_fail_count		"30"
	gcs_var d_reboot_command		"/sbin/reboot"
	gcs_var d_reboot_force_command		"/sbin/reboot -n"

        # Convert the config file into variable decls
        if [ -r "${CFGFILE}" ]
	then
		pppd_startup_delay="$(gcs_get_varcf "pppd_startup_delay" "${CFGFILE}")"
		test_command="$(gcs_get_varcf "test_command" "${CFGFILE}")"
		time_between_tests="$(gcs_get_varcf "time_between_tests" "${CFGFILE}")"
		kill_command="$(gcs_get_varcf "kill_command" "${CFGFILE}")"
		kill9_fail_count="$(gcs_get_varcf "kill9_fail_count" "${CFGFILE}")"
		kill9_command="$(gcs_get_varcf "kill9_command" "${CFGFILE}")"
		reboot_fail_count="$(gcs_get_varcf "reboot_fail_count" "${CFGFILE}")"
		reboot_command="$(gcs_get_varcf "reboot_command" "${CFGFILE}")"
		reboot_force_command="$(gcs_get_varcf "reboot_force_command" "${CFGFILE}")"

	    # Echo the results for user display
	    gcs_var o_pppd_startup_delay	"$pppd_startup_delay"
	    gcs_var o_test_command		"$test_command"
	    gcs_var o_time_between_tests	"$time_between_tests"
	    gcs_var o_kill_command		"$kill_command"
	    gcs_var o_kill9_fail_count		"$kill9_fail_count"
	    gcs_var o_kill9_command		"$kill9_command"
	    gcs_var o_reboot_fail_count		"$reboot_fail_count"
	    gcs_var o_reboot_command		"$reboot_command"
	    gcs_var o_reboot_force_command	"$reboot_force_command"
	fi
}



do_check() {
	gcs_read_vars

	# Standard service control/info vars
	gcs_svc_check

	# Service dependent variables
	[ -z "${new_test_command}" ] && gcs_err "test_command" "Command must be specified."
	[ -z "${new_kill_command}" ] && gcs_err "kill_command" "Command must be specified."

	if [ -n "${new_kill9_fail_count}" ]
	then
		[ -z "${new_kill9_command}" ] && \
			gcs_err "kill9_command" \
				"Command must be specified if force kill count is set."
	fi

	if [ -n "${new_reboot_fail_count}" ]
	then
		[ -z "${new_reboot_command}" ] && \
			gcs_err "reboot_command" \
				"Command must be specified if reboot fail count is set."
		[ -z "${new_reboot_force_command}" ] && \
			gcs_err "reboot_force_command" \
				"Command must be specified if reboot fail count is set."
	fi
}



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

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

	# Create required directory
	group_mkdir "$(dirname "${CFGFILE}")"

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

	# Simple keywords
	gcs_set_varcf "pppd_startup_delay" "${CFGFILE}" "${new_pppd_startup_delay}"
	gcs_set_varcf "test_command" "${CFGFILE}" "${new_test_command}"
	gcs_set_varcf "time_between_tests" "${CFGFILE}" "${new_time_between_tests}"
	gcs_set_varcf "kill_command" "${CFGFILE}" "${new_kill_command}"
	gcs_set_varcf "kill9_fail_count" "${CFGFILE}" "${new_kill9_fail_count}"
	gcs_set_varcf "kill9_command" "${CFGFILE}" "${new_kill9_command}"
	gcs_set_varcf "reboot_fail_count" "${CFGFILE}" "${new_reboot_fail_count}"
	gcs_set_varcf "reboot_command" "${CFGFILE}" "${new_reboot_command}"
	gcs_set_varcf "reboot_force_command" "${CFGFILE}" "${new_reboot_force_command}"

        # 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
