#!/bin/sh
#
# Rebuild core libraries and QA apps in a git tree ...
# - run configure the same way Makepkgs does [not with -q]
# - turn off gcc optimization so gdb has a better chance
#   (be aware that this may hide gcc optimizations in the real
#   package builds)
# - make clean [not with -q]
# - 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 those 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

quick=false
while [ $# -gt 0 ]
do
    case "$1"
    in
	-q)
		quick=true
		shift
		;;
	*)
		echo "Error: no clue about arg \"$1\""
		exit
		;;
    esac
done

# configure unless -q
#
if $quick
then
    :
else
    # -q here so we don't build after configure ... all the building
    # is prescriptive in this script in the following sections
    #
    if qa/admin/myconfigure -q
    then
	:
    else
	echo "Error: myconfigure -q failed"
	exit
    fi
fi

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 vendor \
	src/include src/libpcp src/libpcp_static src/libpcp_pmda \
	src/pmcpp src/pmns \
	src/newhelp \
	src/libpcp_pmcd src/libpcp_gui \
	src/libpcp_mmv src/libpcp_web src/libpcp_import src/libpcp_trace \
	src/libpcp_qmc src/libpcp_fault src/libpcp3 \
	qa/pmdas qa/perfevent qa/qt qa/src
do
    if [ ! -d "$dir" ]
    then
	echo "Arrgh! dir \"$dir\" does not exist"
	exit
    fi
    echo "=== $dir ==="
    cd $dir
    [ -d src ] && cd src

    # make clean except for -q
    #
    if $quick
    then
	:
    else
	case "$dir"
	in
	    src/include|src/pmns)
		    ;;
	    src/libpcp3)
		    KEEP_CONFIG_FILES=yes $PCP_MAKE_PROG clean
		    ;;
	    *)
		    $PCP_MAKE_PROG clean
		    ;;
	esac
    fi

    # make, then for libs sudo make install
    #
    if $PCP_MAKE_PROG
    then
	case "$dir"
	in
	    src/lib*)
		    if sudo $PCP_MAKE_PROG install
		    then
			:
		    else
			echo "Error: sudo make install failed in `pwd`!"
			exit
		    fi
		    ;;
	esac
    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
