#!/bin/bash

value=$1

if [ ! -n "$value" ]; then
    value=ON
fi

if [ -z "$DEBUG" ]; then
	export DEBUG=$value
    echo Debug was off. Now $DEBUG. 
    echo \(remember to use \". debug\" to affect the current shell\)
else
    echo Debug was $DEBUG. Now off.
	export DEBUG=
fi



export PS4='+${LINENO}: '


debug.version()              # prints out the hard coded version tag of $This 
{
 console.log "$0 version: 20210305 15:45"
}

onError() {
  rc=$?
  err.log "ERR at line ${LINENO} (rc: $rc)"
  step
  exit $rc
}

  function stepDebugger()     # steppes through the once.sh script 
  {
      export PS4='+${LINENO}: '
      echo " function ${FUNCNAME[0]}($1)"
      if [ "$1" = "ON" ]; then
          echo step debugger is now ON
          trap step DEBUG
      fi
      if [ "$1" = "OFF" ]; then
            trap "" DEBUG
            DEBUG=OFF
            trap
            echo "cleard TRAP DEBUG"
            export DEBUG=
            set +x
      fi
  }

  function step {
    set +x
    if [[ -n "$BASH_COMMAND" ]]; then
      echo "<-----------------------------------------"
      echo \> \'$BASH_COMMAND\'
      read -p '' CONT

      #warn.log "Cont: $CONT"

      if [[ ! "$CONT" = "" ]]; then
          case $CONT in
              h)
                  echo "
                  h     this help
                  n     next command   
                  ENTER silent next command

                  d     toggle debug messages
                  s     continue silently
                  c     in full debug
                  p     print PATH
                  ll    list dir
                  cd    changing to entered path
                  r     tree root
                  home  tree home
                  i     check eamd
                  eamd  tree workspace

                  cmd   runn command (BE CAREFULL)

                  all other commands exit
                  "
                  step
                  ;;
              p)
                  console.log "PATH=$PATH"
                  step
                  ;;
              s)
                  trap "" DEBUG
                  trap
                  echo "cleard TRAP DEBUG"
                  export DEBUG=
                  set +x
                  ;;
              ll)
                  pwd
                  ls -alF
                  step
                  ;;
              r)
                  tree -aL 2 /root
                  step
                  ;;
              home)
                  tree -aL 2 /home
                  step
                  ;;
              eamd)
                  tree -aL 2 /$defaultWorkspace/..
                  step
                  ;;
              i)
                  eamd check
                  step
                  ;;
              cd)
                  read -p 'cd to?   >' CD_DIR
                  cd $CD_DIR
                  step
                  ;;
              cmd)
                  read -p 'command?  BE CAREFULL >' command
                  set -x
                  $command
                  step
                  ;;

              c)
                  trap "" DEBUG
                  trap
                  echo "cleard TRAP DEBUG"
                  export DEBUG=
                  set -x
                  ;;
              d)
                if [ "ON" = "$DEBUG" ]; then 
                  DEBUG=OFF
                else
                  DEBUG=ON
                fi
                warn.log "Toggeld DEBUG to $DEBUG"
                  step
                  ;;
              n)
                echo "      next..."
                set -x
                ;;
              *)
                  warn.log "Exiting prematurely because of command $CONT"
                  exit 1
                  ;;
          esac
      else
        set +x
      fi
      #warn.log "$CONT"
      #set -x
    fi
  }

makeAnError() 
{
  echo "i will make an error now"
  asldkj;asdf
  echo "ups, what happend ;)"
}

debug.log() {
    if [ "ON" = "$DEBUG" ]; then
        test -t 1 && tput setf 7     
        echo "- $*"
        test -t 1 && tput sgr0 # Reset terminal
    fi
}

console.log() {
    test -t 1 && tput bold; tput setf 7                                            ## white bold
    echo ">  $*"
    test -t 1 && tput sgr0 # Reset terminal
}
err.log() {
    test -t 1 && tput bold; tput setf 4  
    echo "ERROR>  $*"
    test -t 1 && tput sgr0 # Reset terminal
}
warn.log() {
    test -t 1 && tput bold; tput setf 6                                    ## white yellow
    echo "> WARNING $*"
    test -t 1 && tput sgr0 # Reset terminal
}
