#!/bin/sh -e
# See http://jdebp.eu./FGA/slashpackage.html
if test \! -d package || test \! -d src
then
	echo "You are not in the right directory." 1>&2 
	exit 100
fi
if ! test $# -ge 2
then
	echo "Two arguments are required." 1>&2 
	exit 100
fi

src="${1}"
dest="${2}"

##########################################################
# distributor patchable stuff

# Eventually, these will be /usr .
binprefix="/usr/local"
libprefix="/usr/local"
# Eventually, these will be /usr/share .
manprefix="/usr/local"
docprefix="/usr/local/share"
# Eventually, this will be / .
etcprefix="/usr/local"

##########################################################
# binaries and doco

for i in uschedule
do
	if test -s package/"$i".commands1 ||
	   test -s package/"$i".aliases1
	then
		install -d -m 0755 -- "${dest}"/"${i}"/"${binprefix}"/bin 
		install -d -m 0755 -- "${dest}"/"${i}"/"${manprefix}"/man/man1 
	fi
	while read j ;
	do
		ln -f -- "${src}"/command/"$j" "${dest}"/"${i}"/"${binprefix}"/bin/ 
		ln -f -- "${src}"/doc/"$j".1 "${dest}"/"${i}"/"${manprefix}"/man/man1/ 
	done < package/"$i".commands1 ;

	if test -s package/"$i".commands8 ||
	   test -s package/"$i".aliases8
	then
		install -d -m 0755 -- "${dest}"/"${i}"/"${binprefix}"/sbin 
		install -d -m 0755 -- "${dest}"/"${i}"/"${manprefix}"/man/man8 
	fi
	while read j
	do
		ln -f -- "${src}"/command/"$j" "${dest}"/"${i}"/"${binprefix}"/sbin/ 
		ln -f -- "${src}"/doc/"$j".8 "${dest}"/"${i}"/"${manprefix}"/man/man8/ 
	done < package/"$i".commands8 ;

	while read c a
	do
		ln -f -s "$c" "${dest}"/"${i}"/"${binprefix}"/bin/"$a" 
		ln -f -s "$c".1 "${dest}"/"${i}"/"${manprefix}"/man/man1/"$a".1 
	done < package/"$i".aliases1 ;

	while read c a
	do
		ln -f -s "$c" "${dest}"/"${i}"/"${binprefix}"/sbin/"$a" 
		ln -f -s "$c".8 "${dest}"/"${i}"/"${manprefix}"/man/man8/"$a".8 
	done < package/"$i".aliases8 ;

	if test -s package/"$i".extra-manpages7
	then
		install -d -m 0755 -- "${dest}"/"${i}"/"${manprefix}"/man/man7
	fi
	while read j ;
	do
		ln -f -- "${src}"/doc/"$j".7 "${dest}"/"${i}"/"${manprefix}"/man/man7/ 
	done < package/"$i".extra-manpages7 ;
done
