#!/bin/bash
#
# Guralp Configuration System
#   Copyright (c) 2010 Guralp Systems Ltd. All rights reserved.
#
#   Status ioline assert configuration

# Note there will only ever be one instance of this and it will run by default.

# Handle navbar query first so don't have to read files
if [ "X$1" == "X--navbar" ]
then
	echo "statustop	Status iolines"
	exit 0
fi


MYNAME=xmlstatus-assert
CFGFILE="/etc/$MYNAME/$MYNAME.local"



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


do_read() {
    gcs_var "d_enabled" ""
    gcs_var "d_warning_level" ""

    ioline_iselect "output" "assert_on_warning"
    gcs_var "d_assert_on_warning" ""

    gcs_var "d_err_level" ""

    ioline_iselect "output" "assert_on_err"
    gcs_var "d_assert_on_err" ""

    if [ -r "$CFGFILE" ]
    then
        gcs_var_o2 "enabled" ""
        gcs_var_o2 "warning_level" ""
        gcs_var_o2 "assert_on_warning" ""
        gcs_var_o2 "err_level" ""
        gcs_var_o2 "assert_on_err" ""
    fi
}

do_check() {
    gcs_read_vars

    # Basic checks are performed by the configuration engine

    # Need to check that the lines are both setup - default is /none so the
    # config engine won't spot them.  But the config engine will handle the
    # levels not being set.
    if [ "${new_assert_on_warning}" = "/none" ]
    then
        gcs_err "assert_on_warning" "Line to be asserted on warning must be set"
    fi

    if [ "${new_assert_on_err}" = "/none" ]
    then
        gcs_err "assert_on_err" "Line to be asserted on error must be set"
    fi
}

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

    # Standard service controls
    gcs_ensure_cfgfile_exists "$CFGFILE"

    gcs_set_varcf2 "enabled"               "$CFGFILE" "${new_enabled}"
    gcs_set_varcf2 "warning_level"         "$CFGFILE" "${new_warning_level}"
    gcs_set_varcf2 "assert_on_warning"     "$CFGFILE" "${new_assert_on_warning}"
    gcs_set_varcf2 "err_level"             "$CFGFILE" "${new_err_level}"
    gcs_set_varcf2 "assert_on_err"         "$CFGFILE" "${new_assert_on_err}"

    gcs_update_svc "$CFGFILE"

    gcs_svc_reload $MYNAME
}

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

gcs_cleanup
exit 0
