#!/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>"

### new.method

certificates.status() # # lists the env 
{
  env | sort
  declare -a
}

certificates.parameter.completion.scenario() {
  source $ONCE_DEFAULT_SCENARIO/scenario.map
  echo ${SCENARIO_MAP_KEYS[@]}
}

certificates.once.scenario.select.completion() {
  certificates.parameter.completion.scenario $1
}

certificates.once.scenario.list() # <?scenario> <?load> # list all scenarios for once
{
  certificates.parameter.completion.scenario $1
}

certificates.once.scenario.select() # <?scenario> <?log=true> # list all scenarios for once
{
  source $ONCE_DEFAULT_SCENARIO/scenario.map
  scenarioName=$( certificates.parameter.completion.scenario | line.split | line.find $1 | line.select 1 )

  found="not found"
  let i=0
  for index in "${SCENARIO_MAP_KEYS[@]}"; do
    #echo looping ${SCENARIO_MAP_KEYS[$index]}  at $i
    if [ "$scenarioName" == "${SCENARIO_MAP_KEYS[$i]}" ]; then
      found=$i
      #echo found at $i
      break;
    fi
    let i++
  done

  if [ "$found" = "not found" ]; then
    warn.log "Scenario \"$scenarioName\" not found!!!"
    return 1
  else

    SELECTED_SCENARIO_NAME=${scenarioName}
    SELECTED_SCENARIO=${SCENARIO_MAP_VALUES[$found]}
    SELECTED_SCENARIO_VERSION=${SCENARIO_MAP_VERSION[$found]}
    SELECTED_SCENARIO_DC_NAME=${SCENARIO_MAP_DC_NAME[$found]}
    SELECTED_SCENARIO_DI_NAME=${SCENARIO_MAP_DI_NAME[$found]}
    SELECTED_SCENARIO_BACKUP=${SCENARIO_MAP_BACKUP[$found]}
    SELECTED_SCENARIO_PATH=$ONCE_SCENARIO$SELECTED_SCENARIO
    if [ -n "$2" ]; then
      set | line.find "^SELECTED_SCENARIO_"
    fi
  fi

}

certificates.once.get.certbot.path() # # gets the path of the once certificat scenario
{
  certificates.once.scenario.select certbot
  echo $SELECTED_SCENARIO_PATH
}

private.certificates.init() # # initialises the env 
{
  if [ -f ~/".once" ]; then
    source ~/.once
    source $ONCE_DEFAULT_SCENARIO/.once
    source $ONCE_DEFAULT_SCENARIO/scenario.map
    declare -a
  fi 
}


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

  Usage:
  $this: command   Parameter and Description"
  this.help
  echo "
  
  Examples
    $this v
    $this init
    ----------
  "
}

certificates.start()
{
  #echo "sourcing init"
  source this
  source line

  private.certificates.init

  # if [ -z "$1" ]; then
  #   status.discover "$@"
  #   return 0
  # fi

  this.start "$@"
}

certificates.start "$@"

