#!/bin/dash
# firstrun-wizard, run once per-user when the savefile is mounted

### config
APPTITLE="First Run Wizard"
CONF_FILES="/etc/localtime /etc/hwclock.conf $FATDOG_STATE_DIR/xkeymap $FATDOG_STATE_DIR/language STOP"
NLS_SFS=fd64-nls
HWCLOCKCONF=/etc/hwclock.conf
[ $BOOTSTATE_PATH ] && [ -f $BOOTSTATE_PATH ] && . $BOOTSTATE_PATH

export TEXTDOMAIN=fatdog
. gettext.sh

# $@ - params
dlg() {
	Xdialog --title "$APPTITLE" "$@"
}

# attempt to load the nls sfs if it exists
load_nls() {
	# if nls not loaded, attempt to load it
	if ! [ -e /etc/fatdog-nls-version ]; then
		read version < /etc/fatdog-version
		for p in ${NLS_SFS}.sfs ${NLS_SFS}_$version.sfs; do
			if [ -f ${SAVE_DEV_MOUNT}/$p ]; then
				load_sfs.sh --load ${SAVE_DEV_MOUNT}/$p
				break
			fi 
		done
	fi
}

### main - the wizards we want to run
# check - if all the conffiles already exist, no need to run this
for p in $CONF_FILES; do
	! [ -e $p ] && break
done
[ $p = "STOP" ] && exit

### start
dlg --infobox "\
You will now be prompted to setup the minimum configuration settings
for your system: locale, keyboard layout, hardware clock settings, and
timezone. The questions are skippable and if you skip them
(answering cancel when the questions are prompted), reasonable defaults 
will be used.

If you ever need to change the settings again, you can find and run the 
individual setup item under \"Localisation\" tab of Control Panel. 
Please click OK to continue.
" 0 0 30000

# the four scripts we run

# 1. NLS
[ $(id -u) -eq 0 ] && load_nls
! [ -e $FATDOG_STATE_DIR/language ] && fatdog-choose-locale.sh
[ -e $FATDOG_STATE_DIR/language ] && read LANG < $FATDOG_STATE_DIR/language &&
export LANG # next questions are all in NLS

# 2. keyboard
! [ -e $FATDOG_STATE_DIR/xkeymap ] && fatdog-keyboard-wizard.sh

# 3. Time
if [ $(id -u) -eq 0 ]; then
	! [ -e $HWCLOCKCONF ] && hwclockconf.sh
	! [ -e /etc/localtime ] && fatdog-set-timezone.sh	
fi

# restart notification
dlg --yesno "\
Configuration is done. If you have changed anything, they will only
take effect after the desktop is restarted.

Do you want to restart the desktop now?" 0 0 &&
DONT_ASK=1 wmexit restart
