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


NU_FILES="
core/nulib/libremote.nu
core/nulib/lib_provisioning/setup/config.nu
"

WK_FILE=/tmp/make_config_provisioning.nu

[ -r "$WK_FILE" ] && rm -f "$WK_FILE"

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

export NU=$(type -P nu)
[ -z "$NU" ]  && echo "Nu shell not found" && exit 1

export PROVISIONING=${PROVISIONING:-/usr/local/provisioning}
export PROVISIONING_DEBUG=false

for it in $NU_FILES
do
   [ -r "$PROVISIONING/$it" ] && cat $PROVISIONING/$it >> $WK_FILE
done

echo "
install_config \"reset\" --context
" >> $WK_FILE

NU_ARGS=""
CMD_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

[ -d "$PROVISIONING_USER_CONFIG" ] && rm -r  "$PROVISIONING_USER_CONFIG"
[ -r "$PROVISIONING_CONTEXT_PATH" ] && rm -f  "$PROVISIONING_CONTEXT_PATH"

nu $NU_ARGS $WK_FILE $CMD_ARGS

rm -f $WK_FILE
