#!/bin/sh

alterator_api_version=1

. alterator-sh-functions

FACILITIES_DIR=/etc/control.d/facilities
CONTROL=/usr/sbin/control

list_facility()
{
    for f in "$FACILITIES_DIR"/*;do
    local name="${f##*/}"
    write_table_item \
        name "$name" \
        label "$name" \
        summary "$($CONTROL "$name" summary)" \
        current "$($CONTROL "$name")"
    done
}

list_state()
{
    [ -n "$in_name" ] || return 0

    "$CONTROL" "$in_name" list|
	tr ' ' '\n'|
	write_enum
}

write_state()
{
    retcode=0
    if [ -n "$in_name" -a -n "$in_mode" ];then
	"$CONTROL" "$in_name" "$in_mode"
	retcode=$?
    fi

    [ "$retcode" -eq 0 ] ||
	write_error "`_ "facility setup failed"`"
}

read_state()
{
    [ -n "$in_name" ] || return 0
    write_string_param mode "$("$CONTROL" "$in_name")"
}

read_help()
{
    [ -n "$in_name" -a -n "$in_mode" ] || return 0
    write_string_param help "$("$CONTROL" "$in_name" help "$in_mode")"
}

alterator_export_proc list_facility
alterator_export_proc list_state
alterator_export_proc read_state
alterator_export_proc write_state
alterator_export_proc read_help

message_loop
