#!/bin/sh
#
# Run configure like it is run for current system ...
# the goal is to make the workarea files
# src/include/pcp.conf and src/include/builddefs match /etc/pcp.conf
# and /usr/include/pcp/builddefs so "sudo make install"
# works without surprises from anywhere in the source tree and
# "make setup" works in the qa directory
#

_usage()
{
    echo >&2 "Usage: $0 [options]"
    echo >&2 "Options:"
    echo >&2 "-q	do configure, skip make and check steps"
    exit 1
}

quick=false
while getopts 'q?' p
do
    case "$p"
    in
	q)	quick=true
		refresh=false
		;;
	?)	_usage
		# NOTREACHED
    esac
done
shift `expr $OPTIND - 1`
[ $# -ne 0 ] && _usage

if [ ! -f VERSION.pcp ]
then
    echo "Arrgh: VERSION.pcp not found, you need to be at the root of the git tree"
    exit 1
fi

. ./VERSION.pcp
version=$PACKAGE_MAJOR.$PACKAGE_MINOR.$PACKAGE_REVISION

target=`uname -s | sed -e 's/ .*//' | tr 'A-Z' 'a-z'`

# Note:	this logic needs to match the settings in Makepkgs at the
# 	top of thes source tree
#
dorpm=unknown
dodeb=unknown

if [ $dorpm = unknown ]
then
    dorpm=false
    if which slackpkg >/dev/null 2>&1
    then
	# Slackware has rpm(1) and rpmbuild(1), but don't build RPMs
	#
	:
    elif which mx-boot-repair >/dev/null 2>&1
    then
	# MX Linux has rpm(1) and rpmbuild(1), but don't build RPMs
	#
	:
    else
	[ -x /usr/bin/rpmbuild ] && dorpm=true
    fi
fi
if [ $dodeb = unknown ]
then
    dodeb=false
    [ -x /usr/bin/dpkg-buildpackage ] && dodeb=true
fi

# Hmm ...
#
if $dodeb && $dorpm
then
    echo "Do not know how to build RPM and Debian packages at the same time!"
    exit 1
fi

if $dodeb
then
    # needs to match configure_paths in debian/rules ... so just get the
    # magic setting from there
    #
    if [ ! -f debian/rules ]
    then
	echo "Botch: cannot find debian/rules"
	exit 1
    fi
    configopts=`sed -n <debian/rules -e '/^configure_paths/s/^[^=]*= *//p'`
    if [ -z "$configopts" ]
    then
	echo "Botch: cannot get configopts from configure_paths in debian/rules"
	exit 1
    fi
    # and optionally configure_tools from debian/rules also ...
    #
    configtools=`sed -n <debian/rules -e '/^configure_tools/s/^[^=]*= *//p'`
fi

# set $configopts for configure ...
#
. ./Makepkgs.configopts

echo "Using: $configtools configure $configopts ..."

if [ -x ./configure ]
then
    configure=./configure
    if which dh_auto_configure >/dev/null 2>&1
    then
	# looking good ... but for a virgin git tree we don't
	# have debian/control yet, so we may need to use ./configure
	# to bootstrap us ...
	#
	if [ -f debian/contrtol ]
	then
	    configure='dh_auto_configure --'
	fi
    fi
else
    echo "Arrgh, cannot find the configure script!"
    exit 1
fi

eval $configtools $configure $configopts

if [ ! -f src/include/pcp.conf ]
then
    echo "Arrgh, src/include/pcp.conf not found"
    exit 1
fi

. ./src/include/pcp.conf

here=`pwd`

# build critical stuff for "make setup" in qa directory
#
cd src/include; $PCP_MAKE_PROG; cd $here
cd src/pmns; $PCP_MAKE_PROG; cd $here

if $quick
then
    cd src/include; $PCP_MAKE_PROG; cd $here
    echo "== Configured pcp ($configopts)"
else
    # build some useful libs and bits ...
    #
    cd src/include; $PCP_MAKE_PROG; cd $here
    cd src/libpcp; $PCP_MAKE_PROG; cd $here
    cd src/libpcp_static; $PCP_MAKE_PROG; cd $here
    cd src/libpcp_archive; $PCP_MAKE_PROG; cd $here
    cd src/libpcp_pmda; $PCP_MAKE_PROG; cd $here
    cd src/libpcp_qmc; $PCP_MAKE_PROG; cd $here
    cd src/libpcp_web; $PCP_MAKE_PROG; cd $here
    cd src/pmcpp; $PCP_MAKE_PROG; cd $here
    cd src/newhelp; $PCP_MAKE_PROG; cd $here
    cd src/pmns; $PCP_MAKE_PROG; cd $here
fi

# check expected files are the same
#

tmp=${TMPDIR:-/tmp}/myconfigure-$$
trap "rm -rf $tmp.* $tmp; exit 0" 0 1 2 3 15

eval `grep PCP_INC_DIR= src/include/pcp.conf`

mkdir $tmp

cat <<End-of-File | while read wa_file pkg_file
src/include/pcp.conf		/etc/pcp.conf
src/include/builddefs		$PCP_INC_DIR/builddefs
src/include/pcp/config.h	$PCP_INC_DIR/config.h
src/include/pcp/configsz.h	$PCP_INC_DIR/configsz.h
src/include/pcp/platform_defs.h	$PCP_INC_DIR/platform_defs.h
src/include/pcp/platformsz.h	$PCP_INC_DIR/platformsz.h
End-of-File
do
    if [ -f "$wa_file" ]
    then
	mkdir -p `dirname "$tmp/$wa_file"`
	# no rewrite for workarea file
	cp "$wa_file" "$tmp/$wa_file"
	if [ -f "$pkg_file" ]
	then
	    mkdir -p `dirname "$tmp/$pkg_file"`
	    # for package installed file, there are some known and
	    # expected differences from workarea pathnames (relative to
	    # $(TOPDIR)) to absolute pathnames ... reverse this so we
	    # don't get false hits in the diff(1) output
	    #
	    sed <"$pkg_file" >"$tmp/$pkg_file" \
		-e 's@$(PCP_DIR)/etc/pcp.conf@$(TOPDIR)/src/include/pcp.conf@' \
		-e 's@$(PCP_BINADM_DIR)/install-sh@$(TOPDIR)/install-sh@' \
		-e 's@$(PCP_BIN_DIR)/genpmda@$(TOPDIR)/src/genpmda/genpmda@' \
		-e 's@$(PCP_INC_DIR)/buildrules@$(TOPDIR)/src/include/buildrules@' \
		-e '/PACKAGE_BUILD ?=/s/[0-9][0-9]*/1/'
	    # end
	    ( cd $tmp; diff -uw "./$wa_file" "./$pkg_file" ) >$tmp.out
	    if [ $? -eq 0 ]
	    then
		echo "Info: $pkg_file: matches configured workarea file"
	    else
		# different, strip ./ from start of file names in diff output
		sed <$tmp.out \
		    -e '/^---/s@ ./@ @' \
		    -e '/^+++/s@ ./@ @' \
		# end
		echo "Warning: $pkg_file: not the same as $wa_file"
	    fi
	else
	    echo "Warning: $pkg_file: not found, diff skipped"
	fi
    else
	echo "Error: $wa_file: missing!"
    fi
done

echo "== Configured pcp ($configopts)"
