#! /bin/bash

#
# These options can be recognized (longopts comma-separated. colon means argument is required)
#
LONGOPTS=help,arguments
SHORTOPTS=hab:zc

#
# Usage info. Be nice and keep this up-to-date!
#
function printUsage {
   echo "Usage: `basename $0` [OPTIONS]"
   echo "Options: "
   echo "   -h | -help              prints this help and exits"
   echo ""
}

#
# Run getopt (posixly_correct needed). We run twice:
# First run is simply to check the commandline for correctness
# Second run is the real deal which replaces the command line args with getopt's output
export POSIXLY_CORRECT=1

getopt -n `basename $0` -Q -u -a -l $LONGOPTS $SHORTOPTS "$@" || {
   printUsage
	exit $EC_BADARGS;
}

set -- `getopt -u -a -l $LONGOPTS $SHORTOPTS "$@"`

#
# Iterate over getopt's output and set CL_XXX variables accordingly
#
while : 
do
	case "$1" in
	--arguments)            echo "argument 'arguments' well parsed" ;; 
	-a)                 echo "argument 'a' well parsed";; 
	-b)             echo "argument 'b' and '$2' well parsed";; 
	-z)             echo "argument 'z' well parsed";; 
	-c)             echo "argument 'c' well parsed" ;; 
	--help)             CL_HELP=true ;; 
	-h)                 CL_HELP=true ;; 
        --) break ;;
	esac
	shift
done
shift

# restore 
export POSIXLY_CORRECT=
unset POSIXLY_CORRECT

if [ "$CL_HELP" ] ; then
   printUsage
   exit $EC_OK
fi

echo "The rest of arguments are: '$*'";
