#!/bin/sh -x
##
#  Korinf project
#
#  Solaris 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

REMOTESSH=$INTUSER@solarisz10
REMOTEPATH=/usr/home/$INTUSER/work-$PACKAGE

SOLARISKEY="-i $PRIVATESSHKEY"

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

# PACKAGE
build_solaris()
{
	echo "Copying to $REMOTESSH:$REMOTEPATH"
	ssh $SOLARISKEY $REMOTESSH "mkdir -p $REMOTEPATH" || { warning "Cannot create dir $REMOTEPATH" ; return 1 ; }
	echo "TARGETSRPM=$TARGETSRPM"
	scp $SOLARISKEY $TARGETSRPM $REMOTESSH:$REMOTEPATH || { warning "Cannot copy $TARGETSRPM" ; return 1 ; }
	# TODO: filter the script
	echo "Copying done"
	scp $SOLARISKEY $KORINFDIR/korinf/remote-scripts/remote-sunos.sh $REMOTESSH:$REMOTEPATH/ || { warning "Cannot copy script" ; return 1 ; }
	ssh $SOLARISKEY $REMOTESSH "/$REMOTEPATH/remote-sunos.sh build $INTUSER $RPMDIRPATH $PACKAGE $PACKAGEVERSION /$REMOTEPATH/$(basename $TARGETSRPM) $TARGETPKG" || { warning "Can't build" ; return 1 ; }
	true
	# Expand MAINFILELIST, EXTRAFILELIST
	prepare_filelist
}

convert_solaris()
{
	TARGETPKG=$PACKAGE-$PACKAGEVERSION-$PACKAGERELEASE$PKGVENDOR.$PKGFORMAT
	ssh $SOLARISKEY $REMOTESSH "/$REMOTEPATH/remote-sunos.sh convert $INTUSER $RPMDIRPATH $PACKAGE $PACKAGEVERSION /$REMOTEPATH/$(basename $TARGETSRPM) $TARGETPKG $TARGETPKG" || { warning "Can't clean" ; return 1 ; }
}

copying_solaris()
{
	clean_copying_destination
	echo $TARGETPKG
	scp $SOLARISKEY "$REMOTESSH:/var/tmp/korinfer/work-$PACKAGE/$TARGETPKG" $DESTDIR || { warning "Cannot copy $PACKAGE" ; return 1 ; }
	mark_build_done
}

install_solaris()
{
	ssh $SOLARISKEY $REMOTESSH "/opt/csw/bin/sudo /$REMOTEPATH/remote-sunos.sh install $INTUSER $RPMDIRPATH $PACKAGE $PACKAGEVERSION /$REMOTEPATH/$(basename $TARGETSRPM) $TARGETPKG" || { warning "Can't install" ; return 1 ; }
}

cleaning_solaris()
{
	true
}
