#!/usr/bin/env bash
###############################################################
### Initial install script
### Copyright (C) 2021 ALEXPRO100 (alealexpro100)
### License: GPL v3.0
###############################################################

set -e
BSH_VER=`echo "${BASH_VERSION}" | cut -d'(' -f1`
if [[ "${BSH_VER}" < "4.3.30" ]]; then
  CONFIG_FILE="$(readlink -f "$1")"
else
  CONFIG_FILE="$(realpath "$1")"
fi
if [[ ! -f ./version_install ]]; then
  cd "${BASH_SOURCE[0]%/*}"
  [[ ! -f ./version_install ]] && echo "Failed to locate version_install." && exit 1
fi
echo "Starting ${BASH_SOURCE[0]} $(cat ./version_install)"

#Use libraries
export ALEXPRO100_LIB_LOCATION="${ALEXPRO100_LIB_LOCATION:-./lib/alexpro100_lib.sh}"
# shellcheck disable=SC1091
source ./lib/common/lib_connect.sh
# shellcheck disable=SC1091
source ./lib/common/lib_var_op.sh

has_root || return_err "This script requries root permissions!"

# shellcheck disable=SC1091
[[ -f ./public_parameters ]] && source ./public_parameters
# shellcheck disable=SC1091
[[ -f ./private_parameters ]] && source ./private_parameters

msg_print note "cust_param=[${cust_param}]"
[[ -f "${cust_param}" ]] && source "${cust_param}"

[[ -f /tmp/customize_cst.sh ]] && source /tmp/customize_cst.sh

declare -gx cp_safe="cp -Rf"
function custom_actions() {
  [[ -d "$CUSTOM_DIR" ]] || CUSTOM_DIR=./custom
  if [[ -d $CUSTOM_DIR/rootfs ]]; then
    msg_print note "Copying custom files..."
    $cp_safe "$CUSTOM_DIR/rootfs/." "${dir:?}"
  fi
  msg_print note "arch_chroot_command=[$arch_chroot_command]"
  if [[ -f $CUSTOM_DIR/custom_script.sh && -n "$arch_chroot_command" ]]; then
    msg_print note "cp $CUSTOM_DIR/custom_script.sh $dir/root/custom_script.sh"
    cp "$CUSTOM_DIR/custom_script.sh" "$dir/root/custom_script.sh"
    chmod +x "$dir/root/custom_script.sh"
    msg_print note "Executing custom script..."
    msg_print note "$arch_chroot_command [${dir:?}] bash /root/custom_script.sh start"
    $arch_chroot_command "${dir:?}" bash /root/custom_script.sh
    msg_print note "$arch_chroot_command [${dir:?}] bash /root/custom_script.sh end"
    rm -rf "$dir/root/custom_script.sh"
  fi
}

if [[ -z $CONFIG_FILE ]]; then
  echo "Main install script. Requries profile for installation."
  echo "Example: $0 profile_install.sh"
  exit 1
fi

msg_print note "check CONFIG_FILE=[$CONFIG_FILE] files..."

if [[ -f "$CONFIG_FILE" ]]; then
  # shellcheck disable=SC1090
  source "$CONFIG_FILE" || return_err "Failed to use profile!"
  
  msg_print note "parse_arch [$(uname -m)]"

  parse_arch "$(uname -m)"
  # shellcheck disable=SC1090
  msg_print note "arch=[${arch}]"
  msg_print note "source ./lib/distr/${distr:?}/distr_actions.sh"
  source "./lib/distr/${distr:?}/distr_actions.sh"
  msg_print note "run  custom_actions..."
  custom_actions
  msg_print note "end  custom_actions..."
  if ! [[ $OP_MODE == "debug" ]]; then
	  if is_function profile_end_action; then
	    profile_end_action || return_err "Couldn't do profile_end_action."
	  fi
  fi  
  msg_print note "Installed $distr with $CONFIG_FILE to $dir."
else
  return_err "No file $CONFIG_FILE!"
fi

# =)
echo "Script succesfully ended its work. Have a nice day!"
exit 0
