#!/bin/bash
#
# Dink Smallwood installer extractor
# Released into the public domain by its creator,
#     Alexander Krivács Schrøder
# Fixes by Sylvain Beucler  2008
#
# Prerequisites:
# * 7-Zip command line tool (7z) installed on system (Package called
#   p7zip on Gentoo, p7zip-full under Debian)
#
# Parameters:
# 1. The Dink Smallwood installer file (dinksmallwood108.exe)
# 2. The location of the extracted files

# Note: for some reason, 7z (and 7-Zip generally) doesn't extract or
# otherwise detect the following files:
# 
# dink/Sound/107.mid
# dink/Sound/6.mid
# dink/Sound/Battle.mid
# dink/Sound/Caveexpl.mid
# dink/Story/SC-SLAY.d
# dink/Story/SC-LOCK.d
# dink/Story/SC-PILL.d
# island/sound/metopen.wav
# 
# So you might be better off using Wine.


# Define globals
PACKAGENAME=$1
DESTINATION=$2
ORIGLOC=$(pwd)

# Check if we have gotten our parameters
if [ "x$PACKAGENAME" = "x" ] || [ "x$DESTINATION" = "x" ]; then
	echo "Usage: $(basename $0) <installer> <output location>"
	exit
fi

# Check if parameters are valid
if [ ! -e $PACKAGENAME ]; then
	echo "Error: installer does not exist"
	exit
fi

if ! which 7z > /dev/null; then
	echo "You need to install 7z"
	exit
fi

# Create destionation folder and extract files into it
if mkdir -p $DESTINATION; then
	7z x $PACKAGENAME -o$DESTINATION
	cd $DESTINATION

	# Remove plugin directory from installer
	rm -rf \$PLUGINSDIR

	# Move files out of folder with odd name
	cd \$_OUTDIR
	mv * ..
	cd ..
	rm -rf \$_OUTDIR

	# Remove unnecessary files
	rm DFArc.exe dink.exe dinkedit.exe

	# Return to original location
	cd $ORIGLOC
else
	echo "Error: Could not create destionation folder"
fi
