#!/usr/local/bin/bash
#export PS4='\e[90m+${LINENO} in ${#BASH_SOURCE[@]}>${FUNCNAME[0]}:${BASH_SOURCE[@]##*/} \e[0m'
#set -x 

this.start()                # processes the initial command line arguments and calls the corresponding functions 
{
  COMMANDS="$@"
  #stop.log "once.discover $COMMANDS"
  #once.discover "$@"
  source init 
  this.mode

  if [ -z "$1" ]; then
    console.log "no parameters! stage to: $ONCE_STATE"
    console.log "$this: Bye"
  fi

  while [ -n $1 ]; do
    debug.log "start 1: -$1-"
    command=$1
    echo -e "\e[1;96mcommand is $command: \e[0m $this.$command $@"
    shift
    case $command in
      call)
        shift
        "$@"
        ;;
      discover)
        once.discover
        if [ "$ONCE_STATE" = "disvocer" ]; then
          ONCE_STATE=check.installation
          once.stage
        fi
        ;;
      start)
        once.server.start "$@"
        ;;
      X)
        echo "Set DEBUG to X"
        shift
        echo "RETRUN $1"
        RETURN=$1
        ;;
      '')
        debug.log "$0: EXIT"
        #exit 0
        return 0 
        ;;
      help)
        this.help "$@"
        ;;
      *)
        if [ -z "$this" ]; then
          warn.log "stage to: $command $@"
          $command "$@"
        else
        # if [ -z "$this" ]; then
        #   this="this"
        # fi
          debug.log "$this.stage to: $this.$command"
          $this.$command "$@"
        fi
    esac

    while [ ! "$RETURN" = "$1" ]; do
      shift
      debug.log "shift:  -Return:$RETURN-  -$1- -command=$COMMANDS-  =$@="
      if [ -z "$1" ]; then
        debug.log "force stop"
        RETURN=
        exit 0
      fi
    done
    debug.log "found RETURN=$1"
  done
}

this.help()                 # prints a list of all commands for $this 
{
  local detail=$1
  if [ -n "$detail" ]; then
    shift
  fi
  grep "^$this\.$detail.*()" $This | sort
}

this.mode() # checks if sourced
{
  local len=${#BASH_LINENO[@]}
  depth=len
  echo depth=$depth

  local Current=${BASH_SOURCE[0]}
  local Caller=${BASH_SOURCE[$depth-1]}
  local current=$(basename $Current)
  local caller=$(basename $Caller)
  

  # echo "\$0: $0"
  # echo "\$Current: $Current"
  # echo "\$current: $current"
  # echo "\$Caller: $Caller"
  # echo "\$caller: $caller"
  # echo "\$this: $this"
  # echo "\$BASH_COMMAND: $BASH_COMMAND"
  # echo "Functions: ${FUNCNAME[@]}"
  # echo "bash lineno: ${BASH_LINENO[@]}"
  # echo "bash source: ${BASH_SOURCE[@]}"
  # echo "bash depth: ${#BASH_SOURCE[@]}"
  # echo ""
  # echo "current   Functions: ${FUNCNAME[$depth]}"
  # echo "current bash source: ${BASH_SOURCE[$depth]}"

  # if [ -z "$caller" ]; then
  #   caller="this"
  # fi

  # local startFunc="$caller.start"
  # this.functionExists $startFunc
}

this.functionExists()
{

  local startFunc="$1"
  shift
  RETURN=$1

  if [ "$(type -t $startFunc)" = "function" ]; then
    #echo "$startFunc exists"
    return 0
  else
    #echo "$startFunc does not exist"
    return 1
  fi  
}

this.start "$@"
