#!/bin/bash
#
# tce-packman
# Based on urbanterror.sh
# Modified by Javier Llorente, 31-07-2010


if [ "root" != `whoami` ]  
then  
echo "Please run this script as root"  
else  


File1=tcetest049.zip
File2=tce049b_all_os_fixed.zip

set -e


# check if the TCE files have been downloaded
if [ ! -f $HOME/.et-packman/enemyterritory-2.60b.autodl ]; then
echo "You have to install Enemy Territory first"
echo "Run sh /usr/share/games/enemyterritory/et-install" 


else

# check if the TCE files have been downloaded
if [ ! -f $HOME/.tce-packman/tce-0.49b.autodl ]; then
        # force update
        if [ ! -f $HOME/.tce-packman/$File1 ]; then
                set +e
		#Downloads TCE files
                /usr/share/autodl/AutoDL.py /usr/share/games/enemyterritory/tcetest/tce.autodlrc
                STATUS=$?
                set -e
        else
                STATUS=0
        fi

        # status 2 means download was ok, but the user choice not to start the game
        if [ "$STATUS" = "0" -o "$STATUS" = "2" ]; then
                pushd $HOME/.tce-packman > /dev/null
                        			
			# Extracts TCE
			mkdir $HOME/.tce-packman/tce-temp
			cd $HOME/.tce-packman/tce-temp
			
			unzip -qq -u ../$File1
			
			# Setups docs
			mv tcetest/EULA_TrueCombat_Elite.txt /usr/share/doc/packages/tce/
			mv tcetest/*.rtf /usr/share/doc/packages/tce/
			rm -rf tcetest/*.dll *.pkg *[Mm]ac* *ico
						
			# Copies the rest files
			cp -R tcetest/* /usr/share/games/enemyterritory/tcetest/
			
			# Applies update to 0.49b
			unzip -qq -u ../$File2
			rm -rf *.dll *.pkg *[Mm]ac* *ico
			cp -R * /usr/share/games/enemyterritory/tcetest/
						
			# Deletes temporary files
			rm -rf $HOME/.tce-packman/tce-temp
			cd $HOME/.tce-packman/
			
                        touch tce-0.49b.autodl
                popd > /dev/null
        fi
        if [ "$STATUS" != "0" ]; then
                exit $STATUS
        fi
fi

fi

fi