#! /bin/sh
# $Id: ins_mak,v 12.10 2023/01/16 19:48:48 tom Exp $
#
# Install the td_lib make include-file.  We adjust the symbols that refer
# to the location of the include- and lib-directory.

failed() {
	echo "? $*" >&2
	exit 1
}

test $# = 0 && failed 'No INSTALL program given'
PRG="$1"
shift

test $# = 0 && failed 'No DESTDIR given'
DESTDIR=$1
shift

test $# = 0 && failed 'No DATADIR given'
CFGDIR=$1
my_CFG=$1
test -n "$DESTDIR" && my_CFG=`echo "$CFGDIR" | sed -e "s,$DESTDIR,,"`
shift

test $# = 0 && failed 'No LIBDIR given'
LIBDIR=$1
my_LIB=$1
test -n "$DESTDIR" && my_LIB=`echo "$LIBDIR" | sed -e "s,$DESTDIR,,"`
shift

test $# = 0 && failed 'No INCLUDEDIR given'
INCDIR=$1
my_INC=$1
test -n "$DESTDIR" && my_INC=`echo "$INCDIR" | sed -e "s,$DESTDIR,,"`
shift

test $# = 0 && failed 'No SOURCE given'

SED=${TMPDIR-/tmp}/sed$$
trap 'rm -f "$SED"; exit 1' HUP INT QUIT TERM
trap 'rm -f "$SED"; exit 0' EXIT

cat >"$SED" <<EOF
/^TD_LIB[ 	]*=/s%=.*%= $my_LIB%
/^TD_INC[ 	]*=/s%=.*%= $my_INC%
/^TD_CFG[ 	]*=/s%=.*%= $my_CFG%
EOF

for SRC in "$@"
do
	src=`basename "$SRC"`
	if test -f "$SRC"
	then
		echo "installing $SRC in $CFGDIR"
		TMP=${TMPDIR-/tmp}/$src
		sed -f "$SED" "$SRC" > "$TMP"
		eval "$PRG" "$TMP" "$CFGDIR"
		rm -f "$TMP"
	else
		echo "..skipping $SRC"
	fi
done
