#!/bin/bash


if [[ -e /dev/hda ]]; then
    PART_ROOT=hda
elif [[ -e /dev/sda ]]; then
    PART_ROOT=sda
elif [[ -e /dev/vda ]]; then
    PART_ROOT=vda
fi

export AP100_DBG_ON=0

add_var "declare -gx" "SETUPLINUX" "1"
add_var "declare -gx" "SETUPLOG" "/tmp/setup.log"
add_var "declare -gx" "DISK_PART_DO" "1"
add_var "declare -gx" "DISK_PART_ROOT" "$PART_ROOT"
add_var "declare -gx" "DISK_PART_MODE" "auto"
add_var "declare -gx" "DISK_FORMAT_TYPE" "ext4"
add_var "declare -gx" "DEFAULT_DISTR" "debian_cst"
add_var "declare -gx" "repo_mode" "remoto"
add_var "declare -gx" "pubkey" "http://nas-server/dev/mirrors/pubkey/my_pub_key.asc"
add_var "declare -gx" "old_gpgkey" "http://nas-server/dev/mirrors/pubkey/debian-archive-removed-keys.gpg"
add_var "declare -gx" "debian_add_i386" "1"
##add_var "declare -gx" "ECHO_MODE" "auto"

add_var "declare -gx" "dir" "/mnt/mnt"
add_var "declare -gx" "distr" "${distr:-debian_cst}"
add_var "declare -gx" "LANG_SYSTEM" "en_US.UTF-8"
add_var "declare -gx" "hostname" "${distr}-$RANDOM"
add_var "declare -gx" "root_passwd" "129921"
add_var "declare -gx" "user_name" "jin"
add_var "declare -gx" "user_shell" "/bin/bash"
add_var "declare -gx" "passwd" "jin123"
add_var "declare -gx" "fstab" "1"
add_var "declare -gx" "bootloader" "1"
add_var "declare -gx" "kernel" "1"
add_var "declare -gx" "bootloader_type" "bios"
add_var "declare -gx" "bootloader_name" "grub2"
add_var "declare -gx" "bootloader_bios_place" "/dev/${PART_ROOT}"
add_var "declare -gx" "add_soft" "1"
add_var "declare -gx" "arch" "i386"
add_var "declare -gx" "kernel_type" "virtual"
add_var "declare -gx" "backports_kernel" "0"
add_var "declare -gx" "networkmanager" "1"
add_var "declare -gx" "ssh" "1"
add_var "declare -gx" "pulseaudio" "0"
add_var "declare -gx" "bluetooth" "0"
add_var "declare -gx" "printers" "0"
add_var "declare -gx" "graphics" "0"
add_var "declare -gx" "graphics_type" "xorg"
add_var "declare -gx" "desktop_type" "DE"
add_var "declare -gx" "desktop_de" "xfce4"

## add_var "declare -gx" "preinstall" "locales"
add_var "declare -gx" "postinstall" "usbutils pciutils dosfstools software-properties-common screen htop rsync bash-completion"

add_var "declare -gx" "debian_cst_customize" "http://nas-server/dev/setup/distr_opt/debian_cst/customize_cst.sh"
add_var "declare -gx" "alpine_customize" "http://nas-server/dev/setup/distr_opt/alpine/customize_cst.sh"

add_var "declare -gx" "mirror_alpine_local" "http://nas-server/dev/mirrors/alpine"

declare -A debian_repos_add=( [wine]='http://nas-server/dev/mirrors/debian/ $version_debian main' )
old_gpgkey="http://nas-server/dev/mirrors/pubkey/debian-archive-removed-keys.gpg"

END_ECHO_MODE=auto
#END_ECHO_MODE=cli
function do_end_action() {
    # print_param note "ECHO_MODE=${ECHO_MODE}"
    case $1 in
        0) msg_print note "Rebooting...";reboot;;
        1) msg_print note "Powering off..."; poweroff;;
        2) msg_print note "${M_REBOOT_M:-"Complete!"}"; bash;;
    esac
    exit 0
}
export -f do_end_action

add_var "declare -gx" "distr_options" "http://nas-server/dev/setup/distr_opt/${distr}/distr_options.sh"
add_var "declare -gx" "distr_actions" "http://nas-server/dev/setup/distr_opt/${distr}/distr_actions.sh"
add_var "declare -gx" "distr_rootfs" "http://nas-server/dev/setup/distr_opt/${distr}/rootfs_script.sh"

if [[ -n ${OP_STEP} && ${OP_STEP} == "DISTR_FINISH" ]]; then
	if [[ -n ${distr_options} ]]; then
		print_param note "distr_options setting[${distr_options}]"
		declare error_msg
		if is_url "${distr_options}"; then
			if ! check_online; then
				print_param warning "$M_HOST_OFFLINE"
				for interface in $(list_files "/sys/class/net/" -type l | sed '/lo/d'); do
					interface_setup_dhcp "$interface"
				done
			fi
			wget -O /tmp/distr_options.sh "${distr_options}" || error_msg="Couldn't download ${distr_options}."
		fi
		if [[ -z $error_msg ]]; then

			# shellcheck disable=SC1090
			print_param note "change [${distr_options}] => ./lib/distr/${distr:?}/distr_options.sh"
			mv  "./lib/distr/${distr:?}/distr_options.sh"  "./lib/distr/${distr:?}/distr_options_org.sh"
			mv /tmp/distr_options.sh  "./lib/distr/${distr:?}/distr_options.sh"
		fi
		[[ -z $error_msg ]] || print_param error "$error_msg"
		unset error_msg
	fi
	if [[ -n ${distr_actions} ]]; then
		print_param note "distr_actions setting[${distr_actions}]"
		declare error_msg
		if is_url "${distr_actions}"; then
			if ! check_online; then
				print_param warning "$M_HOST_OFFLINE"
				for interface in $(list_files "/sys/class/net/" -type l | sed '/lo/d'); do
					interface_setup_dhcp "$interface"
				done
			fi
			wget -O /tmp/distr_actions.sh "${distr_actions}" || error_msg="Couldn't download ${distr_actions}."
		fi
		if [[ -z $error_msg ]]; then

			# shellcheck disable=SC1090
			print_param note "change [${distr_actions}] => ./lib/distr/${distr:?}/distr_actions.sh"
			mv  "./lib/distr/${distr:?}/distr_actions.sh"  "./lib/distr/${distr:?}/distr_actions_org.sh"
			mv /tmp/distr_actions.sh  "./lib/distr/${distr:?}/distr_actions.sh"
		fi
		[[ -z $error_msg ]] || print_param error "$error_msg"
		unset error_msg
	fi
	if [[ -n ${distr_rootfs} ]]; then
		print_param note "distr_rootfs setting[${distr_rootfs}]"
		declare error_msg
		if is_url "${distr_rootfs}"; then
			if ! check_online; then
				print_param warning "$M_HOST_OFFLINE"
				for interface in $(list_files "/sys/class/net/" -type l | sed '/lo/d'); do
					interface_setup_dhcp "$interface"
				done
			fi
			wget -O /tmp/rootfs_script.sh "${distr_rootfs}" || error_msg="Couldn't download ${distr_rootfs}."
		fi
		if [[ -z $error_msg ]]; then

			# shellcheck disable=SC1090
			print_param note "change [${distr_rootfs}] => ./lib/distr/${distr:?}/rootfs_script.sh"
			mv  "./lib/distr/${distr:?}/rootfs_script.sh"  "./lib/distr/${distr:?}/rootfs_script_org.sh"
			mv /tmp/rootfs_script.sh  "./lib/distr/${distr:?}/rootfs_script.sh"
		fi
		[[ -z $error_msg ]] || print_param error "$error_msg"
		unset error_msg
	fi
fi



