#!/bin/sh
echo "This script will attempt to install the commercial Quake 3 Arena data files"
echo "from any mounted CDROMs and from common game install locations.  It will"
echo "also take any command line arguments as directories to search.  If the"
echo "files are on the same filesystem, they will be hardlinked to save space." 
echo

findpak() {
installdir=${installdir:=/usr/lib/quake3}
[ -d /usr/games/quake3 ] && sources="/usr/games/quake3 $sources"
[ -d /usr/local/games/quake3 ] && sources="/usr/local/games/quake3 $sources"
sources="$(mount|grep 'iso9660\|udf'|awk '{print $3}') $sources $@"

[ ! "$sources" ] && echo "Error: No locations found." && return 1

echo -n "Searching in "
for a in $sources $@; do
    echo -n "${a}... "
    [ -f $a/baseq3/pak0.pk3 ] && pak=$a
done
[ "$pak" ] && return 0
echo
echo
echo "Error: Data files not found."
return 1
}

copypak() {
echo
echo Copying data files from ${pak}...
cp -l $pak/baseq3/pak0.pk3 $installdir/baseq3/pak0.pk3 >&/dev/null || cp -f $pak/baseq3/pak0.pk3 $installdir/baseq3/pak0.pk3
[ $? -ne 0 ] && $xecho "copy failed." && exit 1
cd $installdir/baseq3
echo "Checksumming data file..."
echo "1197ca3df1e65f3c380f8abc10ca43bf  pak0.pk3"|md5sum --status -c
[ $? -ne 0 ] && $xecho "File copied, but checksum failed." && exit 1
$xecho "File copied successfully.  Please restart iccuslus.org/quake3."
}

if [ $DISPLAY ]; then
    [ -x $(which gdialog) ] && xecho="$(which gdialog) --title icculus.org/quake3 --msgbox" xdialog="$(which gdialog) --title icculus.org/quake3 --yesno" || xecho="$(which xmessage)" xdialog="$(which xmessage) -buttons OK:0,Cancel:1"
    findpak 
    [ $? -ne 0 ] && $xdialog "Data files not found.  Insert Quake 3 Arena CD and try again."
    [ $? -ne 0 ] && findpak
    [ $? -ne 0 ] && $xecho "Data files not found.  Exiting." && exit 1
    $xdialog "Data file found in ${pak}.  Press OK to copy."
    copypak
    exit $?
else
    xecho=echo
    findpak || exit 1
    copypak
    exit $?
fi


