#!/bin/sh
##
#  Korinf project
#
#  FreeBSD 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/>.
##

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

FREEBSDSSH=$INTUSER@freebsd
#FREEBSDSSH=$LOCUSER@freebsd

BSDPREF="/mnt/chroot/$DISTRVERSION"

# possibly conflict
FREEBSDPATH=home/$INTUSER/work-$PACKAGE

FREEBSDKEY="-i $PRIVATESSHKEY"

RPMDIRPATH="/home/$INTUSER/RPM"
TARGETPKG=$PACKAGE-$PACKAGEVERSION-$PACKAGERELEASE$PKGVENDOR


# PACKAGE
build_bsd()
{

	echo "Copying to $FREEBSDSSH:$BSDPREF/$FREEBSDPATH"
	ssh $FREEBSDKEY $FREEBSDSSH mkdir -p $BSDPREF/$FREEBSDPATH/ || { warning "Cannot create dir $FREEBSDPATH" ; return 1 ; }
	scp $FREEBSDKEY $TARGETSRPM $FREEBSDSSH:$BSDPREF/$FREEBSDPATH/ || { warning "Cannot copy $TARGETSRPM" ; return 1 ; }
	# TODO: filter the script
	scp $FREEBSDKEY $KORINFDIR/korinf/remote-scripts/remote-freebsd.sh $FREEBSDSSH:$BSDPREF/$FREEBSDPATH/ || { warning "Cannot copy script" ; return 1 ; }
	echo "Copying done"

	# from man su: Note that all command line arguments before the target login name are processed by su itself,
	# everything after the target login name gets passed to the login shell.
	ssh $FREEBSDKEY $FREEBSDSSH "sudo chroot $BSDPREF su - $INTUSER /$FREEBSDPATH/remote-freebsd.sh build $INTUSER $RPMDIRPATH $PACKAGE $PACKAGEVERSION /$FREEBSDPATH/$(basename $TARGETSRPM) $TARGETPKG" || { warning "Can't build" ; return 1 ; }
}

convert_bsd()
{
	ssh $FREEBSDKEY $FREEBSDSSH "sudo chroot $BSDPREF su - $INTUSER /$FREEBSDPATH/remote-freebsd.sh convert $INTUSER $RPMDIRPATH $PACKAGE $PACKAGEVERSION /$FREEBSDPATH/$(basename $TARGETSRPM) $TARGETPKG.$PKGFORMAT $TARGETPKG.$PKGFORMAT" || { warning "Can't clean" ; return 1 ; }
}

copying_bsd()
{
	clean_copying_destination

	scp $FREEBSDKEY $FREEBSDSSH:$BSDPREF/var/tmp/korinfer/work-$PACKAGE/$TARGETPKG.$PKGFORMAT $DESTDIR || { warning "Cannot copy $PACKAGE" ; return 1 ; }
	mark_build_done
}

cleaning_bsd()
{
	true
	#ssh $FREEBSDKEY $FREEBSDSSH "sudo chroot $BSDPREF /$FREEBSDPATH/remote-freebsd.sh clean $INTUSER $RPMBUILDROOT $PACKAGE /$FREEBSDPATH/$TARGETSRPM $TARGETPKG" || { warning "Can't clean" ; return 1 ; }
}

install_bsd()
{
	ssh $FREEBSDKEY $FREEBSDSSH "sudo chroot $BSDPREF /$FREEBSDPATH/remote-freebsd.sh install $INTUSER $RPMDIRPATH $PACKAGE $PACKAGEVERSION /$FREEBSDPATH/$(basename $TARGETSRPM) $TARGETPKG.$PKGFORMAT" || { warning "Can't install" ; return 1 ; }
}
