#!/bin/sh -e
# -*- mode: Shell-script; tab-width: 8; fill-column: 70; -*- 
# $Id: control,v 1.1.1.1 2005/07/07 14:54:21 inger Exp $ 

PROG="${0##*/}" #program name

CONTROL_BIN=/usr/sbin/control
CONTROL_FACILITIES=/etc/control.d/facilities

print_usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
Usage: $PROG [options] 

alterator backend for control manipulations

Valid options are:
  -h, --help	display help screen
  -l, --list	get list of avaliable locales
  -t, --type	check for existance
  -d, --delete	(empty action, required for backend)
  -r, --read	read locale information
  -w, --write	change locale information

Report bugs to <legion@altlinux.org>
EOF
	[ -n "$1" ] && exit "$1" || exit
}

TEMP=`getopt -n $PROG -o h,l:,d:,r:,w:,t: -l help,list:,delete:,read:,write:,type: -- "$@"` || print_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		-h|--help) print_usage 0
			;;
		-l|--list) shift; facility="$1"
			[ "$facility" != "/" ] || facility="*"
			find "$CONTROL_FACILITIES/" -mindepth 1 -maxdepth 1 -type f -name "$facility" ! -name '.provides.sh' -printf '%f r\n' | sort -r
			;;
		-d|--delete)
			#ignore
			shift;dir="$1";
			;;
		-t|--type)
			#ignore
			shift;dir="$1";
			;;
		-r|--read) shift; dir="$1"
			facility="${dir##*/}"
#			printf 'name:%s\n' "$facility"
			printf 'list:'
			"$CONTROL_BIN" "$facility" list
			"$CONTROL_BIN" "$facility" help | sed -e 's,^\([^:]\+:\)[[:space:]]\+$,\1,'
			printf 'status:'
			"$CONTROL_BIN" "$facility"
			;;
		-w|--write) shift; facility="$1"
			while read l; do
				option="${l%%:*}"
				value="${l#*:}"
				[ "$option" = "status" ] || continue
				"$CONTROL_BIN" "$facility" "$value"
				break
			done
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
	shift
done
