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

#echo "starting: $0 <LOG_LEVEL=$1>"

function mycmd.1() {
  console.log "hello ${FUNCNAME[0]} ..."
  
  echo "$1 should be \"a\"" 
  a=$1 
  shift
  b=$1
  echo "$1 should be \"b\""
  shift
  c=$1
  echo "$1 should be \"c\""
  shift

  create.result 0 "$c should be \"c\"" $1
  return $(result save)
}
function mycmd.2() {
  debug.log "hello ${FUNCNAME[0]} ..."
  echo "$1 should be \"d\""
  shift
  echo "$1 should be \"e\""
  local e=$1
  shift
  create.result 1 "warning message $e should be \"e\"" "$1"
  return $(result)
}
function mycmd.3() {
  console.log "hello ${FUNCNAME[0]} ..."
  echo "$1 should be \"f\""
  shift
  last=$1
  echo "$1 should be \"g\""
  shift

  create.result 3 "error message $last should be \"g\"" "$1" >/dev/null
  return $(result)
}

function mycmd.4() {
  grep -w '$1' /root/scripts/once
  export st=$?
  if [ $st = 1 ] && [ $1 != 'cmd']; then
    once $1
    shift
  else
    once cmd $1
    shift
  fi
  RETURN=$1
}

mycmd.usage()
{
  local this=${0##*/}
  echo "You started 
$0

  Usage:
  $this: command <parameter>
      1         parameter should be: a b c
      2                              d e  
      3                              e f
      feature1  log-level
  
  Examples
    $this 1 a b c 3 e f
    $this 2 d e 1 a b c
    $this feature1 5
  "
}

mycmd.feature1()
{
  log.level $1
  important.log "Feature ${FUNCNAME[0]} $@"
  mycmd.feature2
}
mycmd.feature2()
{
  important.log "..Feature ${FUNCNAME[0]} $@"
  this.caller
  console.log "caller: $RESULT"

  this.caller.function
  console.log "caller Function: $RESULT"

  mycmd.feature3
}
mycmd.feature3()
{
  important.log "....Feature ${FUNCNAME[0]} $@"

  this.caller
  console.log "caller: $RESULT"

  this.caller.function
  console.log "caller Function: $RESULT"

  this.isSourced
  console.log "is sourced: $RESULT"

  info.log "function stack: ${FUNCNAME[@]}"
  info.log "source   stack: ${BASH_SOURCE[@]##*/}"
}

mycmd.logging.tests() {
  #exec 2>>$CONFIG_PATH/error.txt
  console.log "console"
  xxxxx  crteat an error intensionally
  error.log   "error"

}
mycmd.pipe.tests() {
  mycmd.logging.tests | tee $CONFIG_PATH/result.txt
}

mycmd.check.test.results() {
  console.log "${GREEN}Result${NORMAL}"
  cat $CONFIG_PATH/result.txt
  console.log "${RED}Errors${NORMAL}"
  cat $CONFIG_PATH/error.txt
}

mycmd.clean.logfiles() {
  console.log "${GREEN}delete Result${NORMAL}"
  rm $CONFIG_PATH/result.txt
  console.log "${RED}delete Errors${NORMAL}"
  rm $CONFIG_PATH/error.txt
}

mycmd.execute.logging.test() {
  mycmd.pipe.tests
  mycmd.check.test.results
}

mycmd.tab() {
  echo "$*"
}

mycmd.tab.completion() {
  compgen -o default -o bashdefault -o dirnames "$1"
}

mycmd.tab.files() { 
  echo "file: $*" 
}

mycmd.tab.files.completion() { 
  compgen -d  "$1" 
}

mycmd.tab.env() { 
  echo "exported ENV variables: $*"
  echo "$1=${!1}"
}
mycmd.tab.env.completion() { 
  compgen -e  "$1" 
}

mycmd.tab.user() { 
  echo "$*"
}
mycmd.tab.user.completion() { 
  compgen -u  "$1" 
}

mycmd.tab.groups() { 
  echo "$*"
}
mycmd.tab.groups.completion() { 
  compgen -g  "$1" 
}

mycmd.tab.buildinCommands() { 
  echo "$*"
}
mycmd.tab.buildinCommands.completion() { 
  compgen -c  "$1" 
}

mycmd.tab.jobs() { 
  echo "$*"
}
mycmd.tab.jobs.completion() { 
  compgen -j  "$1" 
}

mycmd.tab.variables() { 
  echo "all shell variables: $*"
}
mycmd.tab.variables.completion() { 
  compgen -v  "$1" 
}

mycmd.tab.alias() { 
  echo "aliases: $*"
}
mycmd.tab.alias.completion() { 
  compgen -a  "$1" 
}

mycmd.tab.folders() { 
  echo "folders: $*"
}
mycmd.tab.folders.completion() { 
  compgen -d  "$1" 
}

### call
#  config completion.discover mycmd tab.xxx   
#  to see all possible values....TODO fix that is also works without a value 


mycmd.scope()
{
  declare -p
  declare -f | grep "^[^ ]* ()" | cut -d ' ' -f1
}

mycmd.start()
{
  #echo "sourcing init"
  source this
  this.start "$@"
}

mycmd.start "$@"

