#!/bin/sh

# doomsday-compat: This script translates between the "-iwad" parameter
# as expected by vanilla Doom and the one expected by the Doomsday Engine:
# - Vanilla Doom expects the -iwad parameter to give an IWAD file.
# - Doomsday expects the -iwad parameter to give a directory containing
#   IWAD files and loads one depending on the additional -game parameter.

# Copyright © 2013-2015 Fabian Greffrath <fabian@debian.org>
# This package is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>

# On Debian systems, the complete text of the GNU General
# Public License version 2 can be found in "/usr/share/common-licenses/GPL-2".

DOOMSDAY="doomsday"

next_is_iwad=0
next_is_pwad=0

iwaddir="$(awk '/^iwaddir/{print $2}' /etc/doomsday/paths)"
gamemode="doom2"

for arg in "$@"
do
	if [ $arg = "-iwad" ]
	then
		next_is_iwad=1
		continue
	fi

	if [ $arg = "-file" ]
	then
		next_is_pwad=1
		continue
	fi

	if [ $next_is_iwad = 1 ]
	then
		next_is_iwad=0
		iwadpath=$arg
		iwadfile=$(basename $arg)
		iwaddir=$(dirname $arg)
		continue
	fi

	if [ $next_is_pwad = 1 ]
	then
		next_is_pwad=0
		pwadpath=$arg
		pwadfile=$(basename $arg)
		if [ ! -f $pwadpath -a -f $iwaddir/$pwadpath ]
		then
			pwadpath=$iwaddir/$pwadpath
		fi
		continue
	fi

	cmdline="$cmdline $arg"
done

# if no -iwad parameter is given, guess from executable name
if [ -z "$iwadfile" ]
then
	case "$(basename $0)" in
		heretic)
			iwadfile=heretic.wad
			iwadpath=$iwaddir/$iwadfile
			;;
		hexen)
			iwadfile=hexen.wad
			iwadpath=$iwaddir/$iwadfile
			;;
	esac
fi

case "$(echo $iwadfile | tr '[:upper:]' '[:lower:]')" in
	doom1.wad)
		gamemode=doom1-share
		;;
	doomu.wad|doom.wad)
		case "$(md5sum $iwadpath | awk '{print $1}')" in
			1cd63c5ddff1bf8ce844237f580e9cf3)
				gamemode=doom1
				;;
			*)
				gamemode=doom1-ultimate
				;;
		esac
		;;
	doom2f.wad|doom2.wad)
		gamemode=doom2
		;;
	freedoomu.wad|freedoom1.wad)
		gamemode=freedoom1
		;;
	freedoom.wad|freedoom2.wad)
		gamemode=freedoom2
		;;
	freedm.wad)
		gamemode=freedm
		;;
	plutonia.wad)
		gamemode=doom2-plut
		;;
	tnt.wad)
		gamemode=doom2-tnt
		;;
	chex.wad)
		gamemode=chex
		;;
	hacx.wad)
		gamemode=hacx
		;;
	heretic1.wad)
		gamemode=heretic-share
		;;
	heretic.wad)
		case "$(md5sum $iwadpath | awk '{print $1}')" in
			3117e399cdb4298eaa3941625f4b2923|1e4cb4ef075ad344dd63971637307e04)
				gamemode=heretic
				;;
			*)
				gamemode=heretic-ext
				;;
		esac
		;;
	hexendemo.wad|machexendemo.wad|hexenbeta.wad|hexen.wad)
		case "$(md5sum $iwadpath | awk '{print $1}')" in
			876a5a44c7b68f04b3bb9bc7a5bd69d6)
				gamemode=hexen-demo
				;;
			*)
				gamemode=hexen
				;;
		esac
		;;
esac

case "$(echo $pwadfile | tr '[:upper:]' '[:lower:]')" in
	hexdd.wad)
		gamemode=hexen-dk
		;;
esac

# fall back to alternatives
if [ $gamemode = "doom2" \
     -a ! -f $iwaddir/doom2.wad \
     -a ! -f $iwaddir/doom2f.wad \
     -a -f $iwaddir/freedoom2.wad ]
then
	gamemode=freedoom2
elif [ \( $gamemode = "heretic" -o $gamemode = "heretic-ext" \) \
     -a ! -f $iwaddir/heretic.wad \
     -a -f $iwaddir/heretic1.wad ]
then
	gamemode=heretic-share
elif [ $gamemode = "hexen" \
     -a ! -f $iwaddir/hexen.wad \
     -a -f $iwaddir/hexendemo.wad ]
then
	gamemode=hexen-demo
fi

if [ -n "$pwadpath" ]
then
	cmdline="-file $pwadpath $cmdline"
fi

if [ -n "$iwaddir" -a -n "$gamemode" ]
then
	eval $DOOMSDAY -iwad $iwaddir -game $gamemode $cmdline
elif [ -n "$gamemode" ]
then
	eval $DOOMSDAY -game $gamemode $cmdline
else
	eval $DOOMSDAY "$@"
fi


