#!/bin/sh
#
# Rebuild core libraries and QA apps in a git tree ...
# - run configure the same way Makepkgs does
# - turn off gcc optimization so gdb has a better chance
#   (be aware that this may hide gcc optimizations in the real
#   package builds)
# - make and install libraries over the installed libraries
#   (if any)
# - build enough libraries so "make" in the qa directory works
# - using gdb for apps outside the qa directory should work well
#   once thos apps are rebuilt
#

tmp=/var/tmp/$$
sts=1		# failure is the default until the end
trap "rm -f $tmp.*; exit \$sts" 0 1 2 3 15

if [ ! -d .git ]
then
    echo "Error: need to be at the top of the git tree, bozo!"
    exit
fi

# -q so we don't build after configure ... all the building is prescriptive
# in this script in the following sections
#
qa/admin/myconfigure -q
if [ -f src/include/pcp.conf ]
then
    . src/include/pcp.conf
else
    echo "Error: myconfigure failed to make src/include/pcp.conf"
    exit
fi
if [ -z "$PCP_MAKE_PROG" ]
then
    echo "Error: myconfigure failed to set \$PCP_MAKE_PROG"
    exit
fi

echo "Disable gcc optimization ..."
sed -e '/CFLAGS_OPT/s/-O2/-O0/' src/include/builddefs >$tmp.out
if diff src/include/builddefs $tmp.out >/dev/null
then
    echo "Warning: failed to change CFLAGS_OPTS from -O2 to -O0"
    grep CFLAGS_OPT src/include/builddefs
else
    sudo cp $tmp.out src/include/builddefs
fi

# others that could be added to the list below ...
# src/libapp src/libpcp_qed src/libpcp_qwt
#

here=`pwd`
for dir in src/libpcp src/libpcp_pmda src/libpcp_pmcd src/libpcp_gui \
	src/libpcp_web src/libpcp_mmv src/libpcp_import src/libpcp_trace \
	src/libpcp_qmc src/libpcp_fault src/libpcp_static \
	src/newhelp src/pmcpp
do
    if [ ! -d "$dir" ]
    then
	echo "Arrgh! dir \"$dir\" does not exist"
	exit
    fi
    echo "=== $dir ==="
    cd $dir
    [ -d src ] && cd src
    $PCP_MAKE_PROG clean
    if $PCP_MAKE_PROG
    then
	sudo $PCP_MAKE_PROG install
    else
	echo "Error: make failed in `pwd`!"
	exit
    fi
    cd $here
done

echo "=== qa ==="
if [ ! -d qa ]
then
    echo "Arrgh! dir \"qa\" does not exist"
    exit
fi
cd qa
if [ ! -d src ]
then
    echo "Arrgh! dir \"qa/src\" does not exist"
    exit
fi
cd src
$PCP_MAKE_PROG clean-exec
cd ..
if $PCP_MAKE_PROG
then
    :
else
    echo "Error: make failed!"
    exit
fi

# success
#
sts=0
