#!/bin/sh

echo "X-Plane WINE installer"
echo
if [ `whoami` = 'root' ]; then
	echo "Please install X-Plane as a normal user."
	exit 1
fi
echo -n ">>> Looking for needed files... "
if [ ! -f wine-xplane.tar.gz ]; then
	echo "FAILURE"
	echo
	echo "I couldn't find wine-xplane.tar.gz. Put it in this directory and try again."
	exit 1
fi
if [ ! -f xwininst.exe ]; then
	echo "FAILURE"
	echo
	echo "I couldn't find xwininst.exe. Rename your XWINFULL*.exe to that, put it in this directory, and try again."
	exit 1
fi
unrar > /dev/null 2>/dev/null
if [ $? -ne 7 ]; then
	echo "FAILURE"
	echo
	echo "I couldn't find unrar. Make sure that it's in your "'$'"PATH, and try again."
fi
echo "Ok."
echo -n ">>> Verifying integrity of wine-xplane.tar.gz... "
tar tzf wine-xplane.tar.gz >/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
	echo "FAILURE"
	echo
	echo "It seems that wine-xplane.tar.gz is corrupt. Fix it, and try again."
	exit 1
fi
echo "Ok."
echo -n ">>> Verifying integrity of xwininst.exe... "
unrar t xwininst.exe > /dev/null
if [ $? -ne 0 ]; then
	echo "FAILURE"
	echo
	echo "It seems that xwininst.exe is corrupt. Fix it, and try again."
	exit 1
fi
echo "Ok."
echo
echo "Everything looks good as far as I can see."
echo "I'm going to install to" ~/XPlane "- if that's OK, press ENTER to"
echo "continue, otherwise hit Control-C to abort."
echo
read
echo -n ">>> Making directory $HOME/XPlane... "
mkdir $HOME/XPlane > /dev/null 2>/dev/null
echo "Ok."
echo -n ">>> Extracting WINE... "
SRCPATH=`pwd`
cd $HOME/XPlane
tar xzf $SRCPATH/wine-xplane.tar.gz >/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
	echo "FAILURE"
	echo
	echo "For whatever reason, I couldn't untar WINE into $HOME/XPlane."
	exit 1
fi
echo "Ok."
echo -n ">>> Extracting X-Plane... "
unrar x -o+ $SRCPATH/xwininst.exe >/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
	echo "FAILURE"
	echo
	echo "For whatever reason, I couldn't unrar X-Plane into $HOME/XPlane."
	exit 1
fi
echo "Ok."

echo ">>> Compiling WINE."
cd wine
if [ -f Makefile ]; then
	echo
	echo "Something seems to already be in $HOME/XPlane/wine. Get rid of it, then try again."
	exit 1
fi
echo ">>> This will take awhile. Go get a pizza."
sleep 5
tools/wineinstall
if [ $? -ne 0 ]; then
	echo
	echo "Failed to compile WINE. Take a look at the messages above."
	exit 1
fi
cd ..
echo
echo -n ">>> Creating shell wrappers for X-Plane... "
echo '#!/bin/bash' > xplane
echo "cd $HOME/XPlane/X-System\ ???" >> xplane
echo "$HOME/XPlane/winex-bin/bin/xplwine X-Plane\ ???.exe" >> xplane
chmod +x xplane
echo '#!/bin/bash' > planemaker
echo "cd $HOME/XPlane/X-System\ ???" >> planemaker
echo "$HOME/XPlane/winex-bin/bin/xplwine Plane-Maker\ ???.exe" >> planemaker
chmod +x planemaker
echo '#!/bin/bash' > worldmaker
echo "cd $HOME/XPlane/X-System\ ???" >> worldmaker
echo "$HOME/XPlane/winex-bin/bin/xplwine World-Maker\ ???.exe" >> worldmaker
chmod +x worldmaker
echo '#!/bin/bash' > airfoilmaker
echo "cd $HOME/XPlane/X-System\ ???" >> airfoilmaker
echo "$HOME/XPlane/winex-bin/bin/xplwine Airfoil-Maker\ ???.exe" >> airfoilmaker
chmod +x airfoilmaker
echo '#!/bin/bash' > briefer
echo "cd $HOME/XPlane/X-System\ ???" >> briefer
echo "$HOME/XPlane/winex-bin/bin/xplwine Briefer\ ???.exe" >> briefer
chmod +x briefer
echo Ok
echo
echo "X-Plane is all installed."
echo
echo "Run $HOME/XPlane/xplane to play it."