#!/bin/bash

B1=B
B2=("DOOM SETTINGS")

function create_tmpfile {
    TMPFILE=/tmp/doomadd.tmp-$$
    for FILE in $DIR/$WAD ; do
	FILE=`basename $FILE`
	echo "$FILE $DESC off" >> $TMPFILE
    done
}

function set_iwad {
    IWAD="$CHOICES"
}

function set_pwad {
    TMPWAD=$CHOICES
    PWAD="-file $PWADDIR/$CHOICES"
    [ -z "$CHOICES" ] && unset PWAD
}

function menuchoices2 {
    case $choice in
	A) inputbox "Specify the directory with DOOM binary files
(OK for \"$DOOMBINDIR\")"
	   DOOMBINDIR=${INPUT:=$DOOMBINDIR}
	   ;;
	B) inputbox "Specify the directory with IWAD files
(OK for \"$IWADDIR\")"
	   IWADDIR=${INPUT:=$IWADDIR}
	   DESC=DOOM-IWAD-file
	   DIR=$IWADDIR
	   WAD=doom*.wad
	   create_tmpfile
	   CHK="Select *ONE* DOOM-WAD file to play"
	   INPUT=set_iwad
	   DEF=(`cat $TMPFILE`)
	   checkbox "${DEF[@]}"
	   rm -f $TMPFILE
	   ;;
	C) inputbox "Specify the directory with PWAD files
(OK for \"$PWADDIR\")"
	   PWADDIR=${INPUT:=$PWADDIR}
	   DESC=DOOM-PWAD-file
	   DIR=$PWADDIR
	   WAD=*.wad
	   create_tmpfile
	   CHK="Select personal WAD files to play"
	   INPUT=set_pwad
	   DEF=(`cat $TMPFILE`)
	   checkbox "${DEF[@]}"
	   rm -f $TMPFILE
	   ;;
	D) set_geometry
	   ;;
	E) inputbox "Enter a value from 0-7 (Current game is  \"$TMPLGAME\")
Enter \"0\" to disable loadgame"
	   TMPLGAME=${INPUT:=$TMPLGAME}
	   [ "$TMPLGAME" != "0" ] && TMPWARP=0
	   [ "$TMPLGAME" = "0" ] && LGAME= || LGAME="-loadgame $TMPLGAME"
	   ;;
	F) inputbox "Enter the number of the level (hit enter for \"$TMPWARP\")
For DOOM: maps 1-3 and levels 1-9
For DOOM II: levels 1-32
Enter \"0\" to disable level warp
Enter \"pwad\" to go to the first level in a PWAD file"
	   TMPWARP=${INPUT:=$TMPWARP}
	   [ "$TMPWARP" != "0" ] && TMPLGAME=0
	   [ "$TMPWARP" = "0" ] && WARP= || WARP="-warp $TMPWARP"
	   [ "$TMPWARP" = "pwad" ] && WARP="-warp"
	   ;;
	G) inputbox "Enter a value from 0-255 (hit enter for \"$TURBO\")"
	   TURBO=${INPUT:=$TURBO}
	   ;;
	H) inputbox "Enter skill level (0-5) (hit enter for \"$DOOMSKILL\")
Enter \"0\" to disable skill"
	   DOOMSKILL=${INPUT:=$DOOMSKILL}
	   [ "$DOOMSKILL" = "0" ] && SKILL= || SKILL="-skill ${DOOMSKILL:=3}"
	   ;;
	I) if [ "$TMPFAST" = "on" ]; then
		TMPFAST=off
		FAST=
	   else
		TMPFAST=on
		FAST="-fast"
	   fi
	   ;;
	J) if [ "$TMPRESPAWN" = "on" ]; then
		TMPRESPAWN=off
		RESPAWN=
	   else
		TMPRESPAWN=on
		RESPAWN="-respawn"
	   fi
	   ;;
	K) if [ "$TMPNOMONSTERS" = "on" ]; then
		TMPNOMONSTERS=off
		NOMONSTERS=
	   else
		TMPNOMONSTERS=on
		NOMONSTERS="-nomonsters"
	   fi
	   ;;
	L) set_sound
	   ;;
	M) backup_conf --force $RCFILE
	   saverc
	   make_doom_config
	   return
	   ;;
    esac
    configure_doom
}

function configure_doom {
    MENU="CONFIGURE DOOM"
    MENUCHOICES=menuchoices2
    CDOOM=("A" "Where are the DOOM binaries ? [$DOOMBINDIR]" \
	   "B" "Select IWAD file              [$IWAD]" \
	   "C" "Select PWAD file              [$TMPWAD]" \
	   "D" "Select geometry / resolution  [$X11_GEOM]" \
	   "E" "Load saved game               [$TMPLGAME]" \
	   "F" "Select level warp             [$TMPWARP]" \
	   "G" "Set turbo speed               [$TURBO %]" \
	   "H" "Select skill                  [$DOOMSKILL]" \
	   "I" "Fast monsters                 [$TMPFAST]" \
	   "J" "Respawn monsters              [$TMPRESPAWN]" \
	   "K" "No monsters                   [$TMPNOMONSTERS]" \
	   "L" "Sound                         [$TMP_SOUND]" \
	   "M" "Save settings to \"$RCFILE\"")
    menubox "${CDOOM[@]}"
}
