#!/bin/sh
#
# Developer overlay install for libpcp<foo>
#
# Usage: install-dev [-d] [-n]
#

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

debug=false
RM=rm
MV=mv
while [ $# -gt 0 ]
do
    if [ "X$1" = X-d ]
    then
	debug=true
	shift
    elif [ "X$1" = X-n ]
    then
	# only show-me for multiarch games
	#
	RM='echo + rm'
	MV='echo + mv'
	shift
    else
	echo >&2 "Bad arg: $1"
	echo >&2 "Usage: install-dev [-d] [-n]"
	exit
    fi
done

# need to get the pcp.conf used in the build, not the
# installed one
#
if [ ! -f ../../../src/include/pcp.conf ]
then
    echo "Arrgh: ../../../src/include/pcp.conf missing"
    exit
fi
PCP_CONF=../../../src/include/pcp.conf

if [ ! -f ../../../src/include/pcp.env ]
then
    echo "Arrgh: ../../../src/include/pcp.env missing"
    exit
fi

. ../../../src/include/pcp.env

iam="`pwd | sed -e 's;/src$;;' -e's;.*/\(.*\);\1;'`"
case "$iam"
in
    libpcp_fault)	pkg_lib=libpcp
    			;;
    *)			pkg_lib="$iam"
    			;;
esac

if ! make
then
    echo "Arrgh: make failed"
    exit
fi

case "`echo $iam.so.*`"
in
    *\ *)
	echo "Arrgh: more than one .so.*: `echo $iam.so.*`"
	exit
	;;
esac
dso_ver=`echo "$iam".so.* | sed -e 's/.*\.so\.//'`

make -n install \
| grep -v pkgconfig >$tmp.cmd
# even for -n need to run sudo make install subcommands so that
# later logic works
#
if ! sudo sh -e <$tmp.cmd
then
    cat $tmp.cmd
    echo "Arrgh: sudo make install failed"
fi

$debug && echo "Debug: iam=$iam dso_ver=$dso_ver pkg_lib=$pkg_lib"
$debug && echo "Debug: PCP build libdir: $PCP_LIB_DIR"

pkg_dir="`pkg-config --variable=libdir $pkg_lib 2>/dev/null`"
$debug && echo "Debug: pkg-config libdir: $pkg_dir"
if [ -z "$pkg_dir" ]
then
    echo "Arrgh: no libdir from pkg-config"
    exit
fi
if [ ! -d "$pkg_dir" ]
then
    echo "Arrgh: Missing: $pkg_dir: libdir from pkg-config"
    exit
fi

# multiarch games ...
#
if [ "$pkg_dir" != "$PCP_LIB_DIR" ]
then
    $debug && echo "Debug: moving $iam files from $PCP_LIB_DIR to $pkg_dir"
    for file in ${iam}.so ${iam}.so.$dso_ver
    do
	if [ -f "$PCP_LIB_DIR/$file" ]
	then
	    if [ -f "$pkg_dir/$file" ]
	    then
		# both exist
		if cmp "$PCP_LIB_DIR/$file" "$pkg_dir/$file" >/dev/null
		then
		    echo "Warning: removing $PCP_LIB_DIR/$file (already the same as $pkg_dir/$file)"
		    if ! sudo $RM -f "$PCP_LIB_DIR/$file"
		    then
			echo "Warning: rm $PCP_LIB_DIR/$file failed"
		    fi
		else
		    if ! sudo $MV "$PCP_LIB_DIR/$file" "$pkg_dir"
		    then
			ls -l $PCP_LIB_DIR/$file
			echo "Arrgh: mv $PCP_LIB_DIR/$file $pkg_dir failed"
			exit
		    else
			echo "Info: updated $pkg_dir/$file with $PCP_LIB_DIR/$file"
		    fi
		fi
	    else
		echo "Warning: $PCP_LIB_DIR/$file present, but no $pkg_dir/$file"
		if ! sudo $MV "$PCP_LIB_DIR/$file" "$pkg_dir"
		then
		    ls -l $PCP_LIB_DIR/$file
		    echo "Arrgh: mv $PCP_LIB_DIR/$file $pkg_dir failed"
		    exit
		else
		    echo "Info: moved $PCP_LIB_DIR/$file to $pkg_dir"
		fi
	    fi
	else
	    echo "Warning: $PCP_LIB_DIR/$file: not found"
	fi
    done
else
    $debug && echo "Debug: nothing else to do"
fi

sts=0
exit

