#!/bin/sh
#
# Bring the Mozilla source tree up-to-date
#
# TODO: check tinderbox status:
# http://tinderbox.mozilla.org/showbuilds.cgi?tree=Project&quickparse=1
# Project in ["SeaMonkey", "Firefox", "Thunderbird", "XULRunner", "Sunbird"]

#
# Utility Functions
#


SuccessMsg()
{
    echo -e "\033[01;32m*\033[00m $*"
}

FailureMsg()
{
    echo -e "\033[01;31m*\033[00m $*"
}

cwd=`pwd`

#
# Some defaults
#
# Uncomment the following line for fetching Firefox/Thunderbird 1.5 branch
# branch=MOZILLA_1_8_BRANCH
cvsroot=:pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot
logFile="$cwd/update.out"

SuccessMsg 'Removing .mozconfig'
rm -f mozilla/.mozconfig

SuccessMsg 'Logging in to Mozilla CVS servers'
cvs -d $cvsroot login
if [ $? -ne 0 ]
then
	exit $?
fi

SuccessMsg "Logging errors and CVS output to $logFile"

if [ ! -d mozilla ]
then
	SuccessMsg 'Pulling the Mozilla source tree for the first time'
	if [ x"$branch" != x ]
	then
		cvs -d $cvsroot co -r $branch mozilla/client.mk >$logFile 2>&1
	else
		cvs -d $cvsroot co mozilla/client.mk >$logFile 2>&1
	fi
	if [ $? = 0 ]
	then
		cd mozilla
		make -f client.mk checkout \
		MOZ_CO_PROJECT=browser,mail,calendar >>$logFile 2>&1
		if [ $? = 0 ]
		then
			SuccessMsg 'Checkout complete. '\
			    'The system is ready to be built'
		else
			FailureMsg 'Unable to pull the tree successfully.'
		fi
	fi
	cd $cwd
else
	SuccessMsg 'Getting the Mozilla source tree up-to-date'
	cd mozilla
	if [ x"$branch" != x ]
	then
		cvs -q update -dr $branch client.mk >$logFile 2>&1
	else
		cvs -q update -dA client.mk >$logFile 2>&1
	fi
	if [ $? != 0 ]
	then
		FailureMsg 'Unable to update the tree successfully.'
		cd $cwd
		exit $?
	fi
	make -f client.mk checkout \
	MOZ_CO_PROJECT=browser,mail,calendar >>$logFile 2>&1
	if [ $? = 0 ]
	then
		SuccessMsg 'CVS update complete. '\
		    'The system is ready to be built'
	else
		FailureMsg 'Unable to update the tree successfully.'
	fi
	cd $cwd
fi
# EOF
