#! /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
	;;
    *)
	break
	;;
    esac
done

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

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

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

# Sanity checks ... this is sick, but I'm really tired of QA failing
# because of bad binaries being built from the Ubuntu toolchain for
# i?86 platforms
# - Ken McDonell Apr 2010
#
if [ -f /etc/lsb-release -a "$ARCH" = "ia32" ]
then
    if grep -q 'DISTRIB_ID=Ubuntu' /etc/lsb-release
    then
	if [ -f /usr/bin/dpkg-buildpackage ]
	then
	    ok=true
	    if grep -q '^my $default_flags .*O2' /usr/bin/dpkg-buildpackage
	    then
		echo '/usr/bin/dpkg-buildpackage: need to remove O2 from $default_flags'
		ok=false
	    fi
	    if grep -q '^[ 	]*LDFLAGS.*-Bsymbolic-functions' /usr/bin/dpkg-buildpackage
	    then
		echo '/usr/bin/dpkg-buildpackage: need to remove -Bsymbolic-function from LDFLAGS'
		ok=false
	    fi
	    if $ok
	    then
		:
	    else
		echo "Refer to Ubuntu notes in PCP's ./INSTALL"
		exit 1
	    fi
	fi
    fi
fi

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 "== autoconf, log is in $LOGDIR/autoconf"
    autoconf 2>&1 >$LOGDIR/autoconf
fi

LOGF=$LOGDIR/pcp
echo
echo "== Building pcp, log is in $LOGF"

. ./VERSION.pcp

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_pcp 2>&1 | tee -a $LOGF
    else
	$MAKE -j 1 configure_pcp 2>&1 >> $LOGF
    fi

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

    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

    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
fi

# Fall through for MinGW (Win32) builds
if $verbose ; then
    st=`(($MAKE -j 1 default_pcp 2>&1; echo $? >&3) | tee $LOGF 1>&2) 3>&1`
else
    $MAKE -j 1 default_pcp > $LOGF 2>&1; st=$?
fi
if [ $st -ne 0 ] ; then
    if ! $verbose ; then
	echo \"$MAKE default_pcp\" failed, see log in $LOGF
	tail $LOGF
    fi
    exit 1
fi

rm -f files.rpm
echo
echo "== Packaging pcp, log is in $LOGF" | tee -a $LOGF
if $verbose ; then
    ($MAKE -j 1 -C build pack_pcp 2>&1 || touch $tmp.failed) | tee -a $LOGF
else
    ($MAKE -j 1 -C build pack_pcp 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
