#! /bin/sh
#
# Make whichever packages the system supports
#
# Copyright (c) 2012-2013 Red Hat.
# 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.
#
LOGDIR=`pwd`/Logs

clean=false
verbose=false

if [ -z "$MAKE" ]
then
    MAKE=make
else
    # $MAKE in the environment, ensure it is a full path so that
    # configure ends up setting the right thing for $PCP_MAKE_PROG
    #
    try=`which $MAKE 2>/dev/null`
    if [ -z "$try" ]
    then
	echo "Error: $MAKE (from \$MAKE) does not exist!"
	exit 1
    fi
    export MAKE=$try
fi

# Bog-standard options suitable for a package on any platform.
# Command line can be used to extend this set, and any whacko
# platform-specific requirements are also appended later on.
#
configure_options="--prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-rcdir=/etc/init.d"

for opt in $*
do
    case "$opt" in
    -clean|--clean)
	clean=true
	shift
	;;
    -verbose|--verbose)
	verbose=true
	shift
	;;
    *)
	# Add remaining options to the ./configure command line
	configure_options="$configure_options $opt"
	shift
	;;
    esac
done

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

tmp=`mktemp -d /tmp/pcp.XXXXXXXXX` || exit 1
trap "rm -rf $tmp" 0 1 2 3 15

ARCH=`uname -m | sed -e 's/i.86/ia32/'`
VERS=`uname -s | cut -c-5`
case "$VERS"
in
    MINGW)
	configure_options="$configure_options --disable-ssp"
	;;
    Linux)
	configure_options="$configure_options --libexecdir=/usr/lib"
	if [ -f /etc/slackware-version ]
	then
	    if [ "$ARCH" = x86_64 ]
	    then
		configure_options="$configure_options --libdir=/usr/lib64"
	    fi
	fi
	;;
    SunOS)
	ARCH=`isainfo -k`
	if [ "$ARCH" = amd64 -o "$ARCH" = sparcv9 ]
	then
	    configure_options="$configure_options --libdir=/usr/lib/64"
	fi
	;;
esac

# Sanity checks ... this is sick, but I'm really tired of QA failing
# because of bad binaries being built from the Debian/Ubuntu toolchain
# for # i?86 platforms
# - Ken McDonell Apr 2010
#
if [ "$ARCH" = "ia32" -a -f /usr/bin/dpkg-buildpackage ]
then
    OPT=false
    if [ -f /etc/lsb-release ]
    then
	if grep -q 'DISTRIB_ID=Ubuntu' /etc/lsb-release
	then
	    eval `grep DISTRIB_RELEASE= /etc/lsb-release`
	    XDISTRIB_RELEASE=`echo $DISTRIB_RELEASE | sed -e 's/[^0-9]//g'`
	    [ -z "$XDISTRIB_RELEASE" ] && XDISTRIB_RELEASE=0000
	    if [ $XDISTRIB_RELEASE -gt 1110 ]
	    then
		# Hope the problem is fixed after Ubuntu 11.10
		OPT=true
	    fi
	fi
	$OPT || echo "Building without optimization for Ubuntu $DISTRIB_RELEASE"
    elif [ -f /etc/debian_version ]
    then
	DISTRIB_RELEASE=`cat /etc/debian_version`
	XDISTRIB_RELEASE=`echo $DISTRIB_RELEASE | sed -e 's/[^0-9]//g'`
	[ -z "$XDISTRIB_RELEASE" ] && XDISTRIB_RELEASE=0000
	if [ $XDISTRIB_RELEASE -ge 700 ]
	then
	    # Hope the problem is fixed in Debian 7.0.0
	    OPT=true
	fi
	$OPT || echo "Building without optimization for Debian $DISTRIB_RELEASE"
    fi

    if $OPT
    then
	:
    else
	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 Debian/Ubuntu notes in PCP's ./INSTALL"
		exit 1
	    fi
	fi
	# as of Debian 6.0.1 and Ubuntu 10.10, build flags are hidden and
	# extracted using dpkg-buildflags which fortunately honours some
	# environment variable settings
	#
	export DEB_CFLAGS_SET=''
	export DEB_CXXFLAGS_SET=''
	export DEB_LDFLAGS_SET=''
    fi
fi

if $clean; then
    echo "== cleaning, log is in $LOGDIR/clean"
    if $verbose ; then
	($MAKE -j 1 distclean 2>&1 || touch $tmp/failed )| tee $LOGDIR/clean
    else
	$MAKE -j 1 distclean > $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

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

. ./VERSION.pcp

echo "== Configuring pcp for vendor/distro installation ($configure_options)"
if $verbose ; then
    ./configure $configure_options 2>&1 | tee -a $LOGDIR/pcp || touch $tmp.failed
else
    ./configure $configure_options >> $LOGDIR/pcp 2>&1 || touch $tmp.failed
fi
if [ -f $tmp.failed ] ; then
    if ! $verbose ; then
        echo \"./configure\" failed, see log in $LOGDIR/pcp
        tail $LOGDIR/pcp
    fi
    exit 1
fi

# Special case removal of build artefacts that might break the build
#
rm -f qa/qa_outfiles

special=false
[ "$VERS" = MINGW ] && special=windows
[ -f /etc/debian_version ] && special=debian
    
if [ $special != false ]
then
    echo
    echo "== src-link-pcp, log is in $LOGDIR/src-link-pcp" | tee -a $LOGDIR/src-link-pcp

    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 >>$LOGDIR/src-link-pcp || exit 1
    cd $SRCLINK_ROOT

    if [ $special = debian ]
    then
	SUDO=${SUDO:-fakeroot}
	if $verbose ; then
	    dpkg-buildpackage -r$SUDO | tee -a $LOGDIR/pcp
	else
	    dpkg-buildpackage -r$SUDO >> $LOGDIR/pcp || 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 -a $LOGDIR/pcp 1>&2) 3>&1`
else
    $MAKE -j 1 default_pcp >>$LOGDIR/pcp 2>&1; st=$?
fi
if [ $st -ne 0 ] ; then
    if ! $verbose ; then
	echo \"$MAKE default_pcp\" failed, see log in $LOGDIR/pcp
	tail $LOGDIR/pcp
    fi
    exit 1
fi

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

rm -f $tmp/failed
exit 0
