#!/bin/ksh
fe_prefix=/usr/pkg
fe_config="${fe_prefix}/share/falcons-eye-dir/config/jtp_opts.txt"
fe_output=""
if [ ! -f "$fe_config" ]
then
	touch "$fe_config"
	if [ $? -ne 0 ]
	then
		echo "Cannot find nor create $fe_config..."
		exit 1
	fi
fi
settings=$(grep '^[^%].*=' "$fe_config"|sed 's/=\(.*\)/="\1"/')
orig_settings=$(echo "$settings"|sed 's/^/orig_/')
variables="screen_xsize screen_ysize fullscreen play_music play_effects
	linux_midi_player linux_mp3_player wall_style recenter_after_movement
	one_command_per_click scroll_delay command_delay gamma_correction"
eval "$settings"
eval "$orig_settings"

function __menu
{
	choices=$(echo "$2"|sed 's/\^/\
/g')
	max=$(echo "$choices"|wc -l)
	title=$(echo "$1"|sed 's/_/ /g')
	underline=$(echo "$title"|sed 's/./=/g')
	ps=$(echo "$choices"|pr -n\	1|grep '	')
	ps=$(echo -e "\n\n$title\n$underline\n\n$ps\n\nPlease choose> ")
	input=""
	while [ "$input" -lt 1 -o "$input" -gt "$max" ]
	do
		clear
		read input?"$ps"
		input=$(echo "$input"|cut -c 1)
	done
	choice=$(echo "$choices"|head -n +$input|tail -n 1|cut -d ' ' -f 1)
	if [ "$choice" != "return" ]
	then
		${choice}_menu ${choice}
		return 1
	fi
	return 0
}

function do_toggle
{
	toggle_value=$(eval echo \$$1)
	if [ "$toggle_value" -eq 1 ]
	then
		eval $1=0
	else
		eval $1=1
	fi
}

function read_value
{
	rmin="$1"
	rps="$2"
	rvalue=$(($min-1))
	while [ "$rvalue" -lt "$rmin" ]
	do
		read rvalue?"$rps"
		rvalue=$(echo "$rvalue"|sed 's/^\([0-9]*\).*/\1/')
	done
	return $rvalue
}

read_float_value_return=0
function read_float_value
{
	rmin="$1"
	rmax="$2"
	rps="$3"
	rvalue=$(echo "$rmin-1"|bc)
	while [ "$(echo \"$rvalue<$rmin\"|bc)" -eq 1 -o \
		"$(echo \"$rvalue>$rmax\"|bc)" -eq 1 ]
	do
		read rvalue?"$rps"
	done
	rvalue=$(echo "$rvalue"|sed 's/^\./0./')
	read_float_value_return="$rvalue"
}

function find_line
{
	return $(grep -n "^$1=" "$fe_output"|head -1|cut -d : -f 1)
}

function find_commented_line
{
	return $(grep -n "^%$1=" "$fe_output"|head -1|cut -d : -f 1)
}

function edit_replace_line
{
	echo -e "$1\nd\ni\n$2\n.\nwq\n" | ed "$fe_output" >/dev/null 2>&1
	return $?
}

function edit_insert_line
{
	echo -e "$1\ni\n$2\n.\nwq\n" | ed "$fe_output" >/dev/null 2>&1
	return $?
}

function edit_append_line
{
	echo "$1" >>"$fe_output"
	return $?
}

function save_changes
{
	retval=0
	for var in $variables
	do
		value=$(eval echo \$$var)
		if [ "$value" != "$(eval echo \$orig_$var)" ]
		then
			find_line "$var"
			line=$?
			if [ $line -gt 1 ]
			then
				edit_replace_line $line "$var=$value"
				result=$?
			else
				find_commented_line "$var"
				line=$?
				if [ $line -gt 1 ]
				then
					edit_insert_line $line "$var=$value"
					result=$?
				else
					edit_append_line "$var=$value"
					result=$?
				fi
			fi
			if [ $result -ne 0 ]
			then
				retval=$result
			fi
		fi
	done
	return $retval
}

function use_tmp_menu
{
	fe_output=$(mktemp /tmp/jtp_opts.txt.XXXXXX)
	cp "$fe_config" "$fe_output"
}

function use_home_menu
{
	fe_output=$(mktemp $HOME/jtp_opts.txt.XXXXXX)
	cp "$fe_config" "$fe_output"
}

function use_default_path_menu
{
	fe_output="$fe_config"
}

function save_changes_menu
{
	__menu "$1" "use_default_path : $fe_config^use_tmp : write config file to /tmp^use_home : write config file to $HOME"
	save_changes
	if [ $? -ne 0 ]
	then
		echo -e "Error writing ${fe_output}...\nPress enter...\n"
		read pause
	else
		echo -e "Finished writing ${fe_output}...\n"
		if [ "$fe_output" != "$fe_config" ]
		then
			echo "To use new config:"
			echo "	cp ${fe_output} ${fe_config}"
		fi
		exit
	fi
}

function quit_without_saving_menu
{
	exit
}

function have_settings_changed
{
	changed=0
	for var in $variables
	do
		if [ "$(eval echo \$$var)" != "$(eval echo \$orig_$var)" ]
		then
			changed=1
		fi
	done
	return $changed
}

function exit_menu
{
	have_settings_changed
	if [ $? -eq 0 ]
	then
		exit
	fi
	__menu "$1" "save_changes^quit_without_saving"
}

function print_sound_warning
{
	if [ "$play_effects" -eq 1 ]
	then
		cat <<__EOF__

You have sound effects turned on.  Falcon's Eye Nethack uses SDL to play
sound effects, and SDL is configured to directly use /dev/audio.  The
result is a conflict with external players that open /dev/audio.

So you either can have musical scores played by mpg123 and timidity,
or you can have sound effects.

__EOF__
	fi
}

function pick_player
{
	player_path="/usr/pkg/bin/$1"
	"$1" >/dev/null 2>&1
	if [ $? -ne 127 ]
	then
		player_path=$(which $1)
	fi

	looping=1
	while [ "$looping" -eq 1 ]
	do
		print_sound_warning
		echo "Example:"
		echo "	$player_path $2 %s"
		echo -n "Please enter command to play $3 files> "
		read $4
		player_value=$(echo $(eval echo \$$4)|cut -d ' ' -f 1)
		if [ ! -x "$player_value" ]
		then
			if [ "$player_value" = "" ]
			then
				looping=0
			else
				echo "Error: cannot execute $player_value"
			fi
		else
			looping=0
		fi
	done
}

function pick_mp3_player_menu
{
	pick_player mpg123 "-q" "MP3" "linux_mp3_player"
}

function manual_enter_midi_player_menu
{
	pick_player timidity "-idqq" "MIDI" "linux_midi_player"
}

function midiplay_menu
{
	midi_port=99
	max=$(midiplay -l|tail -1|cut -d : -f 1)
	while [ "$midi_port" -lt 0 -o "$midi_port" -gt "$max" ]
	do
		echo ""
		echo "MIDI ports"
		echo "=========="
		echo ""
		midiplay -l
		echo ""
		read midi_port?"Please pick MIDI port> "
		midi_port=$(echo "$midi_port"|cut -c 1)
	done
	if [ "$midi_port" -gt 0 ]
	then
		linux_midi_player="/usr/bin/midiplay -d$midi_port %s"
	else
		linux_midi_player="/usr/bin/midiplay %s"
	fi
}

function pick_midi_player_menu
{
	looping=1
	while [ "$looping" -eq 1 ]
	do
		midiplay -l >/dev/null 2>&1
		if [ $? -ne 127 ]
		then
			menu_string="midiplay^"
		else
			menu_string=""
		fi
		menu_string="${menu_string}manual_enter_midi_player : \"$linux_midi_player\"^return to previous menu"
		__menu "$1" "$menu_string"
		looping=$?
	done
}

function pick_players_menu
{
	looping=1
	while [ "$looping" -eq 1 ]
	do
		__menu "$1" "pick_midi_player : \"$linux_midi_player\"^pick_mp3_player : \"$linux_mp3_player\"^return to previous menu"
		looping=$?
	done
}

function music_menu
{
	do_toggle play_music
}

function sound_effects_menu
{
	do_toggle play_effects
}

function audio_settings_menu
{
	looping=1
	while [ "$looping" -eq 1 ]
	do
		menu_string="sound_effects : $play_effects^music : $play_music"
		menu_string=$(echo "$menu_string"|sed -e 's/: 1/: on/g' -e 's/: 0/: off/g')
		if [ "$play_music" -eq 1 ]
		then
			menu_string="${menu_string}^pick_players"
		fi

		__menu "$1" "${menu_string}^return to previous menu"
		looping=$?
	done
}

function fullscreen_menu
{
	do_toggle fullscreen
}

function screen_xsize_menu
{
	read_value 800 "Please enter x size (minimum 800) -> "
	screen_xsize=$?
}

function screen_ysize_menu
{
	read_value 600 "Please enter y size (minimum 600) -> "
	screen_ysize=$?
}

function gamma_correction_menu
{
	read_float_value 0 2 "Please enter gamma correction (dark 0.0 - light 2.0) -> "
	gamma_correction="$read_float_value_return"
}

function wall_style_full_menu
{
	wall_style="full"
}

function wall_style_half_height_menu
{
	wall_style="half_height"
}

function wall_style_transparent_menu
{
	wall_style="transparent"
}

function wall_style_menu
{
	__menu "$1" "wall_style_full^wall_style_half_height^wall_style_transparent"
}

function video_settings_menu
{
	looping=1
	while [ "$looping" -eq 1 ]
	do
		menu_string="fullscreen : $fullscreen^screen_xsize : \"$screen_xsize\"^screen_ysize : \"$screen_ysize\"^wall_style : \"$wall_style\"^gamma_correction : \"$gamma_correction\"^return to previous menu"
		menu_string=$(echo "$menu_string"|sed -e 's/: 1/: on/g' -e 's/: 0/: off/g')
		__menu "$1" "$menu_string"
		looping=$?
	done
}

function recenter_after_movement_menu
{
	do_toggle recenter_after_movement
}

function one_command_per_click_menu
{
	do_toggle one_command_per_click
}

function scroll_delay_menu
{
	read_float_value 0 10 "Please enter scroll delay (0 - 10 seconds) -> "
	scroll_delay="$read_float_value_return"
}

function command_delay_menu
{
	read_float_value 0 10 "Please enter command delay (0 - 10 seconds) -> "
	command_delay="$read_float_value_return"
}

function interface_settings_menu
{
	looping=1
	while [ "$looping" -eq 1 ]
	do
		menu_string="recenter_after_movement : $recenter_after_movement^one_command_per_click : $one_command_per_click^scroll_delay : \"$scroll_delay\"^command_delay : \"$command_delay\"^return to previous menu"
		menu_string=$(echo "$menu_string"|sed -e 's/: 1/: on/g' -e 's/: 0/: off/g')
		__menu "$1" "$menu_string"
		looping=$?
	done
}

function about_menu
{
	less -d <<__EOF__
This script generates a configuration file for falcon's eye nethack.
Some assumptions are made by this script, for example, that you are
running NetBSD, and that you do not have multiple sound cards.
__EOF__
	main_menu
}

function view_config_file_menu
{
	less -d "$fe_config"
}

function main_menu
{
	__menu "main" "about^audio_settings^video_settings^interface_settings^view_config_file^exit"
}

while :
do
	main_menu
done
