#!/bin/sh
#
# ggcov - A GTK frontend for exploring gcov coverage data
# Copyright (c) 2001-2004 Greg Banks <gnb@users.sourceforge.net>
# 
# 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
# 
# $Id: buildrpm,v 1.12 2010-05-09 05:37:14 gnb Exp $
#

FORCE=no
CVSUP=no
BUILDHOST=
LOCALHOST=$(uname -n|cut -d. -f1)
RPMDEST=
# This file is both controlled and also emitted as part of the build,
# which is probably a bad idea, but regardless it needs to be ignored
# when checking for modified files.
CVSIGNORES="aclocal.m4"

fatal ()
{
    echo "ERROR: $*" >&2
    exit 1
}

usage ()
{
    echo "Usage: $0 [--force] [--cvs-update] [--build-host=host] [--rpm-destination=[host:]dir]"
    exit 1
}

boolopt ()
{
    case "${2#*=}" in
    yes) eval $1=yes ;;
    no) eval $1=no ;;
    *) fatal "Bad boolean option \"$2\"" ;;
    esac
}

while [ $# -gt 0 ]; do
    case "$1" in
    --force|-f) boolopt FORCE yes ;;
    --force=*) boolopt FORCE $1 ;;
    -h|--build-host) BUILDHOST=$2 ; shift ;;
    --build-host=*) BUILDHOST=${1#*=} ;;
    -u|--cvs-update) boolopt CVSUP yes ;;
    --cvs-update=*) boolopt CVSUP $1 ;;
    -d|--rpm-destination) RPMDEST=$2 ; shift ;;
    --rpm-destination=*) RPMDEST=${1#*=} ;;
    *) usage ;;
    esac
    shift
done

# echo FORCE=$FORCE
# echo BUILDHOST=$BUILDHOST
# echo CVSUP=$CVSUP
# echo RPMDEST=$RPMDEST
# exit

case "$BUILDHOST" in
""|"$LOCALHOST"|localhost)
    echo "Building on local host ($LOCALHOST)"
    exec 2>&1 | tee build.$LOCALHOST.log
    ;;
*)
    echo "Building on remote host \"$BUILDHOST\""
    (
	set -x
	ssh $BUILDHOST "cd $PWD && \
    	    ./buildrpm \
	    	--cvs-update=$CVSUP \
		--force=$FORCE \
		--rpm-destination=${RPMDEST:-$LOCALHOST:$PWD}"
    ) 2>&1 | tee build.$BUILDHOST.log
    exit
    ;;
esac

set -- `sed -n -e 's|^AM_INIT_AUTOMAKE(\([^,]\+\),\([^)]\+\))|\1 \2|p' < configure.in` notfound
if [ "$1" = notfound ]; then
    AUTOMAKE=no
    eval `egrep '^(PACKAGE|VERSION)=' < configure.in`
else
    AUTOMAKE=yes
    PACKAGE=$1
    VERSION=$2
fi

SPECFILE=${PACKAGE}.spec
RPMDIR=rpm.d

TARBALL=`awk '/^Source:/{print $2}' < $SPECFILE.in`
case "$TARBALL" in
*.tar.gz) TARBALL=${PACKAGE}-${VERSION}.tar.gz ;;
*.tgz) TARBALL=${PACKAGE}-${VERSION}.tgz ;;
*.tar.bz2) TARBALL=${PACKAGE}-${VERSION}.tar.bz2 ;;
*) fatal "cannot calculate tarball extension from $SPECFILE" ;;
esac

if [ -x /usr/bin/rpmbuild ] ; then
    RPMBUILD=rpmbuild
else
    RPMBUILD=rpm
fi

# Calculate a string which identifies the Linux distro
DISTRO=
if [ -f /etc/redhat-release ]; then
    # Format for RH 7,8.9:
    # Red Hat Linux release 9 (Shrike)
    # Format for Fedora Core 1:
    # Fedora Core release 1 (Yarrow)
    DISTRO=$(sed \
    	-e 's|.*Red[ \t]*Hat[ \t]\+Linux[ \t]\+|rh|' \
	-e 's|.*Fedora[ \t]\+Core[ \t]\+|fc|' \
	-e 's|[ \t]*release[ \t]\+\([^ \t]\+\).*|\1|g' < /etc/redhat-release)
fi

#echo PACKAGE=$PACKAGE
#echo VERSION=$VERSION
#echo TARBALL=$TARBALL
#echo RPMDIR=$RPMDIR
#echo DISTRO=\"$DISTRO\"
#echo RPMBUILD=$RPMBUILD
#echo CVSIGNORES=\"$CVSIGNORES\"
#echo FORCE=$FORCE
#exit 1


if [ $CVSUP = yes ]; then
    echo "Checking for locally changed files"

    CVSIGNORES_RE=$(echo $CVSIGNORES | sed -e 's/^[ \t]*/^(/g'  -e 's/[ \t]*$/)$/g' -e 's/[ \t][ \t]*/|/g')

    CHANGED=$(cvs -nq up | awk '$1~/^[MC]$/ && $2!~/'$CVSIGNORES_RE'/{print $2}')
    [ -z "$CHANGED" ] || \
    	fatal "These files are changed in this work area: $CHANGED"
    echo "Updating files from CVS"
    (
    	set -x
    	cvs -q up || exit 1
    ) || exit 1
fi

if [ $FORCE = yes ]; then
    echo "Removing files to force rebuild"
    (
    	set -x
	/bin/rm -rf config.status config.cache autom4te.cache configure
	/bin/rm -f $SPECFILE $TARBALL
    )
fi

if [ ! -f $SPECFILE ]; then
    echo "Specfile missing, building"
    (
    	set -x
	aclocal
	autoconf
	autoheader
	test "$AUTOMAKE" = yes && automake -a
	./configure
    )
    [ -f $SPECFILE ] || fatal "Can't build $SPECFILE, giving up"
fi

if [ ! -f $TARBALL ]; then
    echo "Tarball missing, building"
    (
    	set -x
	make dist
    )
    [ -f $TARBALL ] || fatal "Can't build $TARBALL, giving up"
fi

echo "Building $RPMDIR directory tree"
/bin/rm -rf $RPMDIR
mkdir -p $RPMDIR
(cd $RPMDIR ; mkdir -p BUILD RPMS/i386 RPMS/`uname -m` SPECS SRPMS )

echo "Building RPMs"
(
    set -x
    $RPMBUILD -bb \
	--define "_topdir $PWD/$RPMDIR" \
	--define "_sourcedir $PWD" \
	--verbose \
	$SPECFILE || exit 1
)

echo "Copying RPMs to destination"
for f in $(find $RPMDIR/RPMS -type f -name \*.rpm) ; do
    f2=$(echo $(basename $f) | sed -e 's|^\([a-z-]\+-\)|\1'${DISTRO}${DISTRO:+-}'|')
    if [ -z "$RPMDEST" ]; then
	echo "Moving $f -> $f2"
	mv $f $f2 || exit 1
    else
	echo "Copying $f -> $RPMDEST/$f2"
	scp $f $RPMDEST/$f2 || exit 1
    fi
done

