:
# NAME:
#	setopts.sh - set opt_* for shell scripts
#
# SYNOPSIS:
#	opt_str=s:a.b^cl,z=
#	opt_a=default
#	
#	. setopts.sh
#
# DESCRIPTION:
#	This module sets shell variables for each option specified in
#	"opt_str".
#
#	If the option is followed by a ``:'' it requires an argument.
#	It defaults to an empty string and specifying that option on
#	the command line overrides the current value. 
#	
#	If the option is followed by a ``.'' then it is treated as for
#	``:'' except that any argument provided on the command line is
#	appended to the current value using the value of "opt_dot" as
#	separator (default is a space).
#
#	If the option is followed by a ``,'' then it is treated as for
#	a ``.'' except that the separator is "opt_comma" (default ,).
#
#	If the option is followed by ``='' it requires an argument
#	of the form "var=val" which will be evaluated.
#
#	If the option is followed by a ``^'' then it is treated as a
#	boolean and defaults to 0.
#	
#	Options that have no qualifier are set to the flag if present
#	otherwise they are unset.  That is if '-c' is given then
#	"opt_c" will be set to '-c'.
#
#	If "opt_assign_eval" is set (and to something other than
#	'no'), args of the form "var=val" will be evaluated.
#			
# AUTHOR:
#	Simon J. Gerraty <sjg@crufty.net>
#

# RCSid:
#	$Id: setopts.sh,v 1.9 2003/01/02 02:09:30 sjg Exp $
#
#	@(#) Copyright (c) 1995 Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

# the case checks just skip the sed(1) commands unless needed
case "$opt_str" in
*\^*)	# the only ones we need to set are the booleans x,
	eval `echo $opt_str | sed -e 's/[^^]*$//' -e 's/[^^]*\([^^]^\)/\1/g' -e 's/\(.\)^/opt_\1=${opt_\1-0}; /g'`
	;;
esac
case "$opt_str" in
*[=,.\^]*)
	_opt_str=`echo $opt_str | sed -e 's/[=,.]/:/g' -e 's/\^//g'`;;
*)	_opt_str=$opt_str;;
esac

set -- `getopt $_opt_str $* 2>&1`
opt_append=${opt_append:-" "}
opt_dot=${opt_dot:-$opt_append}
opt_comma=${opt_comma:-,}

case "$1" in
getopt:)
	Myname=${Myname:-`basename $0 .sh`}
	echo "$*" | tr ':' '\012' | sed -e '/^getopt/d' -e 's/ getopt$//' -e "s/^/$Myname:/" -e 's/ --/:/' -e 's/-.*//' 2>&2
	exit 1
	;;
esac

while :
do
	case "$1" in
	--)	shift; break;;
	-*)
		# Most shells give you ' ' in IFS whether you
		# want it or not, but at least one, doesn't.
		# So the following gives us consistency.
		o=`IFS=" -"; set -- $1; echo $*` # lose the '-'
		case /$opt_str/ in
		*${o}.*)
			eval opt_$o=\"\${opt_$o}\${opt_$o:+$opt_dot}$2\"; shift;;
		*${o},*)
			eval opt_$o=\"\${opt_$o}\${opt_$o:+$opt_comma}$2\"; shift;;
		*${o}:*)
			eval opt_$o=\"$2\"; shift;;
		*${o}=*)
			case "$2" in
			*=*)	eval "$2"; shift;;
			*)	Myname=${Myname:-`basename $0 .sh`}
				echo "$Myname: -$o requires argument of form var=val" >&2
				exit 1
				;;
			esac
			;;
		*${o}\^*)
			eval opt_$o=1;;
		*${o}*)
			eval opt_$o=$1;;
		esac
		;;
	*=*)	case "$opt_assign_eval" in
		""|no) break;;
		*) eval "$1";;
		esac	   
		;;
	*)	break;;
	esac
	shift
done


