#!/usr/bin/env bash
#
# Copyright (C) 2003 VA Linux Systems Japan, K.K.
#
# LICENSE NOTICE
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#                                                                     
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#

: <<-POD

=head1 NAME

ultrapossum-config - interface to ultrapossum configuration framework

=head1 SYNOPSIS

B<ultrapossum-config> <argument> [<argument options>]

=head1 DESCRIPTION

B<ultrapossum-config> provides the interface to UltraPossum configuration
framework.

=head1 ARGUMENT

=over 4

=item B<init>

Prints the bash shell fragment to initialize your bash script for
UltraPossum. You can initialize your script by evaluating this output.

=item B<term>

Prints the bash shell fragment to terminate the UltraPossum environment.
You must evaluate this output when you have evaluated B<init> in your
script.

=item B<get> [<variables...>]

Prints the specified variables in the form VARIABLE=VALUE. Each variable
is printed in 1 line. If you don't specify B<<variables...>>, all variables
are printed.

=item B<set> <module> <variable>=<value>...

Sets the spcified variable of <module> to the specified value.

=item B<remove> <module> <variables...>

Removes the specified variable configuration of <module> and make them default value.

=item B<module>

Prints all the plugins.

=item B<variable> [-l] [<modules...>]

Prints variables of the spcified modules. When no <modules..> argument given,
all variables are printed. If B<-l> given, a long listing format will be used.

=item B<status> [<modules...>]

Prints the status of plugins.

=back

=head1 EXAMPLE

To see the specified variable,

  ~$ ultrapossum-config get PACKAGE VERSION
  PACKAGE="ultrapossum"
  VERSION="1.0rc5"

To set the value

  ~$ ultrapossum-config set server MASTER=master

=head1 AUTHOR

Masato Taruishi <taru@valinux.co.jp>

=cut

POD

set -e

if test "x$ULTRAPOSSUM_DEBUG" != "x"; then
  set -x
fi

prefix=${prefix:-/usr}
SHAREDIR=${prefix}/share/ultrapossum
MODULEDIR=${MODULEDIR:-$SHAREDIR/module.d}
SYSCONFDIR=${SYSCONFDIR:-/etc/ultrapossum}
localstatedir=${localstatedir:-/var}
PACKAGE=${PACKAGE:-ultrapossum}

. $SHAREDIR/init.d/10compat
. $SHAREDIR/init.d/10funcs

case "x$1" in
    xinit)
        shift
	echo "MODULECATEGORY=$1; . $SHAREDIR/modules"
	;;
    xterm)
        echo ". $SHAREDIR/term"
	;;
    xvariable)
        . $SHAREDIR/init.d/10config
        shift
	if test "x$1" = "x-l"; then
	  shift
	  l=`getlogical "$@"`
	  getvariable "$@" | while read v; do
	    if include "$v" "$l"; then
	      echo -n "l"
	    else
	      echo -n "p"
	    fi
            echo " $v"
	  done
	else
          getvariable "$@"
	fi
        ;;
    xget)
        shift
	eval `ultrapossum-config init`
        trap "eval `ultrapossum-config term`" 0
	getconfig "$@"
	;;
    xremove)
        shift
        if test "x$2" = "x"; then
          echo "Usage: $0 remove <category> <var>..."
          exit 1
        fi
        cat=$1
        shift
        . $SHAREDIR/init.d/10config
        for v in "$@"
        do
          ultrapossum_removeconf "$cat" "$v"
        done
        ;;

    xset)
        shift
        if test "x$2" = "x"; then
          echo "Usage: $0 set <category> <var=val...>"
          exit 1
        fi
        eval `ultrapossum-config init`
        trap "eval `ultrapossum-config term`" 0
	c=$1
        shift
	for v in "$@"
	do
          var=`echo $v | cut -d= -f1`
          val=`echo $v | cut -d= -f2-`
          ultrapossum_setconf $c $var "$val"
	done
        ;;

    xstatus)

	if test -f $localstatedir/lib/$PACKAGE/status; then
	  . $localstatedir/lib/$PACKAGE/status
	fi
	shift
	if test "x$1" != "x"; then
	  modules="$@"
	else
	  modules=`ultrapossum-config module`
	fi
	for m in $modules
	do
	  m2="ULTRAPOSSUM_MODULE_`echo $m | tr 'a-z' 'A-Z'`"
	  echo "$m=${!m2}"
	done

        ;;

    xmodule)
	/bin/ls -1 $MODULEDIR/ | while read f
	do
	  if test -d $MODULEDIR/$f; then
	    echo $f
	  fi
	done
        ;;
    x)
	echo "Usage: $0 init|get <var...>|<set <module> <var>=<val>...>|remove <module> <var>|module|variable <module...>|status <module...>|term"
	exit 1
	;;
    x*)
	echo "E: unknown argument $1" 1>&2
	exit 1
	;;
esac

