#!/bin/bash

# this procedure is meant to be inherited from
var_DEFAULTFS="/boot:32:ext2:+ swap:256:swap /:7500:ext3 /home:*:ext3"
var_TARGET_DIR="/mnt" # When overriding this, do _not_ add a trailing /.  It's not needed and maybe you could even break something
var_RUNTIME_REPOSITORIES= # array like this ('name1' 'location of repo 1' ['name2' 'location of repo2',..])
var_RUNTIME_PACKAGES=
var_PKG_FILE=$RUNTIME_DIR/aif-package-list # not used by default in base/interactive. can be used by custom procedures or profiles for the automatic procedure
var_MIRRORLIST="/etc/pacman.d/mirrorlist"
var_UI_TYPE="cli" # set to cli or dia for dialog
var_ARCH=`uname -m` #i686 or x86_64. NOTE: this assumes you want to install the same arch as the installation environment you're using. maybe we could decouple those someday..
[ -z "$var_ARCH" ] && die_error "Could not determine your architecture"
grubmenu="$var_TARGET_DIR$grubmenu"
syslinuxmenu="$var_TARGET_DIR$syslinuxmenu"

###### Phases ( can be overridden by more specific procedures) ######
phase_preparation=(\
	configure \
	intro \
	sysprep \
	select_source \
	runtime_network \
	runtime_repositories \
	runtime_packages)

phase_basics=(\
	set_clock \
	prepare_disks)

phase_system=(\
	package_list \
	install_packages \
	configure_system \
	install_bootloader)

phase_finish=(msg_report)



###### Workers ( can be overridden by more specific procedures) ######
worker_intro ()
{
	ask_yesno "This is a low-interactivity 'common defaults' installation, not meant for direct invocation.  Do you really want to do this?  We may overwrite your data."
	if [ $? -gt 0 ]
	then
		die_error "User aborted base profile execution"
	fi
	true
}


worker_sysprep ()
{
	mount -o remount,rw / &>/dev/null
}


worker_configure ()
{
	var_UI_TYPE=${arg_ui_type:-cli}
	ui_init
}


# this function must set TARGET_REPOSITORIES, which should contain at least 1
# core repo  If you want to use a network mirror, $MIRROR should be
# set, we will populate the needed mirrorlists after select_source. although
# you can also use a remote repository directly without using a mirrorlist but
# either way you should set $MIRROR so that we can know when networking is
# needed, we do this in a few places in the code.
worker_select_source ()
{
	true
}


worker_runtime_network ()
{
	#override if needed
	true
}


worker_runtime_repositories ()
{
	for i in `seq 0 $((${#var_RUNTIME_REPOSITORIES[@]}/2-1))`
	do
		repo=${var_RUNTIME_REPOSITORIES[$(($i*2))]}
		location=${var_RUNTIME_REPOSITORIES[$(($i*2+1))]}
		if ! list_pacman_repos runtime | grep -q $repo
		then
			add_pacman_repo runtime $repo "$location" || return 1
		fi
	done
	return 0
}


worker_runtime_packages ()
{
	for pkg in $var_RUNTIME_PACKAGES
	do
		$PACMAN -Sy --noconfirm --needed $pkg || return 1
	done
	return 0
}


worker_set_clock ()
{
	local default=no
	local timezone_file_copied=0
	while true; do
		ask_option $default "Date/time configuration" '' required \
		"1" "Select region and timezone" \
		"2" "Set time and date" \
		"3" "Return to Main Menu" || return 1
		case $ANSWER_OPTION in
			"1") execute worker interactive_timezone && copy_timezone_file && timezone_file_copied=1 && default=2 || return 1 ;;
			"2") if check_depend worker interactive_timezone
			     then
				execute worker interactive_time && default=3 || return 1
			     fi ;;
			"3") break ;;
		esac
	done
	[ $timezone_file_copied -eq 1 ] || copy_timezone_file || return 1
	return 0
}


worker_interactive_timezone ()
{
	interactive_timezone
}


worker_interactive_time ()
{
	interactive_time
}


worker_prepare_disks ()
{
	partition && get_possible_fs
	# in official installer: autoprepare or diy first partitions, then mountpoints
}


# Put the list of packages to be installed in $var_TARGET_PACKAGES and $var_TARGET_GROUPS
worker_package_list ()
{
	var_TARGET_GROUPS=base
}


worker_install_packages ()
{
	target_prepare_pacman && installpkg
}

worker_configure_system ()
{
	preconfigure_target && postconfigure_target
}


worker_install_bootloader ()
{
	true
}

worker_msg_report ()
{
	show_report
}
