#
# this is an include file for scriptt.sh
# Usage: put it into the working directory if calling scriptt.sh and
#        ensure that the line 
#
#        [ -f "./scriptt_test.sh" ] && . "./scriptt_test.sh"
#
# exists in scriptt.sh
#

# test functions

## --------------------------------------
## SetWindowTitle
##
## change the title of an X window
##
## usage: SetWindowTitle "newtitle"
##
## returns: ${__TRUE} - ok, window title set
##          ${__FALSE} - error, don't know the terminal type; 
##                       window title NOT changed
##
## Note
##
## Original Author of this function (xtitle) is:
##  William Seppeler
##  Email:seppeler@yahoo.com
##
function SetWindowTitle {
  typeset __FUNCTION="SetWindowTitle";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}

  typeset NEW_TITLE="$*"

  typeset THISRC=${__FALSE}

  typeset ESC='\033'
  typeset BEL='\007'

  case ${TERM} in

    xterm*|dtterm|aixterm|rxvt)
      print "${ESC}]0;${NEW_TITLE}${BEL}\c"
      THISRC=${__TRUE}
      ;;

    sun-cmd)
      print "${ESC}]l${NEW_TITLE}${ESC}\\\\\c"
      #print "${ESC}]L${NEW_TITLE}${ESC}\\\\\c"
      THISRC=${__TRUE}
      ;;

    iris-ansi)
      print "${ESC}P1.y${NEW_TITLE}${ESC}\\\\\c"
     #print "${ESC}P3.y${NEW_TITLE}${ESC
      THISRC=${__TRUE}
      ;;
  esac     

  return ${THISRC}
}

# exit routines for testing 
#
function MyExitRoutine1 {
  typeset __FUNCTION="MyExitRoutine1";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}

  typeset THISRC=0
  LogMsg "*** MyExitRoutine1 called ***"

  return  ${THISRC}
}


function MyExitRoutine2 {
  typeset __FUNCTION="MyExitRoutine2";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}

  typeset THISRC=0
  LogMsg "*** MyExitRoutine2 called ***"

  return  ${THISRC}
}


# finish routines for testing
#
function MyFinishRoutine1 {
  typeset __FUNCTION="MyFinishRoutine1";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}

  typeset THISRC=0
  LogMsg "*** MyFinishRoutine1 called ***"

  return  ${THISRC}
}


function MyFinishRoutine2 {
  typeset __FUNCTION="MyFinishRoutine2";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}

  typeset THISRC=0
  LogMsg "*** MyFinishRoutine2 called ***"

  return  ${THISRC}
}



# user defined routines for the various signals for testing
#
function MyRoutineSIGTERM {
  typeset __FUNCTION="MyRoutineSIGTERM";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}

  typeset THISRC=0
  LogMsg "*** MyRoutineSIGTERM called ***"
  SIGTERM_CATCHED=${__TRUE}

  return  ${THISRC}
}

function MyRoutineSIGUSR1 {
  typeset __FUNCTION="MyRoutineSIGUSR1";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}

  typeset THISRC=0
  LogMsg "*** MyRoutineSIGUSR1 called ***"
  SIGUSR1_CATCHED=${__TRUE}

  return  ${THISRC}
}

function MyRoutineSIGUSR2 {
  typeset __FUNCTION="MyRoutineSIGUSR2";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}

  typeset THISRC=0
  LogMsg "*** MyRoutineSIGUSR2 called ***"
  SIGUSR2_CATCHED=${__TRUE}

  return  ${THISRC}
}


function MyRoutineSIGINT {
  typeset __FUNCTION="MyRoutineSIGINT";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}
  
  typeset THISRC=0
  LogMsg "*** MyRoutineSIGINT called ***"
  SIGINT_CATCHED=${__TRUE}

  return  ${THISRC}
}

function MyRoutineSIGHUP_0 {
  typeset __FUNCTION="MyRoutineSIGHUP_0";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}
  
  typeset THISRC=0
  LogMsg "*** MyRoutineSIGUP_0 called ***"
  SIGHUP_CATCHED=${__TRUE}

  return  ${THISRC}
}

function MyRoutineSIGHUP_1 {
  typeset __FUNCTION="MyRoutineSIGHUP_1";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}
  
  typeset THISRC=1
  LogMsg "*** MyRoutineSIGHUP_1 called ***"
  SIGHUP_CATCHED=${__TRUE}

  return  ${THISRC}
}

# routine to test the signal handling in a sub routine
#
function MySIGTestRoutine {
  typeset __FUNCTION="MySIGTestRoutine";    ${__FUNCTION_INIT} ; ${__DEBUG_CODE}
  LINENO=$LINENO
    
  LogMsg "Testing the SIGNAL handler from inside a function ..."
  
  MY_VERBOSE_MODE="${__VERBOSE_MODE}"
  MY_VERBOSE_LEVEL="${__VERBOSE_LEVEL}"
  MY_RT_VERBOSE_LEVEL="${__RT_VERBOSE_LEVEL}"

  LogMsg "-"
  LogMsg "The current function is MySIGTestRoutine; LineNo is $LINENO."
  
  LogMsg "Testing INT Signal Handler"
  __SIGNAL_SIGINT_FUNCTION="MyRoutineSIGINT"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
  SIGINT_CATCHED=${__FALSE}
  __USER_BREAK_ALLOWED=${__FALSE}
  LogMsg "SIGINT_CATCHED is ${SIGINT_CATCHED}; LINENO is $LINENO"
  kill -2 $$
  LogMsg "SIGINT_CATCHED is ${SIGINT_CATCHED}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}


  LogMsg "-"
  LogMsg "The current function is MySIGTestRoutine; LineNo is $LINENO."
  LogMsg "Testing HUP Signal Handler -1 -"
  __SIGNAL_SIGHUP_FUNCTION="MyRoutineSIGHUP_1"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
  SIGHUP_CATCHED=${__FALSE}
  LogMsg "__VERBOSE_MODE is ${__VERBOSE_MODE}"
  LogMsg "SIGHUP_CATCHED is ${SIGHUP_CATCHED}; LINENO is $LINENO"
  LogMsg "Expected result:  __VERBOSE_MODE should not change"
  kill -HUP $$
  LogMsg "SIGHUP_CATCHED is ${SIGHUP_CATCHED} (should be ${__TRUE} now)"
  LogMsg "__VERBOSE_MODE is ${__VERBOSE_MODE}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}
 
  LogMsg "-"
  LogMsg "The current function is MySIGTestRoutine; LineNo is $LINENO."
  LogMsg "Testing HUP Signal Handler - 2 -"
  __SIGNAL_SIGHUP_FUNCTION="MyRoutineSIGHUP_0"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
    SIGHUP_CATCHED=${__FALSE}
  LogMsg "__VERBOSE_MODE is ${__VERBOSE_MODE}"
  LogMsg "SIGHUP_CATCHED is ${SIGHUP_CATCHED}; LINENO is $LINENO"
  LogMsg "Expected result:  __VERBOSE_MODE should change"
  kill -HUP $$
  LogMsg "SIGHUP_CATCHED is ${SIGHUP_CATCHED} (should be ${__TRUE} now)"   
  LogMsg "__VERBOSE_MODE is ${__VERBOSE_MODE}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}

  LogMsg "-"
  LogMsg "The current function is MySIGTestRoutine; LineNo is $LINENO."
  LogMsg "Testing USR1 Signal Handler"
  __SIGNAL_SIGUSR1_FUNCTION="MyRoutineSIGUSR1"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
  SIGUSR1_CATCHED=${__FALSE}
  LogMsg "SIGUSR1_CATCHED is ${SIGUSR1_CATCHED}; LINENO is $LINENO"
  kill -USR1 $$
  LogMsg "SIGUSR1_CATCHED is ${SIGUSR1_CATCHED}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}

  LogMsg "-"
  LogMsg "The current function is MySIGTestRoutine; LineNo is $LINENO."
  LogMsg "Testing USR2 Signal Handler"
  __SIGNAL_SIGUSR2_FUNCTION="MyRoutineSIGUSR2"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
  SIGUSR2_CATCHED=${__FALSE}
  LogMsg "SIGUSR2_CATCHED is ${SIGUSR2_CATCHED}; LINENO is $LINENO"
  kill -USR2 $$
  LogMsg "SIGUSR2_CATCHED is ${SIGUSR2_CATCHED}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}


  LogMsg "-"
}


# main test code
#
  LogMsg " --------------------------------------------------------- "
  LogMsg " Hints: Use \"$0 -H 2 >$0.doc\" "
  LogMsg "        to get the documentation "
  LogMsg "        Use \"$0 -D\" to test the single step mode"
  LogMsg " --------------------------------------------------------- "
         

# *** TEST CODE START ***
        
  SetWindowTitle "$0 - testing  general routines"
#  Code to test the sub routines

# code to test the house keeping
#
  mydir0="/tmp/testdir0.$$"
  myfile0="/tmp/testdir0.$$/testfile0.$$"
  myfile1="/tmp/testdir0.$$/testfile1.$$"

  mkdir -p "${mydir0}"
  touch "${myfile0}"
  touch "${myfile0}"

## __LIST_OF_TMP_MOUNTS - list of mounts that should be umounted at program end
##
  __LIST_OF_TMP_MOUNTS=""

## __LIST_OF_TMP_DIRS - list of directories that should be removed at program end
##
  __LIST_OF_TMP_DIRS="${mydir0} ${__LIST_OF_TMP_DIRS}"

## __LIST_OF_TMP_FILES - list of files that should be removed at program end
##
  __LIST_OF_TMP_FILES="${myfile0} ${myfile1} ${__LIST_OF_TMP_FILES}"

## __EXITROUTINES - list of routines that should be executed before the script ends
##   Note: These routines are called *before* temp files, temp directories, and temp 
##         mounts are removed
##
  __EXITROUTINES="MyExitRoutine1 MyExitRoutine2 MyExitRoutine3 ${__EXITROUTINES}"

## __FINISHROUTINES - list of routines that should be executed before the script ends
##   Note: These routines are called *after* temp files, temp directories, and temp
##         mounts are removed
##
  __FINISHROUTINES="MyFinishRoutine1 MyFinishRoutine3 MyFinishRoutine2 ${__FINISHROUTINES}"


  LogMsg "Calling \"CheckInputDevice\" ..."
  CheckInputDevice
  [ $? -eq 0 ] && LogMsg "  The input device is a terminal" || LogMsg "  The input device is NOT a terminal"

  LogMsg "Calling \"GetProgramDirectory\" ..."
  GetProgramDirectory $0 __ABSOLUTE_SCRIPTDIR
  LogMsg "   The variable __ABSOLUTE_SCRIPTDIR is now  \"${__ABSOLUTE_SCRIPTDIR}\" "

  SetWindowTitle "$0 - testing  substr"
#               123456789 1234567             

  sourceString="autobahn12schiene"
  pos="7"
  length="4"
  expectedResult="hn12"
  LogMsg "Calling \"substr\" with ${sourceString}  ${pos} ${length}"
  resultString=$( substr ${sourceString}  ${pos} ${length} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "


#               123456789 1234567             
  sourceString="autobahn12schiene"
  pos="1"
  length="3"
  expectedResult="aut"
  LogMsg "Calling \"substr\" with ${sourceString}  ${pos} ${length}"
  resultString=$( substr ${sourceString}  ${pos} ${length} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

#               123456789 1234567             
  sourceString="autobahn12schiene"
  pos="15"
  length="3"
  expectedResult="ene"
  LogMsg "Calling \"substr\" with ${sourceString}  ${pos} ${length}"
  resultString=$( substr ${sourceString}  ${pos} ${length} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

#               123456789 1234567             
  sourceString="autobahn12schiene"
  pos="15"
  length="7"
  expectedResult="ene"
  LogMsg "Calling \"substr\" with ${sourceString}  ${pos} ${length}"
  resultString=$( substr ${sourceString}  ${pos} ${length} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

#               123456789 1234567             
  sourceString="autobahn12schiene"
  pos="1"
  length="17"
  expectedResult="autobahn12schiene"
  LogMsg "Calling \"substr\" with ${sourceString}  ${pos} ${length}"
  resultString=$( substr ${sourceString}  ${pos} ${length} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "


#               123456789 1234567             
  sourceString="autobahn12schiene"
  pos="20"
  length="17"
  expectedResult=""
  LogMsg "Calling \"substr\" with ${sourceString}  ${pos} ${length}"
  resultString=$( substr ${sourceString} ${pos} ${length} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "



  SetWindowTitle "$0 - testing  pos"
#               123456789 123456789 12345
  sourceString="autobahn12auto12schiene12"
  searchString="auto"
  expectedResult="1"
  LogMsg "Calling \"pos\" with \"${searchString}\" \"${sourceString}\" "
  pos ${searchString}  ${sourceString} 
  result=$? 
  LogMsg "   The result is        \"${result}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

#               123456789 123456789 12345
  sourceString="autobahn12auto12schiene12"
  searchString="12schiene"
  expectedResult="15"
  LogMsg "Calling \"pos\" with \"${searchString}\" \"${sourceString}\" "
  pos ${searchString}  ${sourceString} 
  result=$? 
  LogMsg "   The result is        \"${result}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

#               123456789 123456789 12345
  sourceString="autobahn12auto12schiene12"
  searchString="auto12"
  expectedResult="11"
  LogMsg "Calling \"pos\" with \"${searchString}\" \"${sourceString}\" "
  pos ${searchString}  ${sourceString} 
  result=$? 
  LogMsg "   The result is        \"${result}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

#               123456789 123456789 12345
  sourceString="autobahn12auto12schiene12"
  searchString="auto"
  expectedResult="1"
  LogMsg "Calling \"pos\" with \"${searchString}\" \"${sourceString}\" "
  pos ${searchString}  ${sourceString} 
  result=$? 
  LogMsg "   The result is        \"${result}\" "
  LogMsg "   The result should be \"${expectedResult}\" "


#               123456789 123456789 12345
  sourceString="autobahn12auto12schiene12"
  searchString="autobahnX"
  expectedResult="0"
  LogMsg "Calling \"pos\" with \"${searchString}\" \"${sourceString}\" "
  pos ${searchString}  ${sourceString} 
  result=$? 
  LogMsg "   The result is        \"${result}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

#               123456789 123456789 12345
  sourceString="autobahn12auto12schiene12"
  searchString="12schiene"
  expectedResult="15"
  LogMsg "Calling \"pos\" with \"${searchString}\" \"${sourceString}\" "
  pos ${searchString}  ${sourceString} 
  result=$? 
  LogMsg "   The result is        \"${result}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

#               123456789 123456789 12345
  sourceString="autobahn12auto12schiene12"
  searchString="schiene12"
  expectedResult="17"
  LogMsg "Calling \"pos\" with \"${searchString}\" \"${sourceString}\" "
  pos ${searchString}  ${sourceString} 
  result=$? 
  LogMsg "   The result is        \"${result}\" "
  LogMsg "   The result should be \"${expectedResult}\" "


  SetWindowTitle "$0 - testing  lastpos"
#               123456789 123456789 12345
  sourceString="autobahn12auto12schiene12"
  searchString="auto"
  expectedResult="11"
  LogMsg "Calling \"lastpos\" with \"${searchString}\" \"${sourceString}\" "
  lastpos ${searchString}  ${sourceString} 
  result=$? 
  LogMsg "   The result is        \"${result}\" "
  LogMsg "   The result should be \"${expectedResult}\" "


#               123456789 1234567             
  sourceString="autobahn12schiene"
  searchString="ene"
  expectedResult="15"
  LogMsg "Calling \"lastpos\" with \"${searchString}\" \"${sourceString}\" "
  lastpos ${searchString}  ${sourceString} 
  result=$? 
  LogMsg "   The result is        \"${result}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

#               123456789 1234567             
  sourceString="autobahn12schiene"
  searchString="12"
  expectedResult="9"
  LogMsg "Calling \"lastpos\" with \"${searchString}\" \"${sourceString}\" "
  lastpos ${searchString}  ${sourceString} 
  result=$? 
  LogMsg "   The result is        \"${result}\" "
  LogMsg "   The result should be \"${expectedResult}\" "


  SetWindowTitle "$0 - testing  isnumber"
  testNumber="4"
  LogMsg "Calling \"isNumber\" with ${testNumber} "
  isNumber ${testNumber} 
  LogMsg "   The result is        $?"
  LogMsg "   The result should be 0"

  testNumber="4.5"
  LogMsg "Calling \"isNumber\" with ${testNumber} "
  isNumber ${testNumber} 
  LogMsg "   The result is        $?"
  LogMsg "   The result should be 1"

  testNumber="4a"
  LogMsg "Calling \"isNumber\" with ${testNumber} "
  isNumber ${testNumber} 
  LogMsg "   The result is        $?"
  LogMsg "   The result should be 1"

  testNumber="xy"
  LogMsg "Calling \"isNumber\" with ${testNumber} "
  isNumber ${testNumber} 
  LogMsg "   The result is        $?"
  LogMsg "   The result should be 1"

  SetWindowTitle "$0 - testing  toUppercase"
#               123456789 1234567             
  sourceString="autobahn12schiene"
  expectedResult="AUTOBAHN12SCHIENE"
  LogMsg "Calling \"toUppercase\" with \"${sourceString}\" "
  resultString=$( toUppercase  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

  SetWindowTitle "$0 - testing  toLowercase"
#               123456789 1234567             
  sourceString="AUTOBAHN12SCHiene"
  expectedResult="autobahn12schiene"
  LogMsg "Calling \"toLowercase\" with \"${sourceString}\" "
  resultString=$( toLowercase  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "


  SetWindowTitle "$0 - testing ConvertToBinary"
  sourceString="555"
  expectedResult="1000101011"
  LogMsg "Calling \"ConvertToBinary\" with \"${sourceString}\" "
  resultString=$( ConvertToBinary  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

  sourceString="255"
  expectedResult="11111111"
  LogMsg "Calling \"ConvertToBinary\" with \"${sourceString}\" "
  resultString=$( ConvertToBinary  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

  sourceString="0"
  expectedResult="0"
  LogMsg "Calling \"ConvertToBinary\" with \"${sourceString}\" "
  resultString=$( ConvertToBinary  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "


  SetWindowTitle "$0 - testing ConvertToHex"
  sourceString="555"
  expectedResult="22b"
  LogMsg "Calling \"ConvertToHex\" with \"${sourceString}\" "
  resultString=$( ConvertToHex  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

  sourceString="255"
  expectedResult="ff"
  LogMsg "Calling \"ConvertToHex\" with \"${sourceString}\" "
  resultString=$( ConvertToHex  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

  sourceString="0"
  expectedResult="0"
  LogMsg "Calling \"ConvertToHex\" with \"${sourceString}\" "
  resultString=$( ConvertToHex  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "


  SetWindowTitle "$0 - testing ConvertToOctal"
  sourceString="555"
  expectedResult="1053"
  LogMsg "Calling \"ConvertToOctal\" with \"${sourceString}\" "
  resultString=$( ConvertToOctal  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

  sourceString="255"
  expectedResult="377"
  LogMsg "Calling \"ConvertToOctal\" with \"${sourceString}\" "
  resultString=$( ConvertToOctal  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "

  sourceString="0"
  expectedResult=""
  LogMsg "Calling \"ConvertToOctal\" with \"${sourceString}\" "
  resultString=$( ConvertToOctal  ${sourceString} )
  LogMsg "   The result is        \"${resultString}\" "
  LogMsg "   The result should be \"${expectedResult}\" "


  SetWindowTitle "$0 - testing  replacestr"
  sourceString="aaa12345bbbb"
  oldSubstr="12345"
  newSubstr="ZZZZZ"
  expectedResult="aaaZZZZZbbbb"
  LogMsg "Calling \"replacestr\" with ${sourceString} ${oldSubstr} ${newSubstr}"
  replacestr ${sourceString} ${oldSubstr} ${newSubstr} resultvar
  THISRC=$?
  LogMsg "    The result is         \"${resultvar}\" "
  LogMsg "    The result should be  \"${expectedResult}\" "
  LogMsg "    The RC is ${THISRC}"
  
  sourceString="aaa12345bbbb"
  oldSubstr="12345"
  newSubstr="ZZZZZ"
  expectedResult="aaaZZZZZbbbb"
  LogMsg "Calling \"replacestr\" with ${sourceString} ${oldSubstr} ${newSubstr}"
  resultvar=$(replacestr ${sourceString} ${oldSubstr} ${newSubstr} )
  THISRC=$?
  LogMsg "    The result is         \"${resultvar}\" "
  LogMsg "    The result should be  \"${expectedResult}\" "
  LogMsg "    The RC is ${THISRC}"


  sourceString="aaa12345bbbb12345"
  oldSubstr="12345"
  newSubstr="ZZZZZ"
  expectedResult="aaaZZZZZbbbb12345"
  LogMsg "Calling \"replacestr\" with ${sourceString} ${oldSubstr} ${newSubstr}"
  replacestr ${sourceString} ${oldSubstr} ${newSubstr} resultvar
  THISRC=$?
  LogMsg "    The result is         \"${resultvar}\" "
  LogMsg "    The result should be  \"${expectedResult}\" "
  LogMsg "    The RC is ${THISRC}"
  
  sourceString="aaa12345bbbb12345"
  oldSubstr="12345"
  newSubstr="ZZZZZ"
  expectedResult="aaaZZZZZbbbb12345"
  LogMsg "Calling \"replacestr\" with ${sourceString} ${oldSubstr} ${newSubstr}"
  resultvar=$(replacestr ${sourceString} ${oldSubstr} ${newSubstr} )
  THISRC=$?
  LogMsg "    The result is         \"${resultvar}\" "
  LogMsg "    The result should be  \"${expectedResult}\" "
  LogMsg "    The RC is ${THISRC}"

  sourceString="aaa12345bbbb"
  oldSubstr="aaa"  
  newSubstr="ZZZ"
  expectedResult="ZZZ12345bbbb"
  LogMsg "Calling \"replacestr\" with ${sourceString} ${oldSubstr} ${newSubstr}"
  replacestr ${sourceString} ${oldSubstr} ${newSubstr} resultvar
  THISRC=$?
  LogMsg "    The result is         \"${resultvar}\" "
  LogMsg "    The result should be  \"${expectedResult}\" "
  LogMsg "    The RC is ${THISRC}"
  

  sourceString="aaa12345bbbb"
  oldSubstr="bbbb"
  newSubstr="ZZZZ"
  expectedResult="aaa12345ZZZZ"
  LogMsg "Calling \"replacestr\" with ${sourceString} ${oldSubstr} ${newSubstr}"
  replacestr ${sourceString} ${oldSubstr} ${newSubstr} resultvar
  THISRC=$?
  LogMsg "    The result is         \"${resultvar}\" "
  LogMsg "    The result should be  \"${expectedResult}\" "
  LogMsg "    The RC is ${THISRC}"

  sourceString="aaa12345bbbb"
  oldSubstr="aaa"  
  newSubstr=""
  expectedResult="12345bbbb"
  LogMsg "Calling \"replacestr\" with ${sourceString} ${oldSubstr} ${newSubstr}"
  resultvar=$(replacestr ${sourceString} ${oldSubstr} ${newSubstr} )
  THISRC=$?
  LogMsg "    The result is         \"${resultvar}\" "
  LogMsg "    The result should be  \"${expectedResult}\" "
  LogMsg "    The RC is ${THISRC}"

  sourceString="aaa12345bbbb"
  oldSubstr="123"  
  newSubstr=""
  expectedResult="aaa45bbbb"
  LogMsg "Calling \"replacestr\" with ${sourceString} ${oldSubstr} ${newSubstr}"
  resultvar=$(replacestr ${sourceString} ${oldSubstr} ${newSubstr} )
  THISRC=$?
  LogMsg "    The result is         \"${resultvar}\" "
  LogMsg "    The result should be  \"${expectedResult}\" "
  LogMsg "    The RC is ${THISRC}"
  
  sourceString="aaa12345bbbb"
  oldSubstr="aa"  
  newSubstr=""
  expectedResult="a12445bbbb"
  LogMsg "Calling \"replacestr\" with ${sourceString} ${oldSubstr} ${newSubstr}"
  resultvar=$(replacestr ${sourceString} ${oldSubstr} ${newSubstr} )
  THISRC=$?
  LogMsg "    The result is         \"${resultvar}\" "
  LogMsg "    The result should be  \"${expectedResult}\" "
  LogMsg "    The RC is ${THISRC}"
  

  sourceString="aaa12345bbbb"
  oldSubstr="bb"  
  newSubstr=""
  expectedResult="aaa12445bb"
  LogMsg "Calling \"replacestr\" with ${sourceString} ${oldSubstr} ${newSubstr}"
  resultvar=$(replacestr ${sourceString} ${oldSubstr} ${newSubstr} )
  THISRC=$?
  LogMsg "    The result is         \"${resultvar}\" "
  LogMsg "    The result should be  \"${expectedResult}\" "
  LogMsg "    The RC is ${THISRC}"

  
  SetWindowTitle "$0 - testing  general routines for user/group ids"
  LogMsg "Calling \"UserIsRoot\" ..."
  UserIsRoot
  [ $? -eq 0 ] && LogMsg "The user is root" || LogMsg "The user is NOT root"

  testUserID="xtrnaw7"
  LogMsg "Calling \"UserIs\" with \"${testUserID}\" "
  UserIs ${testUserID}
  LogMsg "   The result is $?"

  testUserID="zaurus"
  LogMsg "Calling \"UserIs\" with \"${testUserID}\" "
  UserIs ${testUserID}
  LogMsg "   The result is $?"

  testUserID="adm"
  LogMsg "Calling \"UserIs\" with \"${testUserID}\" "
  UserIs ${testUserID}
  LogMsg "   The result is $?"
  
  testUserID="otto"
  LogMsg "Calling \"UserIs\" with \"${testUserID}\" "
  UserIs ${testUserID}
  LogMsg "   The result is $?"

  LogMsg "Calling \"GetCurrentUID\"  "
  THIS_UID=$( GetCurrentUID )
  LogMsg "   The result is ${THIS_UID}"

  testUID="501"
  LogMsg "Calling \"GetUserName\" with \"${testUID}\"  "
  GetUserName ${testUID}
  LogMsg "   The result is \"${__USERNAME}\""

  testUID="0"
  LogMsg "Calling \"GetUserName\" with \"${testUID}\"  "
  GetUserName ${testUID}
  LogMsg "   The result is \"${__USERNAME}\""

  testUID="103"
  LogMsg "Calling \"GetUserName\" with \"${testUID}\"  "
  GetUserName ${testUID}
  LogMsg "   The result is \"${__USERNAME}\""

  testUID="1501"
  LogMsg "Calling \"GetUserName\" with \"${testUID}\"  "
  GetUserName ${testUID}
  LogMsg "   The result is \"${__USERNAME}\""

  testUsername="nobody"
  LogMsg "Calling \"GetU_ID\" with \"${testUsername}\"  "
  GetUID ${testUsername}
  LogMsg "   The result is \"${__USER_ID}\""

  testUsername="avahi"
  LogMsg "Calling \"GetU_ID\" with \"${testUsername}\"  "
  GetUID ${testUsername}
  LogMsg "   The result is \"${__USER_ID}\""

  testUsername="root"
  LogMsg "Calling \"GetUID\" with \"${testUsername}\"  "
  GetUID ${testUsername}
  LogMsg "   The result is \"${__USER_ID}\""


  testUsername="uucp"
  LogMsg "Calling \"GetUID\" with \"${testUsername}\"  "
  GetUID ${testUsername}
  LogMsg "   The result is \"${__USER_ID}\""

  testUsername="otto"
  LogMsg "Calling \"GetUID\" with \"${testUsername}\"  "
  GetUID ${testUsername}
  LogMsg "   The result is \"${__USER_ID}\""

  LogMsg " -------------------------------------------"
  

  SetWindowTitle "$0 - testing random routine"

  RAND_NUMBER1=$( rand ) ; RAND_RC1=$?
  LogMsg "The random number returned by rand() is ${RAND_NUMBER1}; the RC of rand() is ${RAND_RC1}"

  RAND_NUMBER2=$( rand ) ; RAND_RC2=$?
  LogMsg "The random number returned by rand() is ${RAND_NUMBER2}; the RC of rand() is ${RAND_RC2}"

  if [ "${RAND_NUMBER1}"x = "${RAND_NUMBER2}"x ] ; then
    LogMsg "The rand() function does not work."
  else
    LogMsg "The rand() function seems to work."
  fi  
  SetWindowTitle "$0 - testing  trap handler"

  LogMsg " -------------------------------------------"

  LogMsg "Installed trap handler are:"
  trap 

  LogMsg "Defined Signal functions:"
  typeset +f | grep MyRoutine
  
  LogMsg "The PID of the current process is $$; the variable LINENO is $LINENO" ;  

  MY_VERBOSE_MODE="${__VERBOSE_MODE}"
  MY_VERBOSE_LEVEL="${__VERBOSE_LEVEL}"
  MY_RT_VERBOSE_LEVEL="${__RT_VERBOSE_LEVEL}"

  LogMsg "-"
  LogMsg "Testing INT Signal Handler"
  __SIGNAL_SIGINT_FUNCTION="MyRoutineSIGINT"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
  SIGINT_CATCHED=${__FALSE}
  __USER_BREAK_ALLOWED=${__FALSE}
  LogMsg "SIGINT_CATCHED is ${SIGINT_CATCHED}; LINENO is $LINENO"
  kill -2 $$

  LogMsg "SIGINT_CATCHED is ${SIGINT_CATCHED}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}


  LogMsg "-"
  LogMsg "Testing HUP Signal Handler -1 -"
  __SIGNAL_SIGHUP_FUNCTION="MyRoutineSIGHUP_1"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
  SIGHUP_CATCHED=${__FALSE}
  LogMsg "__VERBOSE_MODE is ${__VERBOSE_MODE}"
  LogMsg "SIGHUP_CATCHED is ${SIGHUP_CATCHED}; LINENO is $LINENO"
  LogMsg "Expected result:  __VERBOSE_MODE should not change"
  kill -HUP $$
  LogMsg "SIGHUP_CATCHED is ${SIGHUP_CATCHED} (should be ${__TRUE} now)"
  LogMsg "__VERBOSE_MODE is ${__VERBOSE_MODE}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}
 

  LogMsg "-"
  LogMsg "Testing HUP Signal Handler - 2 -"
  __SIGNAL_SIGHUP_FUNCTION="MyRoutineSIGHUP_0"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
    SIGHUP_CATCHED=${__FALSE}
  LogMsg "__VERBOSE_MODE is ${__VERBOSE_MODE}"
  LogMsg "SIGHUP_CATCHED is ${SIGHUP_CATCHED}; LINENO is $LINENO"
  LogMsg "Expected result:  __VERBOSE_MODE should change"
  kill -HUP $$
  LogMsg "SIGHUP_CATCHED is ${SIGHUP_CATCHED} (should be ${__TRUE} now)"   
  LogMsg "__VERBOSE_MODE is ${__VERBOSE_MODE}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}


  LogMsg "-"
  LogMsg "Testing USR1 Signal Handler"
  __SIGNAL_SIGUSR1_FUNCTION="MyRoutineSIGUSR1"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
  SIGUSR1_CATCHED=${__FALSE}
  LogMsg "SIGUSR1_CATCHED is ${SIGUSR1_CATCHED}; LINENO is $LINENO"
  kill -USR1 $$
  LogMsg "SIGUSR1_CATCHED is ${SIGUSR1_CATCHED}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}


  LogMsg "-"
  LogMsg "Testing USR2 Signal Handler"
  __SIGNAL_SIGUSR2_FUNCTION="MyRoutineSIGUSR2"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
  SIGUSR2_CATCHED=${__FALSE}
  LogMsg "SIGUSR2_CATCHED is ${SIGUSR2_CATCHED}; LINENO is $LINENO"
  kill -USR2 $$
  LogMsg "SIGUSR2_CATCHED is ${SIGUSR2_CATCHED}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}

  LogMsg "-"

  MySIGTestRoutine

  LogWarning "Testwarning 1 2 3 "
  LogWarning "Testwarning 4 5 6 "
  
  LogError "TestError 1 2 3 "
  LogError "TestError 4 5 6 "

  LogMsg "-"

  LogMsg " --------------------------------------------------------- "
  LogMsg " Hints: Use \"$0 -H 2 >$0.doc\" "
  LogMsg "        to get the documentation "
  LogMsg "        Use \"$0 -h\" to get the usage help"
  LogMsg "        Use \"$0 -D\" to test the single step mode"
  LogMsg " --------------------------------------------------------- "

  SetWindowTitle "$0 done "

  LogMsg "Testing PrintWithTimeStamp "
  PrintWithTimestamp  iostat 1 15

  LogMsg "Testing TERM Signal Handler (should end the program)"   

  LogMsg "-"
  __SIGNAL_SIGUSR1_FUNCTION="MyRoutineSIGTERM"
  __VERBOSE_MODE=${__TRUE}
  __VERBOSE_LEVEL=2
  __RT_VERBOSE_LEVEL=1
  SIGTERM_CATCHED=${__FALSE}
  LogMsg "SIGTERM_CATCHED is ${SIGTERM_CATCHED}; LINENO is $LINENO"
  kill -TERM $$

# the next code should not be executed.
#
  LogMsg "SIGTERM_CATCHED is ${SIGTERM_CATCHED}"
  __VERBOSE_MODE=${MY_VERBOSE_MODE}
  __VERBOSE_LEVEL=${MY_VERBOSE_LEVEL}
  __RT_VERBOSE_LEVEL=${MY_RT_VERBOSE_LEVEL}


# *** TEST CODE END ***

