#! /bin/sh
#
# Make whichever packages the system supports
#
# Copyright (c) 2004 Silicon Graphics, Inc.  All Rights Reserved.
# 
# This program 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.
# 
# 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 General Public License
# for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
LOGDIR=Logs

clean=false
verbose=false

MAKE=${MAKE:-make}

for opt in $*
do
    case "$opt" in
    -clean)
	clean=true
	shift
	;;
    -verbose)
	verbose=true
	shift
	;;
    -*)
	echo "Unknown option $opt"
	echo "Usage: Makepkgs [-clean] [-verbose] products..."
	echo "products may be one or more of \"pcp pro\""
	exit 1
	;;
    *)
	break
	;;
    esac
done

if [ $# = 0 ] ; then
    set -- pcp
fi

test ! -d $LOGDIR && mkdir $LOGDIR
rm -rf $LOGDIR/* 

tmp=/tmp/$$
trap "rm -f $tmp.*;" 0 1 2 3 15

if $clean; then
    echo "== cleaning, log is in $LOGDIR/clean"
    if $verbose ; then
	($MAKE -j 1 clean 2>&1 || touch $tmp.failed )| tee $LOGDIR/clean
    else
	$MAKE -j 1 clean > $LOGDIR/clean 2>&1  || touch $tmp.failed
    fi
    if [ -f $tmp.failed ] ; then
	if ! $verbose ; then
	    echo \"$MAKE clean\" failed, see log in $LOGDIR/clean
	    tail $LOGDIR/clean
	fi
	exit 1
    fi
fi

echo
if [ ! -f ./configure ]
then
    echo "== autconf, log is in $LOGDIR/autoconf"
    autoconf 2>&1 >$LOGDIR/autoconf
fi

ARCH=`uname -m | sed -e 's/i.86/ia32/'`
VERS=`uname -s | cut -c-5`

for prod ; do
    LOGF=$LOGDIR/$prod
    echo
    echo "== Building $prod, log is in $LOGF"

    . VERSION.$prod

    if [ "$prod" = "pro" ]
    then
    	if [ ! -f src/libpcp/src/libpcp.so.2 -o ! -f src/pmns/stdpmid ]
	then
	    echo "Error: \"pcp\" must be built before (or with) \"pro\""
	    exit 1
	fi

	#
	# unpack the root for the pcp-pro build
	[ -z "$ROOT" ] && export ROOT=`pwd`/root-$ARCH
	if [ ! -d $ROOT/include -o ! -d $ROOT/lib ]; then make -C $ROOT; fi
    else
	if [ -n "$SGI_PROPACK_BUILD" ] ; then
	    PACKAGE_NAME=${prod}-open
	else
	    PACKAGE_NAME=${prod}
	fi
	export PACKAGE_NAME
    fi

    special=false
    [ "$VERS" = MINGW ] && special=windows
    [ -f /etc/debian_version ] && special=debian
    
    if [ $special != false ]
    then
	LOGF=`pwd`/$LOGF

	if $verbose ; then
	    $MAKE -j 1 configure_$prod 2>&1 | tee -a $LOGF
	else
	    $MAKE -j 1 configure_$prod 2>&1 >> $LOGF
	fi

	VERSION=${PACKAGE_MAJOR}.${PACKAGE_MINOR}.${PACKAGE_REVISION}
	SRCLINK_ROOT=`pwd`/pcp-$VERSION
	if [ $special = debian ] ; then
	    SRCLINK_ROOT=`pwd`/build/deb/pcp-$VERSION
	fi
	export SRCLINK_ROOT

	rm -fr $SRCLINK_ROOT
	mkdir -p $SRCLINK_ROOT || exit 1
	$MAKE -j 1 src-link-pcp || exit 1
	cd $SRCLINK_ROOT
	[ ! -f .census ] && touch .census

	if [ $special = debian ]
	then
	    SUDO=${SUDO:-fakeroot}
	    if $verbose ; then
		dpkg-buildpackage -r$SUDO | tee -a $LOGF
	    else
		dpkg-buildpackage -r$SUDO >> $LOGF || exit 1
	    fi
	    exit 0
	fi
	# Fall through for MinGW (Win32) builds
    fi

    if $verbose ; then
	st=`(($MAKE -j 1 default_$prod 2>&1; echo $? >&3) | tee $LOGF 1>&2) 3>&1`
    else
	$MAKE -j 1 default_$prod > $LOGF 2>&1; st=$? 
    fi
    if [ $st -ne 0 ] ; then
	if ! $verbose ; then
	    echo \"$MAKE default_$prod\" failed, see log in $LOGF
	    tail $LOGF
	fi
	exit 1
    fi

    rm -f files.rpm
    echo
    echo "== Packaging $prod, log is in $LOGF" | tee -a $LOGF
    [ ! -f .census ] && touch .census
    if $verbose ; then
	($MAKE -j 1 -C build pack_$prod 2>&1 || touch $tmp.failed) | tee -a $LOGF
    else
	($MAKE -j 1 -C build pack_$prod 2>&1 || touch $tmp.failed) >> $LOGF
    fi
    if [ -f $tmp.failed ] ; then
	if ! $verbose ; then
	    echo Packaging failed, see log in $LOGF
	    tail $LOGF
	fi
	exit 1
    else
	if ! $verbose ; then
	    grep '^Wrote:' $LOGF | sed -e 's/\.\.\/\.\.\///' | fgrep -v tar.gz
	fi
    fi
done

rm -f $tmp.failed
exit 0
