#!/bin/bash

# sourcing example, P. TenHoopen, 04/22/08

CFGFILE="myconfig.cfg"

# test for configuration file

if [[ ! -z "$CFGFILE" ]]
then
    echo ""
    echo "No configuration file found."
    echo "Auto-creating one with default values..."
    touch $CFGFILE
    echo "# Default configuration file" > $CFGFILE
    echo "DEBUG=0" >> $CFGFILE
    echo "VERBOSE=0" >> $CFGFILE
fi

# read in config file
source myconfig.cfg

# 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 ""


