#!/bin/bash
# Info: Script to install/create/delete/update kubectl from file settings 
# Author: JesusPerezLorenzo 
# Release: 1.0 
# Date: 12-10-2024

USAGE="install-provisioning.sh install | update | remove"
[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1
debug=${-//[^x]/}

[ -r "env-provisioning" ] && . env-provisioning

function _install_reqs {
    # KCL requires gcc and crt.o 
    echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selections
    DEBIAN_FRONTEND=noninteractive sudo apt-get -y -qq install gcc-multilib 2>/dev/null 
}

_install_reqs

ROOT_PROVISIONING="${ROOT_PROVISIONING:-/usr/local}"
ROOT_BIN_PROVISIONING="${ROOT_BIN_PROVISIONING:-/usr/local/bin}"
PROVISIONING_RUN_MODE="${PROVISIONING_RUN_MODE:-mode-ui}"

if [ -r "provisioning.tar.gz" ] ; then
  tar xzf provisioning.tar.gz 
  rm -f provisioning.tar.gz 
fi
if [ ! -r "provisioning" ] ; then 
  echo "Error: path 'provisioning' not found"
   exit 1
fi
[ -d "$ROOT_PROVISIONING/provisioning" ] && sudo rm -r "$ROOT_PROVISIONING/provisioning"
sudo rm -f "$ROOT_BIN_PROVISIONING/provisioning"
id_u=$(id -u)
root_path=$HOME
[ "$it_u" != 0 ] && root_path="/root" 

# Need this to Nushell to be activated with plugins
if [ -d "config-nushell" ] ; then 
  mkdir -p $HOME/.config/nushell
  cp config-nushell/* $HOME/.config/nushell
  if [ "$root_path"  != "$HOME" ] ; then 
    sudo mkdir -p $root_path/.config/nushell
    sudo cp config-nushell/* $root_path/.config/nushell
  fi
  if [ -d "$USER_HOME" ] && [ -n "$USER_NAME" ] && [ "$USER_HOME" != "$HOME" ] ; then 
    sudo mkdir -p $USER_HOME/.config/nushell
    sudo cp config-nushell/* $USER_HOME/.config/nushell 
    sudo chown -R $USER_NAME $USER_HOME/.config
  fi
fi
if [ -n "$debug" ] ; then 
  bash -x ./installer "$ROOT_PROVISIONING" "$ROOT_BIN_PROVISIONING" "$PROVISIONING_RUN_MODE"
else 
  ./installer "$ROOT_PROVISIONING"
fi
has_provisioning=$(type -P provisioning) 
[ -n "$has_provisioning" ] && echo "provisioning installed" 

# Need this for Nushell with plugins copied to $HOME and $USER_HOME
if [ "$root_path" != "$HOME" ] ; then 
  sudo cp $root_path/.config/nushell/plugin.nu $HOME/.config/nushell
  sudo chown -R $(whoami) $HOME/.config/nushell/plugin.nu
fi
if [ -d "$USER_HOME" ] && [ -n "$USER_NAME" ] && [ "$USER_HOME" != "$HOME" ] ; then 
  sudo cp $root_path/.config/nushell/plugin.nu $USER_HOME/.config/nushell 
  sudo chown $USER_NAME $USER_HOME/.config/nushell/plugin.nu
fi
sudo chmod 755 $root_path/.config/provisioning 2>/dev/null
if [ -d "$USER_HOME" ] && [ -n "$USER_NAME" ]; then 
  mkdir -p $USER_HOME/.config
  if sudo cp -pr $root_path/.config/provisioning $USER_HOME/.config ;  then 
      sudo chown -R $USER_NAME $USER_HOME/.config
      sudo chmod 755 $USER_HOME/.config/provisioning
  fi
fi
if [ ! -d "$HOME/.config/provisioning" ] ; then 
  mkdir -p $HOME/.config
  if sudo cp -pr $root_path/.config/provisioning $HOME/.config ;  then 
      sudo chown -R $(whoami) $HOME/.config
      sudo chmod 755 $HOME/.config/provisioning
  fi
fi
