#!/bin/sh
#
# Helper script for developer rebuild and install of PCP bits after
# libpcp_web (not a DSO) is changed.
#
# We assume you know what you're doing!
#

tmp=/var/tmp/install-dev-$$
sts=1
trap "rm -f $tmp.*; exit \$sts" 0 1 2 3 15

if make >$tmp.out 2>&1
then
    :
else
    cat $tmp.out
    echo "make in `pwd` failed!"
    exit
fi

# dirs below are relative to the base of the PCP source tree
#
cd ../..
here=`pwd`

# executables ... look for $(PCP_WEBLIB) in a GNUmakefile
#
# QA src apps ... look for -lpcp_web in qa/src/GNUlocaldefs
#
sed -e '/^#/d' <<End-of-File | while read dir prep target
# dir			prep	target
#			<- make $prep first unless $prep = "-"
# libs
src/libpcp_web/src	-	default
# executables
src/pmfind		-	pmfind
src/pmjson		-	pmjson
src/pmproxy/src		clean	pmproxy
src/pmsearch		-	pmsearch
src/pmseries		-	pmseries
# PMDAs
src/pmdas/apache	-	default
src/pmdas/bpf		clean	default
src/pmdas/docker	-	default
src/pmdas/podman	-	default
src/pmdas/root		-	default
src/pmdas/statsd/src	clean	default
# QA apps
qa/src			-	httpfetch
qa/src			-	json_test
qa/src			-	sha1int2ext
qa/src			-	test_encodings
End-of-File
do
    if cd $dir
    then
	if [ X"$prep" != X"-" ]
	then
	    if make "$prep" >$tmp.out 2>&1
	    then
		:
	    else
		cat $tmp.out
		echo "$dir: [prep] make $prep failed!"
		exit
	    fi
	fi
	case "$target"
	in
	    clean|default|install)
		    # nothing to do
		    ;;
	    *)
		    if rm -f $target.o $target
		    then
			:
		    else
			echo "$dir: rm -f $target.o $target failed!"
			exit
		    fi
		    ;;
	esac
	if make $target >$tmp.out 2>&1
	then
	    case "$dir"
	    in
		src/*)	# need to install these ones
			    if sudo make install >$tmp.out 2>&1
			    then
				#debug# cat $tmp.out
				if grep -q "Nothing to be done for 'install'" $tmp.out
				then
				    echo "$dir: nothing to install"
				else
				    echo "$dir: $target remade and installed"
				fi
			    else
				cat $tmp.out
				echo "$dir: sudo make install failed!"
				exit
			    fi
			    ;;
		qa/*)	# nothing to install here
			    echo "$dir: $target remade"
			    ;;
		*)		# botch
			    echo "Botch: install rules for dir $dir"
			    exit
			    ;;
	    esac
	else
	    cat $tmp.out
	    echo "$dir: make $target failed!"
	    exit
	fi
    else
	echo "$dir: cd $dir failed from `pwd`"
	exit
    fi
    cd $here
done
