#! /bin/sh

# Copyright (c) 2002, Alan De Smet All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#
#     * 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.
#
#     * The names of this software's contributors may not be used to endorse or
#     promote products derived from this software without specific prior
#     written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.

cat <<END_INTRO

This script will attempt to install a local copy of OpenAL for use with
Pontifex II for Linux and to modify your Pontifex II for Linux installation to
use the newly installed copy of OpenAL for Linux.

Pontifex II is copyright Chronic Logic.  They probably have the trademark as
well.  I have no affiliation with Chronic Logic, this script should not be
taken to be official nor supported by Chronic Logic.

I wrote this script for free and provide it for free.  As a result, I provide
this software "AS IS" and I disclaim ANY express or implied warranty.  The full
gory details of the disclaimer are available at the begining of the source to
this file.  (It's the BSD license, if you're familiar with it.)

Script written by Alan De Smet <openal.hack@highprogrammer.com>

Do you want to continue?
END_INTRO
read USER_INPUT
if [ "$USER_INPUT" = "y" -o \
	"$USER_INPUT" = "Y" -o \
	"$USER_INPUT" = "yes" -o \
	"$USER_INPUT" = "Yes" -o \
	"$USER_INPUT" = "YES" ] ; then
	echo "Great, continuing."
	else
	echo "You didn't reply with yes, exiting without installing."
	exit 1
fi;

echo "Excellent, let's get going..."

if [ -x pfx2 -a -e pfx2.bmp -a -x setup ] ; then
	echo "Looks like we're in the correct directory."
else
	cat <<END_COMPLAIN
This script should be run from the directory you installed Pontifex II
into.  It doesn't look like you're in the Pontifex II directory.
Are you absolutely sure you want to continue?
END_COMPLAIN
	read USER_INPUT
	if [ "$USER_INPUT" = "y" -o \
		"$USER_INPUT" = "Y" -o \
		"$USER_INPUT" = "yes" -o \
		"$USER_INPUT" = "Yes" -o \
		"$USER_INPUT" = "YES" ] ; then
		echo "Okay."
		else
		echo "You didn't reply with yes, exiting without installing."
		exit 1
	fi;
fi

echo -n "Downloading required packages..."
URL_BASE=http://www.highprogrammer.com/alan/pfx2
FILENAME=openal-alan-hack-0.0.1.tar.gz

if ! wget $URL_BASE/$FILENAME ; then
if ! curl -o $FILENAME $URL_BASE/$FILENAME ; then
if ! GET $URL_BASE/$FILENAME > $FILENAME ; then
if ! lynx -dump -source $URL_BASE/$FILENAME > $FILENAME ; then
	cat <<END_FAILED_DOWNLOAD
Failed to download the distribution file using any of wget,
curl, GET, or lynx.  There could be any number of reasons, including
the file has been moved or removed, you don't have write permissions
in the local directory, your network connection being down, the
web site's network connection being done, and running out of disk space
Any messages above may hint at the problem.
Installation has failed.
Your Pontifex II installation should be fine.
END_FAILED_DOWNLOAD
	exit 1
fi
fi
fi
fi
echo done

echo -n Installing required packages...
if ! tar xzf $FILENAME ; then
	cat <<END_TAR
Unable to open the distribution.  Installation has failed.
Your Pontifex II installation should be fine.
END_TAR
	exit 1;
fi
echo done

echo -n Deleting unneeded files...
if ! rm $FILENAME ; then
	cat <<END_RM
Unable to delete $FILENAME .
Installation will continue, your install may yet succeed.
You'll probably want to delete $FILENAME yourself, it is
uneeded.
END_RM
fi
echo done

LIB_ORIG=libopenal.so.0.0.6
LIB_NEW=libopenal.so.0

echo -n Testing required packages...
if [ ! -e $LIB_ORIG ] ; then
	cat <<END_TAR
The distribution appears to be corrupt.  Installation has failed.
Your Pontifex II installation should be fine.
END_TAR
	exit 1;
fi
echo done

echo -n Setting up required packages...
if ! ln -s $LIB_ORIG $LIB_NEW ; then
	cat <<END_TAR
Failed to symlink $LIB_ORIG to $LIB_NEW .
You may not have the required permissions, or your hard drive may be full.
Installation has failed.  Your Pontifex II installation should be fine.
END_TAR
	exit 1;
fi
echo done


echo -n Setting up Pontifex II
if ! mv pfx2 pfx2-bin ; then
	cat <<END_TAR
Unable to move the pfx2 binary out of the way.
Installation has failed.
Your Pontifex II installation should be fine.
END_TAR
	exit 1;
fi

if ! touch pfx2 ; then
	cat <<END_TAR
Unable to create a wrapper for Pontifex II. 
Installation has failed.
You will want to rename pfx2-bin to pfx2 to restore Pontifex II to
its prior state.
END_TAR
	exit 1;
fi


cat <<END_WRAPPER >> pfx2
#! /bin/sh
install_dir=\$(dirname \$0)
if [ "\$LD_LIBRARY_PATH" = "" ]; then
        LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:\$install_dir"
else
        LD_LIBRARY_PATH=\$install_dir
fi
export LD_LIBRARY_PATH
cd \$install_dir
./pfx2-bin
END_WRAPPER

if ! chmod a+x pfx2 ; then
	cat <<END_TAR
Unable to mark wrapper for Pontifex II executable. 
Installation has failed.
You will want to rename pfx2-bin to pfx2 to restore Pontifex II to
its prior state.
END_TAR
	exit 1;
fi
echo done

echo ""
echo "All Done!  You should be able to run pfx2 to start Pontifex II"


