#!/usr/bin/env bash
# Info: Script to run Provisioning
# Author: JesusPerezLorenzo 
# Release: 1.0.5
# Date: 15-04-2024

set +o errexit
set +o pipefail

export NU=$(type -P nu)

_release() { 
  grep "^# Release:" "$0" | sed "s/# Release: //g"
}

export PROVISIONING_VERS=$(_release)

set -o allexport 
## shellcheck disable=SC1090
[ -n "$PROVISIONING_ENV" ] && [ -r "$PROVISIONING_ENV" ] && source "$PROVISIONING_ENV"
[ -r "../env-provisioning" ] && source ../env-provisioning
[ -r "env-provisioning" ] && source ./env-provisioning
#[ -r ".env" ] && source .env set 
set +o allexport

export PROVISIONING=${PROVISIONING:-/usr/local/provisioning}
PROVIISONING_WKPATH=${PROVIISONING_WKPATH:-/tmp/tmp.}

RUNNER="provisioning"

[ "$1" == "" ] && shift

[ -z "$NU" ] || [ "$1" == "install" ] || [ "$1" == "reinstall" ] || [ "$1" == "mode" ] && exec bash $PROVISIONING/core/bin/install_nu.sh $PROVISIONING $1 $2

[ "$1" == "rmwk" ] && rm -rf "$PROVIISONING_WKPATH"* && echo "$PROVIISONING_WKPATH deleted"  && exit 
[ "$1" == "-x" ] &&  debug=-x && export PROVISIONING_DEBUG=true  && shift
[ "$1" == "-xm" ] &&  export PROVISIONING_METADATA=true  && shift
[ "$1" == "nu" ] &&  export PROVISIONING_DEBUG=true 
[ "$1" == "--x" ] && set -x && debug=-x && export PROVISIONING_DEBUG=true  && shift
[ "$1" == "-i" ] || [ "$2"  == "-i" ] && echo "$(basename "$0") $(grep "^# Info:" "$0" | sed "s/# Info: //g") " && exit 
[ "$1" == "-v" ] || [ "$2"  == "-v" ] &&  _release && exit 
CMD_ARGS=$@
case "$1" in 
  "setup") 
      export PROVISIONING_MODULE="setup"
      shift
      CMD_ARGS=$@
    ;;  
  -mod)
      export PROVISIONING_MODULE=$(echo "$2" | sed 's/ //g' | cut -f1 -d"|") 
      PROVISIONING_MODULE_TASK=$(echo "$2" | sed 's/ //g' | cut -f2 -d"|") 
      [ "$PROVISIONING_MODULE" == "$PROVISIONING_MODULE_TASK" ] && PROVISIONING_MODULE_TASK=""
      shift 2
      CMD_ARGS=$@
      ;;
esac
NU_ARGS=""

DEFAULT_CONTEXT_TEMPLATE="default_context.yaml"
case "$(uname | tr '[:upper:]' '[:lower:]')" in
  linux) PROVISIONING_USER_CONFIG="$HOME/.config/provisioning/nushell" 
      PROVISIONING_CONTEXT_PATH="$HOME/.config/provisioning/$DEFAULT_CONTEXT_TEMPLATE"

      ;;
  darwin) PROVISIONING_USER_CONFIG="$HOME/Library/Application Support/provisioning/nushell" 
      PROVISIONING_CONTEXT_PATH="$HOME/Library/Application Support/provisioning/$DEFAULT_CONTEXT_TEMPLATE" 
      ;;
  *) PROVISIONING_USER_CONFIG="$HOME/.config/provisioning/nushell" 
      PROVISIONING_CONTEXT_PATH="$HOME/.config/provisioning/$DEFAULT_CONTEXT_TEMPLATE"
      ;;
esac

if [ ! -d "$PROVISIONING_USER_CONFIG" ]  || [ ! -r "$PROVISIONING_CONTEXT_PATH" ] ; then 
  [ ! -x "$PROVISIONING/core/nulib/provisioning setup" ] && echo "$PROVISIONING/core/nulib/provisioning setup  not found"  && exit 1
  cd "$PROVISIONING/core/nulib"
  ./"provisioning setup"
  echo ""
  read -p "Use [enter] to continue or [ctrl-c] to cancel"
fi
[ ! -r "$PROVISIONING_USER_CONFIG/config.nu" ] && echo "$PROVISIONING_USER_CONFIG/config.nu not found" && exit 1
[ ! -r "$PROVISIONING_USER_CONFIG/env.nu" ] && echo "$PROVISIONING_USER_CONFIG/env.nu not found" && exit 1

NU_ARGS=(--config "$PROVISIONING_USER_CONFIG/config.nu" --env-config "$PROVISIONING_USER_CONFIG/env.nu")
export PROVISIONING_ARGS="$CMD_ARGS"  NU_ARGS="$NU_ARGS"
#export NU_ARGS=${NU_ARGS//Application Support/Application\\ Support}

if [ -n "$PROVISIONING_MODULE" ] ; then 
  if [[ -x $PROVISIONING/core/nulib/$RUNNER\ $PROVISIONING_MODULE ]] ; then
        $NU "${NU_ARGS[@]}" "$PROVISIONING/core/nulib/$RUNNER $PROVISIONING_MODULE" $PROVISIONING_MODULE_TASK $CMD_ARGS
  else 
    echo "Error \"$PROVISIONING/core/nulib/$RUNNER $PROVISIONING_MODULE\" not found"
  fi
else
    $NU "${NU_ARGS[@]}" "$PROVISIONING/core/nulib/$RUNNER" $CMD_ARGS
fi
