#!/bin/bash

#getopts example, P. TenHoopen, 03/25/08

# set default values

DEBUG=0
VERBOSE=0

# define function to show usage of script

fnUsage()
{
echo ""
echo "getopts Example"
echo ""
echo "Options:"
echo "   -d      Turn debugging on"
echo "   -f      Specify file to work with"
echo "   -h      Show this message"
echo "   -v      Verbose"
}

# parse command line options

while getopts "df:hv" options; do
  case $options in
    d ) DEBUG=1;;
    f ) FILE=$OPTARG;;
    h ) fnUsage
         exit 0;;
    v ) VERBOSE=1;;
    \? ) fnUsage
         exit 1;;
    * ) fnUsage
          exit 1;;
  esac
done

# test for required variables

if [[ -z "$FILE" ]]
then
     echo ""
     echo "Error:  Please specify a file."
     fnUsage
     exit 1
fi


# main processing

echo ""

if [ $DEBUG -eq 1 ]
then
   echo "Debugging is on"
fi

if [ $VERBOSE -eq 1 ]
then
   if [ $DEBUG -eq 1 ]
   then
      echo "Entering verbose code block"
   fi
   echo "Verbose output is on"
   echo ""
   ls -l $FILE
else
   if [ $DEBUG -eq 1 ]
   then
      echo "Entering non-verbose code block"
   fi
   echo ""
   ls $FILE
fi

echo ""

