#!/bin/ash

do_exit() {
	[ -f "$TMP_LIST" ] && rm "$TMP_LIST"
	if [ "$1" ]; then
		yad $YAD_STD_OPTS --title "FrugalPup v${VER}" --width=220 --text-align=center \
			--text "${1}" \
			--button=gtk-ok:0
	fi
	exit $2
}

error_exit() {
	[ "$CACHE_FN" ] && [ -f "$CACHE_FN" ] && rm "$CACHE_FN"
	do_exit "$1" 1
}

normal_exit() {
	do_exit "$1" 0
}

EXE_DIR="$(dirname $(readlink -f ${0}))"
COMN_FN="$EXE_DIR/frugalpup-common"
[ -f "$COMN_FN" ] && . "$COMN_FN"

[ -f "$UTILS_DIR/functions_part" ] || error_exit "File <span foreground='purple'>$UTILS_DIR/functions_part</span> not found."

EFI_TAR="$(find $EXE_DIR -name '*-efi.tar.xz' | head -n1)"
[ "$EFI_TAR" ] || error_exit "grub2-efi support missing"

MBR_TAR="$(find $EXE_DIR -name '*-mbr.tar.xz' | head -n1)"
[ "$MBR_TAR" ] || error_exit "grub2-mbr support missing"

PUP_ICON='/usr/share/icons/hicolor/16x16/apps/linux.png'
BOOT_ICON='/usr/share/icons/Puppy Standard/16/actions/gtk-save.svg'
ISO_ICON='/usr/share/icons/Puppy Standard/16/devices/cdrom.svg'
HELP_ICON='/usr/share/icons/Puppy Standard/16/actions/gtk-help.svg'
EXIT_ICON='/usr/share/icons/Puppy Standard/16/actions/application-exit.svg'
SET_ICON='/usr/share/icons/Puppy Standard/16/categories/preferences.svg'

HELP_FN='/usr/share/doc/frugalpup.html'

TMP_LIST="/tmp/${0##*/}_$$_tmp_list.txt"

[ -f "$TMP_LIST" ] && rm "$TMP_LIST"
echo "" >> "$TMP_LIST"
echo "" >> "$TMP_LIST"
echo "" >> "$TMP_LIST"
echo "" >> "$TMP_LIST"
echo "" >> "$TMP_LIST"
echo "defaulthtmlviewer $HELP_FN" >> "$TMP_LIST"
echo "$UTILS_DIR/kill-parent-yad $EXE_DIR/frugalpup-settings" >> "$TMP_LIST"
echo "$UTILS_DIR/kill-parent-yad $EXE_DIR/frugalpup-puppyfiles" >> "$TMP_LIST"
echo "$UTILS_DIR/kill-parent-yad $EXE_DIR/frugalpup-bootfiles" >> "$TMP_LIST"
echo "$UTILS_DIR/kill-parent-yad $EXE_DIR/frugalpup-bootcd" >> "$TMP_LIST"

yad $YAD_STD_OPTS --title "FrugalPup v${VER}" --width=500 \
	--text="This utility installs/manages multiple Puppy frugal installs.

Click the corresponding button to execute a facility." \
	--form --columns=2 \
	--field="<span foreground='blue'>Help</span> button:
View the help pages for <span foreground='purple'>FrugalPup</span>.:LBL" \
	--field="<span foreground='blue'>Settings</span> button:
Modify the config file <span foreground='purple'>$CONFIG_FN</span>.
Enable/disable the display of optional advanced dialogs,
<span foreground='purple'>Separate save partition</span>, <span foreground='purple'>Kernel boot parameters</span>
and <span foreground='purple'>Pfix boot parameter</span>.:LBL" \
	--field="<span foreground='blue'>Puppy</span> button:
Setup a Puppy frugal install directory with Puppy files.
Use this facility to create or update a Puppy frugal install directory, with appropriate Puppy release files.:LBL" \
	--field="<span foreground='blue'>Boot</span> button:
Setup a boot partition with grub2 files for selected Puppies.
Use this facility to setup or update the boot configuration files on a fat32 boot partition.
<span foreground='red'>Overwrites existing boot files.</span>:LBL" \
	--field="<span foreground='blue'>BootCD</span> button:
Create an ISO file with grub2 files to boot selected Puppies.
This is for older machines that can't boot from a usb stick.
Use this facility to create a CD/DVD that can boot existing Puppy frugal installs.:LBL" \
	--field="Help!${HELP_ICON}!View help pages:FBTN" \
	--field="Settings!${SET_ICON}!Execute frugalpup-settings:FBTN" \
	--field="Puppy!${PUP_ICON}!Execute frugalpup-puppyfiles:FBTN" \
	--field="Boot!${BOOT_ICON}!Execute frugalpup-bootfiles:FBTN" \
	--field="BootCD!${ISO_ICON}!Execute frugalpup-bootcd:FBTN" \
	--rest="$TMP_LIST" \
	--button="Exit!${EXIT_ICON}:3" >/dev/null
# if exit is via button, not kill-parent-yad script
[ $? -eq 3 ] && [ "$CACHE_FN" ] && [ -f "$CACHE_FN" ] && rm "$CACHE_FN"

normal_exit
