#!/bin/bash

#
#  G N O K I I
#
#  A Linux/Unix toolset and driver for the mobile phones.
#
#  This file is part of gnokii.
#
#  Gnokii is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  Gnokii is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with gnokii; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#  Copyright (C) 2006-2007       Jari Turkia
#
#  Nightly build script.
#

##
# Configurable parameters
##
GNOKIIDIR=~/c/Gnokii
RPMBUILDDIR=~/rpmbuild
ARCH=x86_64
FTPHOST="-missing-"
FTPUSER="-missing-"
FTPPASSWORD="-missing-"
FTPDIR="-missing"
ADDITIONALFILES="gnokii-smsd2mail.sh gnokii-smsd.logrotate gnokii-smsd.sysconfig gnokii-smsd.init gnokii-smsd-README.smsd2mail"
FTPURL="ftp://$FTPUSER:$FTPPASSWORD@$FTPHOST/$FTPDIR"
#SFTPURL="$FTPUSER@$FTPHOST"
VERSION=unknown
REVISION=`date "+%Y%m%d"`

##
# Do not change anything below!
##

## Testing secure FTP
if [ 1 == 0 ]; then
	# Success
	sftp $SFTPURL 2>&1 <<End-of-Session
cd $FTPDIR
dir
rm *
lcd $RPMBUILDDIR/RPMS/$ARCH/
put gnokii-*
put xgnokii-*
lcd $RPMBUILDDIR/SRPMS/
put gnokii-*
End-of-Session
	exit
fi
## end test secure FTP

## Testing NcFTP
if [ 1 == 0 ]; then
	# Success
	ncftp $FTPURL > /tmp/fail-$VERSION-$REVISION.log 2>&1 <<End-of-Session
bin
dir
rm *
dir
lcd $RPMBUILDDIR/RPMS/$ARCH/
mput gnokii-*
lcd $RPMBUILDDIR/SRPMS/
mput gnokii-*
End-of-Session
	exit
fi
## end test NcFTP


# Part 1:
rm -f $RPMBUILDDIR/RPMS/$ARCH/gnokii-*
rm -f $RPMBUILDDIR/RPMS/$ARCH/xgnokii-*
rm -f $RPMBUILDDIR/SOURCES/gnokii-*
rm -f $RPMBUILDDIR/SRPMS/gnokii-*
rm -Rf $RPMBUILDDIR/BUILD/gnokii-*

cd $GNOKIIDIR/gnokii/
cvs update -CPdA 2>&1
stat=$?
if [ $stat != 0 ]; then
	echo CVS update failed with code: $stat
	exit $stat
fi

#VERSION=`cat $GNOKIIDIR/gnokii/VERSION`
macros="define(\`AC_INIT', \`divert(0)\$2divert(-1)')divert(\`-1')"
VERSION=`echo ${macros} | m4 - configure.in | sed -e "s/\[//" | sed -e "s/]//"`
echo Version: $VERSION-$REVISION

cd $GNOKIIDIR
mkdir /tmp/gnokii-$VERSION
cp -R gnokii/* /tmp/gnokii-$VERSION
stat=$?
if [ $stat != 0 ]; then
	echo Cannot make a temporary copy of Gnokii source. cp failed with: $stat
	exit $stat
fi

cd /tmp
tar jcf gnokii-$VERSION.tar.bz2 gnokii-$VERSION/
stat=$?
if [ $stat != 0 ]; then
	echo Tar failed with: $stat
	exit $stat
fi

cp gnokii-$VERSION.tar.bz2 $RPMBUILDDIR/SOURCES
rm -Rf gnokii-$VERSION.tar.bz2 gnokii-$VERSION

# Part 2:
for a in $ADDITIONALFILES; do
	cp $GNOKIIDIR/nightly/files/$a $RPMBUILDDIR/SOURCES
done

sed "s/VVVVVV/$VERSION/" $GNOKIIDIR/nightly/gnokii.spec | sed "s/RRRRRR/$REVISION/" > $RPMBUILDDIR/SPECS/gnokii.spec
stat=$?
if [ $stat != 0 ]; then
	echo Sed failed with code: $stat
	exit $stat
fi

# Part 3:
cd $RPMBUILDDIR
rpmbuild -ba SPECS/gnokii.spec > /tmp/fail-$VERSION-$REVISION.log 2>&1
stat=$?
if [ $stat != 0 ]; then
	# Failure
	if [ "$SFTPURL" == "" ]; then
		# Regular FTP
		ncftp $FTPURL 2>&1 <<End-of-Session
bin
rm fail-*
lcd /tmp
put fail-$VERSION-$REVISION.log
End-of-Session
	else
		# Secure FTP
		sftp $SFTPURL 2>&1 <<End-of-Session
cd $FTPDIR
dir
rm fail-*
lcd /tmp
put fail-$VERSION-$REVISION.log
End-of-Session
	fi

	rm -f /tmp/fail-$VERSION-$REVISION.log
	echo Build failed with code: $stat
	exit $stat
else
	# Success
	if [ "$SFTPURL" == "" ]; then
		# Regular FTP
		ncftp $FTPURL > /tmp/fail-$VERSION-$REVISION.log 2>&1 <<End-of-Session
bin
rm *
lcd $RPMBUILDDIR/RPMS/$ARCH/
mput gnokii-*
mput xgnokii-*
lcd $RPMBUILDDIR/SRPMS/
mput gnokii-*
End-of-Session
		if [ $? != 0 ]; then
			cat /tmp/fail-$VERSION-$REVISION.log 1>&2
		fi
	else
		# Secure FTP
		sftp $SFTPURL > /tmp/fail-$VERSION-$REVISION.log 2>&1 <<End-of-Session
cd $FTPDIR
dir
rm *
lcd $RPMBUILDDIR/RPMS/$ARCH/
put gnokii-*
put xgnokii-*
lcd $RPMBUILDDIR/SRPMS/
put gnokii-*
End-of-Session
		if [ $? != 0 ]; then
			cat /tmp/fail-$VERSION-$REVISION.log 1>&2
		fi
	fi

	echo Success done.
fi

# Part 4:

# Clean up all the files
rm -f /tmp/fail-$VERSION-$REVISION.log
rm -f $RPMBUILDDIR/RPMS/$ARCH/gnokii-*
rm -f $RPMBUILDDIR/RPMS/$ARCH/xgnokii-*
rm -f $RPMBUILDDIR/SOURCES/gnokii-*
rm -f $RPMBUILDDIR/SRPMS/gnokii-*

