# !/bin/bash
# Copyright (C) 2018 Nicholas.A.Schembri@bamboofields.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#


self=$(echo $0|sed 's:.*/::')
display_help(){
cat <<EOF
$self(1)                            User Commands

NAME
       $self

SYNOPSIS
       $self [OPTION]

DESCRIPTION

      $self is able to be run with the following. (add more)   
   
      --cron Do not produce any output. The needed information is written to daily.log
                  only with --cron will data be written to daily

      --envInstance=<Environment> 
                dev
                test
                qa
                uat
                prod

     --port=<port>
                tcp port 


      --testall
                Run internal test 
                <expand https://github.com/ingydotnet/test-simple-bash/blob/master/lib/test-simple.bash>
                <planned test-simple.bash - Simple TAP test framework for Bash>

      --debugLevel=<number>
                0 - none
                1 - some
                2 - more

       -h, --help
              Display

Note:   

          $self --debugLevel=0 --testall


Validation:
          ( test code is okay )
          ( test is valid     )


        
EOF
}




###########################################################
cron_task(){

           cmd="curl --silent --insecure  -i https://www.google.com"
           cmd="date"
           if [ $debugLevel -gt 0 ]; then
              echo "${cmd}"
           fi
           # sometimes it's hard for bash to parse cmd with variables  
           # eval "$cmd"
           # export output=$(${cmd}) 
           echo $($cmd) .
}





########################################################
timestamp=$(date +%Y.%m.%d.%H%M%S)
LOG_FILE=/var/tmp/$self.$timestamp.log 
verbose=0
quiet=1
user=""
pass=""
host=""
task=""
cron=0
debugLevel=0

# get the args
while [ "$#" -gt 0 ]; do
    case $1 in
        -h|-\?|--help)   # synopsis, then exit.
            display_help
            exit
            ;;
        -q|--quiet)
            quiet=1
            ;;
        --task=?*)
            task=${1#*=} 
            ;;
        --testall)
            testall
            exit
            ;;
      --host=?*)
            host=${1#*=} # Delete everything up to "=" and assign the remainder.
            ;;
      --debugLevel=?*)
            debugLevel=${1#*=} 
            #export debugLevel
            ;;
        --cron)
            cron=1
            ;;
        --)              # End of all options.
            shift
            break
            ;;
        -?*)
            printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
            ;;
        *)               # Default case: If no more options then break out of the loop.
            break
    esac

    shift
done



#########################################################



    if [ $debugLevel -gt 0 ]; then
           cat <<EOF
           
        Check the command line output
           
           host: $host

           user: $user
           pass: ${pass}
           cron: ${cron}
EOF

     fi



########################
# 
# turn arguments into function calls;
# the case is a simple example
case $task in
        writeConfig)
            write_config
            ;;
        displayConfig)
            display_config   
            ;;

     esac


# if the task is complex, call a function for more that a few rules.
# if a and b but not c 
   


if [ $cron -gt 0 ]; then
    output=$(cron_task)
    [ $debugLevel -gt 0 ] && echo "${output}"
    rc=$?
    if [ $rc -gt 0 ]; then
      echo "${output}"
      exit $rc ;
    fi
    exit 0;

fi


Copyright 2024 Nicholas.A.Schembri