#!/bin/sh
#
# $CoreSDI: build,v 1.7 2002/01/02 20:56:43 claudio Exp $
#
# Copyright (c) 2000, 2001, Core SDI S.A., Argentina
# All rights reserved
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither name of the Core SDI S.A. nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#


function fatal_exit
{
	echo "$1"
	exit 1
}


function rmremote
{
	echo "  Cleaning remote files..."
	if test "$RSH" = "ssh"
	then
		if ! ssh -1 -i $IDENTITY $RUSER@$HOSTNAME "rm -rf $1"
		then
			fatal_exit "Build failed."
		fi
	else
		fatal_exit "Remote shell not supported: $RSH."
	fi
}


function local2remote
{
	echo "  Transferring $1 to $HOSTNAME..."
	if test "$TRANSFER" = "scp"
	then
		if ! test -f "$IDENTITY"
		then
			fatal_exit "Missing ssh-1 identity file."
		fi
		if ! scp -oProtocol=1 -i $IDENTITY $1 $RUSER@$HOSTNAME:~/ 
		then
			fatal_exit "Transfer failed."
		fi
	else
		fatal_exit "Transfer method '$TRANSFER' not supported."
	fi
}


function remote2local
{
	echo "  Transferring $1 from $HOSTNAME to $2..."
	if test "$TRANSFER" = "scp"
	then
		if ! test "$IDENTITY"
		then
			fatal_exit "Missing ssh-1 identity file."
		fi
		if ! scp -oProtocol=1 -i $IDENTITY $RUSER@$HOSTNAME:~/$1 $2
		then
			fatal_exit "Transfer failed."
		fi
	else
		fatal_exit "Transfer method '$TRANSFER' not supported."
	fi
	chmod 444 $2/*
}


function do_binaries
{
	echo "  Building binary packages..."
	if test "$RSH" = "ssh"
	then
		if ! ssh -1 -i $IDENTITY $RUSER@$HOSTNAME ./$BUILD
		then
			fatal_exit "Build failed."
		fi
	else
		fatal_exit "Remote shell not supported: $RSH."
	fi
}


function do_checksum
{
	echo "  Making MD5 checksum on $1..."
	cd $1 && \
	md5 * > MD5 && \
	chmod 444 MD5 && \
	cd - && return 0
	fatal_exit "Can't do checksum on $1."
}


#
# Remote hosts specification file
#
HOSTSFILE=../../remote/hosts


#
# Product name and version number
#
PRODUCT=audit
VERSION=`awk -F \" '/AUDIT_VERSION/ { print $2 }' ../src/version.h`


#
# Current release and sources tar ball.
#
RELEASE=$PRODUCT-v$VERSION
RELEASEDIR=../releases/$RELEASE
PACKAGESDIR=$RELEASEDIR/packages
RPACKAGESDIR=$RELEASE
SRCS_TARBALL=$RELEASE-src.tar.gz
PORT_TARBALL=$PRODUCT.tgz
PORTSDIR=../ports
BUILD=build


#
# Check hosts file
#
if ! test -f $HOSTFILE
then
	fatal_exit "Missing remote hosts specification file: '$HOSTSFILE'."
fi

#
# Create distribution tree
#
if test -d $RELEASEDIR
then
	fatal_exit "Releases directory '$RELEASEDIR' exists."
elif ! install -d $PACKAGESDIR
then
	fatal_exit "Can't create '$PACKAGESDIR'."
fi


#
# Get sources tarball
#
if ! test -f ../$SRCS_TARBALL
then
	fatal_exit "Missing sources tarball: $SRCS_TARBALL.\nTry 'make bin'"
fi
cp ../$SRCS_TARBALL $RELEASEDIR/


#
# Make binaries on all platforms specified on hosts file
#
cat $HOSTSFILE |&
while read -p OSNAME OSVERSION ARCH HOSTNAME TRANSFER RSH IDENTITY RUSER
do
	#
	# Skip lines that begins with "# "
	#
	if test "$OSNAME" = "" -o "$OSNAME" = "#"
	then
		continue
	fi

	#
	# Create platform release directory
	#
	if ! install -d ./$PACKAGESDIR/$OSNAME-$OSVERSION/$ARCH
	then
		fatal_exit "Can't create directory: $PACKAGESDIR/$OSNAME-$OSVERSION/$ARCH."
	fi

	echo "\nBuilding binaries for $OSNAME-$OSVERSION-$ARCH..."
	if ! cp $PORTSDIR/$OSNAME/$PORT_TARBALL $PACKAGESDIR/$OSNAME-$OSVERSION
	then
		exit 1
	fi

	#
	# Send port tar ball, build script, and sources tar ball
	# to the remote host
	#
	rmremote $PRODUCT* $BUILD
	local2remote $PORTSDIR/$OSNAME/$PORT_TARBALL
	local2remote $PORTSDIR/$OSNAME/$BUILD
	local2remote $RELEASEDIR/$SRCS_TARBALL

	#
	# Compile and make binaries on the remote host
	#
	do_binaries

	#
	# Copy binary packages from remote into the local release directory
	#
	remote2local $RPACKAGESDIR/* $PACKAGESDIR/$OSNAME-$OSVERSION/$ARCH

	#
	# Make checksum on platform tree
	#
	do_checksum $PACKAGESDIR/$OSNAME-$OSVERSION/$ARCH
	do_checksum $PACKAGESDIR/$OSNAME-$OSVERSION
done


#
# Make checksum on base release tree
#
do_checksum $PACKAGESDIR
do_checksum $RELEASEDIR


