#!/bin/bash -e

export insttype="${1:-install}"
stampfile=".ucm-install-all-$insttype-stamp"

case "$insttype" in
uninst)
	rm -f .ucm-install-all-*-stamp
	;;
*)
	if ! changed_since . "$stampfile"; then
		exit 0
	fi
	;;
esac

install_dir() {
	local from=$1
	local to=$2
	local D
	if [ -e $from ]; then
		if [ "$insttype" != uninst ]; then
			mkdir -p $to
		fi
		pushd $from >/dev/null
		for D in `lsd`; do
			install_dir $D $to/$D
		done
		ucm-install `lsx` `lsl` $to
		ucm-install -m644 `lsnox` $to
		popd >/dev/null
	fi
}

for dir in bin bin.*; do
	install_dir $dir $bindir
done
install_dir sbin $sbindir
install_dir perl $perldir
install_dir sh $bindir
install_dir py $bindir   # not ideal!!
install_dir cgi-bin $cgibindir
install_dir html $bindir  # not ideal!!
install_dir prompts $bindir  # not ideal!!
install_dir templates $bindir  # not ideal!!

if [ "$insttype" != "uninst" ]; then
	for A in `brace_files` ; do
		INST=`readlink -f "$prefix/$A"`
		INSTX="`dirname $INST`/.`basename $INST`"
		SRCX="`dirname $A`/.`basename $A`"
		if [ "$insttype" = "devinst" ]; then
			X="$SRCX"
		else
			A="$INST"
			X="$INSTX"
		fi
		if [ "$A" -nt "$X" ]; then
			echo >&2 building ${A#./} ...
#			br "$A" "$X"
			cz -c "$A"
		fi
	done
	for A in `find -name '*.m4'`; do
		B="${A%.m4}"
		if [ "$A" -nt "$B" ]; then
			echo >&2 building ${B#./} ...
			m4 "$A" > "$B"
		fi
	done
fi

touch "$stampfile"
