#!/bin/sh

#  Copyright (c) 2010-2014 Andy Kosela <akosela@andykosela.com>
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#  PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
#  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

inform=%%PREFIX%%/bin/inform6
ni=%%PREFIX%%/bin/ni
rules=%%DATADIR%%/Internal

main()
{
	pkg=`basename $story | cut -d. -f1 2>/dev/null`
	mkdir -p $pkg.inform/Build $pkg.inform/Source $pkg.inform/Index
	cp $story $pkg.inform/Source/story.ni
	$ni --release --internal $rules --format=$zsuf --project $pkg.inform
	mv "$pkg.inform/Build/Debug log.txt" debug.log
	if [ -s $pkg.inform/Build/auto.inf ]; then
		echo ""
		$inform $zopts $pkg.inform/Build/auto.inf -o $pkg.$zsuf
		mv $pkg.inform/Build/Map.eps ${pkg}_map.eps 1>/dev/null 2>&1
	fi
	echo "Debug log: debug.log"
	rm gameinfo.dbg 2>/dev/null
	rm -rf $pkg.inform $pkg.materials 2>/dev/null
	exit 0
}

replay()
{
	if [ ! -z "`grep -e quit -e save $pkg.$$.key`" ]; then
		sed -i '' '/^quit/d ; /^save/d' $pkg.$$.key
	fi
	if [ -z "`grep 'script off' $pkg.$$.key`" ]; then
		echo "script off" >> $pkg.$$.key
	fi
	echo -e \
	"0a\nscript\n$pkg.$$.scr\n.\n\$a\nsave\n$pkg.$$.sav\nquit\ny\ny\n.\nw" |
	    ed - $pkg.$$.key

	if [ ! -z `echo $story | grep -e \.ulx -e \.gblorb -e \.blb` ]; then
		cat $pkg.$$.key | dumb-glulxe $story 1>/dev/null 2>&1
		cat $pkg.$$.scr | fold -s -72 > $pkg.$$.scr
	else
		cat $pkg.$$.key | frotz -QS 72 $story 1>/dev/null 2>&1
	fi

	echo -e \
	"g/^script/d\ng/^quit/d\ng/^save/d\ng/.sav/d\ng/.scr/d\ng/^y$/d\nw" |
	    ed - $pkg.$$.key
	echo "$pkg.$$.scr -- replay transcript file"
	echo "$pkg.$$.sav -- replay save file"
	echo "$pkg.$$.key -- replay walkthrough file"
	exit 0
}

usage()
{
	echo "Usage: i7 story.ni"
	echo "       i7 [-g] story.ni"
	echo "       i7 [-r] story.scr story"
	echo "       i7 [-w] story.key story"
	echo ""
	echo "       -g : create Glulx release"
	echo "       -r : replay game from a transcript file"
	echo "       -w : replay game from a walkthrough file"
	echo ""
	echo "By default i7 creates Z-code v8 releases."
	exit 0
}

# Start of script

if [ ! -z `echo $1 | grep '.ni\|.inf\|.txt'` ]; then
	zsuf=z8
	zopts=-kE2w~S~DC0xv8
	story=$1
	main
elif [ -z "$1" ]; then
	usage
fi	

while getopts g:r:w:h opts
do
	case $opts in
		g) story=$OPTARG
		   zsuf=ulx
		   zopts=-kE2w~S~DC0xG
		   main
		;;
		h) usage
		;;
		r) story=$3
		   pkg=`basename $OPTARG | cut -d. -f1 2>/dev/null`
		   grep '>' $OPTARG | cut -d'>' -f2 > $pkg.$$.key
		   replay
		;;
		w) story=$3
		   pkg=`basename $OPTARG | cut -d. -f1 2>/dev/null`
		   cp $OPTARG $pkg.$$.key
		   replay
		;;
		*) usage
		;;
	esac
done
