#!/bin/sh
##
#  Korinf project
#
#  Windows build related functions
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005, 2006, 2007, 2009
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.

#  This program 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 Affero General Public License for more details.

#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

# FIXME
# BUILDROOT - root of current system
# BUILDERHOME - absolute path to user dir in current system

load_mod rpm build

#$PACKAGEVERSION
#$PACKAGERELEASE

# FIXME
HOMEDIR=~/
RPMDIR=$HOMEDIR/RPM
#BUILDUTILSDIR=$HOMEDIR/Projects/etersoft-build-utils
#INTBUILT=/home/$INTUSER/$RPMSDIR
#RPMARGS="$(get_rpm_args $dist $BUILDNAME)"

#dirty hack for postgresql
if [ `echo $BUILDNAME | grep [0-9]` ] ; then
	WINNAME=`echo $BUILDNAME | sed -r "s|^([a-z]*)-[0-9]*.*|\1|"`
else
	WINNAME=$BUILDNAME
fi
echo $WINNAME

build_windows()
{
	# prevent gear detecting (operate inside our dir)
	export IGNOREGEAR=1
	# NOTE: we can do cd to some other dir...
	export RPMTOPDIR=$RPMDIR/BPW
	uni_rpminstall $BUILDSRPM

        #mkdir -p $RPMDIR/SPECS $RPMDIR/SRPMS
        #rpm -ivv $BUILDERHOME/tmp/${WINNAME}*.src.rpm

        cd $RPMTOPDIR/SPECS
		#TODO: test specfile?
		#TODO: use uni_rpmbuild
		#unpack sources and apply patches
		uni_rpmbuild ${WINNAME}*.spec -bp -v --nodeps || return
        cd -

	ls -l $RPMTOPDIR/BUILD/$WINNAME*/
	cd $RPMTOPDIR/BUILD/$WINNAME*/${WINNAME}*win* || return
		./windows.sh $RPMTOPDIR $PACKAGERELEASE $PACKAGEVERSION || return
	cd -

	# Expand MAINFILELIST, EXTRAFILELIST
	prepare_filelist
}

copying_windows()
{
	echo "Copying to ftp"
	mkdir -p $DESTDIR
	clean_copying_destination
        cp -f $RPMTOPDIR/BUILD/$WINNAME*/${WINNAME}*installer*/*.exe $DESTDIR || { warning "Cannot copy packages" ; return 1; }
	echo "Done with copying"
        chmod g+rw -R $DESTDIR/* || true
}

clean_windows()
{
	echo "Cleaning $RPMTOPDIR/BUILD/$WINNAME*"
	rm -rf $RPMTOPDIR/BUILD/$WINNAME*
        #true
}
