#!/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=${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 --ok-label "PROCEED" --cancel-label "QUIT" --yesno "\
This is the First Run wizard.

It will prompt you to setup minimum configurations settings for your
system: locale, keyboard layout, hardware clock, and timezone.

You will be prompted only for settings you haven't set-up before.
These prompts themselves can be skipped (simply choose Cancel when
asked) and reasonable defaults will be used.

Please click PROCEED to continue, or QUIT to leave.

Note: this wizard only runs once. You will never be asked again.

However, you can always configure these (or change them if you choose
to proceed and perform the settings now) by launching the individual
settings item under \"Localisation\" tab the Control Panel.

" 0 0 30000 || exit

# 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
