#! /bin/sh
#
# Make whichever packages the system supports
#
# Copyright (c) 2004 Silicon Graphics, Inc.  All Rights Reserved.
# Copyright (c) 2008 Aconex.  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.
#
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]"
	exit 1
	;;
    *)
	break
	;;
    esac
done

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

test ! -d $LOGDIR && mkdir $LOGDIR
rm -rf $LOGDIR/* > /dev/null 2>&1

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

if $clean; then
    echo "== clean, log is in $LOGDIR/clean"
    if $verbose ; then
	$MAKE clean 2>&1 | tee $LOGDIR/clean
    else
	$MAKE clean > $LOGDIR/clean 2>&1  || exit 1
    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 "== autoconf, log is in $LOGDIR/autoconf"
    autoconf 2>&1 >$LOGDIR/autoconf
fi

LOGF=$LOGDIR/pcp-gui
. ./VERSION

special=false
ARCH=`uname -m | sed -e 's/i.86/ia32/'`
VERS=`uname -s | cut -c-5`
[ "$VERS" = MINGW ] && special=windows
[ -f /etc/debian_version ] && special=debian

if [ $special != false ]
then
    LOGF=`pwd`/$LOGF

    if $verbose ; then
	$MAKE -j 1 configure-pcp-gui 2>&1 | tee -a $LOGF
    else
	$MAKE -j 1 configure-pcp-gui 2>&1 >> $LOGF
    fi

    echo
    echo "== src-link, log is in $LOGF" | tee -a $LOGF

    VERSION=${PKG_MAJOR}.${PKG_MINOR}.${PKG_REVISION}
    SRCLINK_ROOT=`pwd`/pcp-gui-$VERSION
    if [ $special = debian ] ; then
	SRCLINK_ROOT=`pwd`/build/deb/pcp-gui-$VERSION
    fi
    export SRCLINK_ROOT

    rm -fr $SRCLINK_ROOT
    mkdir -p $SRCLINK_ROOT || exit 1
    $MAKE -j 1 src-link-pcp-gui || 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

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

rm -f files.rpm
echo
echo "== Packaging, log is in $LOGF"
[ ! -f .census ] && touch .census
if $verbose ; then
    ($MAKE -j 1 -C build pack 2>&1 || touch $tmp.failed) | tee -a $LOGF
else
    ($MAKE -j 1 -C build pack 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/\.\.\/\.\.\///'
    fi
fi

rm -f $tmp.failed
exit 0
