#!/bin/bash

# This script tries to find all of the resources from Quake3 Arena
# that are not available in the installed openarena
#
# author: die_z
# licence: GPLv3
#
# example call: ./q3a2oadiff /usr/games/quake3/baseq3/


## variables
START=$(date +%s)
OAPACKAGE="openarena-data"
Q3ABASEDIR="/usr/local/games/quake3/baseq3/"
OABASEDIR="/usr/share/games/openarena/baseoa/"
CWD="$(pwd)"
TMP="/tmp"
WD="q3a2oadiff"
Q3ADIR="q3a-resources"
OADIR="oa-resources"
DIFFDIR="diff-resources"
PAKNAME="pak8-q3astuff.pk3"


## initial tests
echo -n "Checking for presence of package $OAPACKAGE..."
if [ ! "$(aptitude search ~i$OAPACKAGE)" ]; then {
	echo " ouch!"
	echo "Package $OAPACKAGE seems not to be installed!"
	echo "I cannot continue without that package, aborting..."
	echo
	exit 1
	}; else {
	echo " found!"
	echo
	}
fi

if [ $1 ]; then {
	echo "Quake3 Arena's pak location was given, trying it..."
	if [ -d $1 ] && [ -r $1/pak0.pk3 ]; then {
		echo "Ok, it's a directory I can read a pak0.pk3 in it... fine!"
		Q3ABASEDIR="$1"
		echo "I'll use $Q3ABASEDIR as Quake3 Arena's resource directory"
		echo
		}; else {
		echo "Ouch! I cannot read a pak0.pk3 in it, I must give up, sorry..."
		echo
		exit 1
		}
	fi
	}; else {
	echo "You didn't specify Quake3 Arena's pak location!"
	echo "Looking for it in the default directory..."
	if [ -r $Q3ABASEDIR ]; then {
		echo "Got it! Let's go on..."
		echo "I'll use $Q3ABASEDIR as Quake3 Arena's resource directory"
		echo
		}; else {
		echo "Ouch! I cannot find it, I must give up, sorry..."
		echo
		exit 1
		}
	fi
	}
fi


## working setup
echo -n "Setting up temporary directories..."
	if [ ! -w "$TMP/" ]; then {
		echo "$TMP/ is not writable, I cannot do anything... aborting!"
		exit 1
		}
	fi
	cd $TMP/
	if [ -d "$WD" ]; then {
		echo "Directory $TMP/$WD/ already exists, aborting..."
		exit 1
		}
	fi
	mkdir $WD
	cd $WD
	mkdir $Q3ADIR
	mkdir $OADIR
	mkdir $DIFFDIR
	echo " done!"
	echo
echo -n "Unpacking Quake3Arena resources..."
	cd $Q3ADIR
	for pak in $(ls $Q3ABASEDIR|grep ^pak[0-9].pk3); do {
		unzip -o $Q3ABASEDIR$pak > /dev/null
		};
	done
	echo " done!"
	echo -n "Removing videos, we don't need 'em..."
	rm -fr video
	cd ..
	echo " done!"
echo -n "Unpacking Openarena resources..."
	cd $OADIR
	for pak in $(ls $OABASEDIR); do {
		unzip -o $OABASEDIR$pak > /dev/null
		};
	done
	cd ..
	echo " done!"
	echo


## the fun part
cd $Q3ADIR
echo "Finding files only present in Quake3 Arena's resources"
echo -n "and copying those files to a new location..."
for res in $(find . -print); do {
	if [ ! -e "../$OADIR/$res" ]; then {
		tar -c $res | tar -x -C $TMP/$WD/$DIFFDIR/
		}
	fi };
done
cd ..
echo " done!"
echo
if [ -e $CWD/$PAKNAME ]; then {
	echo "$CWD/$PAKNAME already exists, removing it."
	}
fi
echo -n "Generating pak file..."
	cd $DIFFDIR
	#echo "DEBUG: I'm in $(pwd), started from $CWD"
	#echo "DEBUG: $(ls -ld $pwd)"
	#echo "DEBUG: $(ls -ld $CWD)"
	#echo "DEBUG: these are the files I see..."
	#echo "$(ls -l)"
	#zip -R -X $CWD/$PAKNAME . -i *
	zip -R -q -X $CWD/$PAKNAME *
echo " done!"
echo


## cleanup
echo -n "Cleaning up temporary working set..."
cd $TMP/
rm -fr $WD
cd $CWD
echo " done!"
echo
echo "OK, we're finished, here you have this newly generated $PAKNAME."
STOP=$(date +%s)
TOTALSECS=$[$STOP-$START]
MINS=$[$TOTALSECS/60]
SECS=$[$TOTALSECS-$MINS*60]
echo "It took me $MINS minutes and $SECS seconds to do it all"
echo "Have fun!"

