#!/bin/bash

read_param "" "$M_LANG_SYSTEM" "$LANG_SYSTEM" LANG_SYSTEM menu_var "$(gen_menu < <(echo -e "en_US.UTF-8\n$LANG_SYSTEM" | uniq))"
read_param "" "$M_HOSTNAME" "${hostname:?}" hostname text

read_param "" "root $M_PASS"  "${root_passwd:?}"  root_passwd secret_empty
read_param "" "$M_USER" "${user_name:?}" user_name text
read_param "" "$M_SHELL" "${user_shell:?}" user_shell text
read_param "" "$M_PASS"  "${passwd:?}"  passwd secret_empty
if [[ -z $passwd ]]; then
  add_var "declare -gx" passwd "${passwd_default:?}"
  print_param warning "$M_PASS_NO $passwd."
fi
if mountpoint -q "${dir:?}"; then
  if [[ $LIVE_MODE == "1" ]]; then
    ECHO_MODE_TMP=$ECHO_MODE
    ECHO_MODE=auto
  fi
  read_param "" "$M_FSTAB" "${fstab:?}" fstab yes_or_no
  read_param "" "$M_BOOTLOADER" "${bootloader:?}" bootloader yes_or_no
  # shellcheck disable=SC2154
  if [[ $bootloader == "1" ]]; then
   ECHO_MODE_BAK=$ECHO_MODE
   if [[ $SETUP_MODE == "manual" ]]; then
	ECHO_MODE=${G_ECHO_MODE}
   fi
    read_param "" "$M_KERNEL" "${kernel:?}" kernel yes_or_no
    ECHO_MODE=${ECHO_MODE_BAK}
    if [[ -d /sys/firmware/efi/efivars ]]; then
      BOOTLOADER_TYPE_DEFAULT=${BOOTLOADER_TYPE_DEFAULT:-uefi}
    else
      BOOTLOADER_TYPE_DEFAULT=${BOOTLOADER_TYPE_DEFAULT:-bios}
    fi
    read_param "" "$M_BOOTLOADER_TYPE" "${bootloader_type:?}" bootloader_type menu_var "$(gen_menu < <(echo -e "bios\nuefi"))"
    if [[ ${bootloader_type:?} == "uefi" && $(findmnt -funcevo FSTYPE "$dir/boot") != vfat ]]; then
      print_param error "$M_BOOTLOADER_UEFI_VFAT_NO \"$dir/boot\"!"
    fi
    read_param "" "$M_BOOTLOADER_NAME" "${bootloader_name:?}" bootloader_name menu_var "$(gen_menu < <(echo -e "grub2"))"
    [[ $bootloader_type == bios ]] && read_param "" "$M_BOOTLOADER_PATH" "${bootloader_bios_place:-$(findmnt -funcevo SOURCE "$dir")}" bootloader_bios_place text
    if [[ $LIVE_MODE == "1" ]]; then
      ECHO_MODE=$ECHO_MODE_TMP
      unset ECHO_MODE_TMP
    else
      read_param "" "$M_BOOTLOADER_REMOVABLE" "${removable_disk:?}" removable_disk no_or_yes
    fi
    read_param "" "$M_ADD_SOFT" "${add_soft:?}" add_soft yes_or_no
  fi
else
  add_var "declare -gx" "bootloader" "0"
  read_param "" "$M_KERNEL" "${kernel:?}" kernel no_or_yes
  read_param "" "$M_ADD_SOFT" "${add_soft:?}" add_soft no_or_yes
fi

parse_arch "$(uname -m)"

print_param note "check ./lib/distr/${distr:?}/customize_cst.sh"
[[ -f "./lib/distr/${distr:?}/customize_cst.sh" ]] && source "./lib/distr/${distr:?}/customize_cst.sh"
cust="${distr}_customize"
cust_sh="${!cust}"

print_param note "check cust[${cust}] cust_sh[${cust_sh}]"
if [[ -n "${cust_sh}" ]]; then
    print_param note "distr custom profile setting[${cust_sh}]"
    declare error_msg
    if is_url "$cust_sh"; then
        wget -O /tmp/customize_cst.sh "$cust_sh" || error_msg="Couldn't download $cust_sh."
    else
        cp -a "$cust_sh" /tmp/customize_cst.sh || error_msg="Couldn't copy $cust_sh."
    fi
    if [[ -z $error_msg ]]; then
       source /tmp/customize_cst.sh || error_msg="$M_MODE_AUTO_FAIL"
    fi
    [[ -z $error_msg ]] || print_param error "$error_msg"
    unset error_msg
fi
unset cust
unset cust_sh
