#!/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
#

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
    [ -x /usr/bin/rpmbuild ] && dorpm=true
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
elif $dorpm
then
    configopts=`rpmbuild --eval '--prefix=%{_prefix} --exec-prefix=%{_exec_prefix} --bindir=%{_bindir} --sbindir=%{_sbindir} --sysconfdir=%{_sysconfdir} --datadir=%{_datadir} --includedir=%{_includedir} --libdir=%{_libdir} --libexecdir=%{_libexecdir} --localstatedir=%{_localstatedir} --sharedstatedir=%{_sharedstatedir} --mandir=%{_mandir}' 2>/dev/null`
    # rpmbuild clears the environment, so force these settings into the
    # rpm build configure
    #
    configopts="$configopts --with-make=$MAKE --with-tar=$TAR --with-zip=$ZIP"
elif [ $target = darwin ]
then
    # On Mac OS X platforms, we install away from /usr/bin, /usr/lib
    # and so on because of security requirements in recent versions.
    #
    configopts="--prefix=/usr/local --sysconfdir=/etc --localstatedir=/var"
elif [ $target = netbsd ]
then
    # Try to mimic where pkgsrc/pkgin place things
    #
    configopts="--prefix=/usr/pkg --exec-prefix=/usr/pkg --mandir=/usr/pkg/man --with-rcdir=/etc/rc.d --localstatedir=/usr/pkg --with-rundir=/var/run --with-tmpdir=/var/tmp --with-logdir=/var/log/pcp"
elif [ $target = freebsd ]
then
    # Python build does not work on FreeBSD 9.3, so skip it
    #
    case `uname -r`
    in
	9.3*)
		# default case, just without python
		#
		configopts="--prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-python=no --with-python3=no"
		;;
	*)
		# default case, as below ...
		#
		configopts="--prefix=/usr --sysconfdir=/etc --localstatedir=/var"
		;;
    esac
else
    # default case, we know no better
    #
    configopts="--prefix=/usr --sysconfdir=/etc --localstatedir=/var"
fi

case "$target"
in
    mingw)
	configopts="$configopts --disable-ssp --without-threads"
	;;
    linux)
	ARCH=`uname -m`
	[ -f /etc/slackware-version -a "$ARCH" = x86_64 ] && configopts="$configopts --libdir=/usr/lib64"
	if `which dpkg >/dev/null 2>&1`
	then
	    configopts="$configopts --libexecdir=/usr/lib"
	fi
	;;
    sunos)
	ARCH=`isainfo -k`
	[ "$ARCH" = amd64 -o "$ARCH" = sparcv9 ] && configopts="$configopts --libdir=/usr/lib/64"
	;;
esac

# now some specific QA/development hosts with special needs ...
#
case `hostname`
in
    vm11|vm11.localdomain)
	# gcc -O2 is broken here (for the linux PMDA)
	#
	configopts="$configopts --without-optimization"
	;;
esac

echo "Using: configure $configopts ..."

./configure $configopts

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

#debug# exit 0

. src/include/pcp.conf

# build some useful libs ...
#
here=`pwd`
cd src/include
$PCP_MAKE_PROG
cd ../libpcp
$PCP_MAKE_PROG
cd ../libpcp_pmda
$PCP_MAKE_PROG
cd ../pmns
$PCP_MAKE_PROG
cd ../pmcpp
$PCP_MAKE_PROG
cd ../newhelp
$PCP_MAKE_PROG
cd $here

# check expected files are the same
#

tmp=/var/tmp/$$
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)"
