#!/bin/sh
# ----------------------------------------------------------------------------
# - afnix-query -
# - afnix default option query system -
# ----------------------------------------------------------------------------
# - This program is free software; you can redistribute it and/or modify -
# - it provided that this copyright notice is kept intact. -
# - -
# - 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. In not event shall -
# - the copyright holder be liable for any direct, indirect, incidental or -
# - special damages arising in any way out of the use of this software. -
# ----------------------------------------------------------------------------
# - copyright (c) 1999-2007 amaury darsch -
# ----------------------------------------------------------------------------
# the program name
progname=$0
# the value to query
queryval=$1
# default value to return
retrnval=
# the directory list to search
dirslist=
# the platform to search
platname=
# ----------------------------------------------------------------------------
# - local function always make life easier -
# ----------------------------------------------------------------------------
# print a usage message
usage () {
echo "usage: afnix-query [options] name"
echo " -h print this help message"
echo
echo " --default=value set the default value"
echo " --dirlist=dir set the directory list"
exit 0
}
# print an error message
error () {
echo "error: $1"
exit 1
}
# get the deault value
getdef () {
# update the platform name by calling afnix-guess
guess=`dirname $progname`/afnix-guess
platname=`$guess -n`
if test "$?" != "0"; then
echo $platname
exit 1
fi
# update the platform directory list
defltdir=`dirname $progname`/../def
dirslist="~/.afnix /usr/local/etc/afnix /etc/afnix $defltdir"
}
# query the value in the directory list
query () {
for d in $dirslist; do
dfile=$d/afnix-${platname}.def
if test -f "$dfile"; then
value=`grep "^$queryval" $dfile`
if test "$?" = "0"; then
echo $value | awk '{print $2}'
exit 0
fi
fi
done
}
# get the default value
getdef
# ----------------------------------------------------------------------------
# - parse options - this is where we really start -
# ----------------------------------------------------------------------------
lastopt=
for nextopt
do
# assign the previous option argument
if test -n "$lastopt"; then
eval "$lastopt=\$nextopt"
lastopt=
continue
fi
# extract options
case "$nextopt" in
-*=*) argsopt=`echo "$nextopt" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
*) argsopt= ;;
esac
# process options now
case "$nextopt" in
-h | --help) usage ;;
--default) lastopt=prefix ;;
--default=*) retrnval="$argsopt" ;;
--dirlist) lastopt=prefix ;;
--dirlist=*) dirslist="$argsopt" ;;
-*) error "illegal option $nextopt" ;;
*) queryval="$nextopt" ;;
esac
done
# check for query value
if test -z "$queryval" ; then
error "missing query value"
fi
# query the value
query
if test -z "$retrnval"; then
error "cannot query value"
fi
# return the default value
echo $retrnval
exit 0
syntax highlighted by Code2HTML, v. 0.9.1