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

./package/prepare

# Rebuild.
if ! > /dev/null expr -- "${MAKEFLAGS}" : "--jobs" &&
   ! > /dev/null expr -- "${REDOFLAGS}" : "--jobs" &&
   test -z "${cpus}"
then
	case "`uname`" in
	(Linux) cpus="`fgrep -c processor /proc/cpuinfo`" ;;
	(*BSD) cpus="`sysctl -n hw.ncpu`" ;;
	esac
fi
redo --directory build ${cpus:+--jobs "${cpus}"} -- all

# Atomically update the release directories ./command, ./library, ./include, and so forth.
# The build and release directories need not be on the same disc volume.
# And the files released must not be potentially overwritable and truncatable by the compiler/linker during subsequent builds.
# But released files can be links to other released files, of course.
install -d -m 0755 command manual guide guide/commands examples
 
# ##############################################################################
# Build all of the ...{new} files in ./command, ./config, and ./manual.
# Main guide pages
cat package/guidepages |
while read -r i
do
	rm -f -- guide/"$i".html{new}
	install -m 0444 -p -- build/doco/"$i".html guide/"$i".html{new}
done
cat package/examples |
while read -r i
do
	rm -f -- examples/"$i".bas{new}
	install -m 0444 -p -- build/examples/"$i".bas examples/"$i".bas{new}
done
for i in guide.css
do
	rm -f -- guide/"$i"{new}
	install -m 0444 -p -- build/doco/"$i" guide/"$i"{new}
done
for i in docbook-xml.css
do
	rm -f -- guide/"$i"{new}
	install -m 0444 -p -- build/doco/"$i" guide/commands/"$i"{new}
done
# All of the commands that have their own manual and guide pages
for section in 1 5
do
	install -d -m 0755 manual/man${section} manual/docbook${section}
	! test -r package/commands${section} || cat package/commands${section} |
	while read command
	do
		rm -f -- command/"${command}"{new} manual/man${section}/"${command}".${section}{new} manual/docbook${section}/"${command}".xml{new} guide/commands/"${command}".xml{new}
		cp -p -f -- build/command/"${command}" command/"${command}"{new}
		cp -p -f -- build/manual/"${command}".${section} manual/man${section}/"${command}".${section}{new}
		cp -p -f -- build/"${command}".xml manual/docbook${section}/"${command}".xml{new}
		ln -f -- manual/docbook${section}/"${command}".xml{new} guide/commands/"${command}".xml{new}
		strip command/"${command}"{new}
	done
	! test -r package/aliases${section} || cat package/aliases${section} |
	while read command alias
	do
		ln -f -- command/"${command}"{new} command/"${alias}"{new}
		ln -f -- manual/man${section}/"${command}".${section}{new} manual/man${section}/"${alias}".${section}{new}
		ln -f -- manual/docbook${section}/"${command}".xml{new} manual/docbook${section}/"${alias}".xml{new}
		ln -f -- manual/docbook${section}/"${command}".xml{new} guide/commands/"${alias}".xml{new}
	done
	! test -r package/extra-manpages${section} || cat package/extra-manpages${section} |
	while read command
	do
		rm -f -- manual/man${section}/"${command}".${section}{new} manual/docbook${section}/"${command}".xml{new} guide/commands/"${command}".xml{new}
		cp -p -f -- build/manual/"${command}".${section} manual/man${section}/"${command}".${section}{new}
		cp -p -f -- build/"${command}".xml manual/docbook${section}/"${command}".xml{new}
		ln -f -- manual/docbook${section}/"${command}".xml{new} guide/commands/"${command}".xml{new}
	done
done
 
# ##############################################################################
# Rename the ...{new} files in ./command, ./config, and ./manual.
# Main guide pages
cat package/guidepages |
while read -r i
do
	mv -f -- guide/"$i".html{new} guide/"$i".html
done
cat package/examples |
while read -r i
do
	mv -f -- examples/"$i".bas{new} examples/"$i".bas
done
for i in guide.css commands/docbook-xml.css
do
	mv -f -- guide/"$i"{new} guide/"$i"
done
# All of the commands that have their own manual and guide pages
for section in 1 5
do
	! test -r package/commands${section} || cat package/commands${section} |
	while read command
	do
		mv -f -- command/"${command}"{new} command/"${command}"
		mv -f -- manual/man${section}/"${command}".${section}{new} manual/man${section}/"${command}".${section}
		mv -f -- manual/docbook${section}/"${command}".xml{new} manual/docbook${section}/"${command}".xml
		mv -f -- guide/commands/"${command}".xml{new} guide/commands/"${command}".xml
	done
	! test -r package/aliases${section} || cat package/aliases${section} |
	while read command alias
	do
		mv -f -- command/"${alias}"{new} command/"${alias}"
		mv -f -- manual/man${section}/"${alias}".${section}{new} manual/man${section}/"${alias}".${section}
		mv -f -- manual/docbook${section}/"${alias}".xml{new} manual/docbook${section}/"${alias}".xml
		mv -f -- guide/commands/"${alias}".xml{new} guide/commands/"${alias}".xml
	done
	! test -r package/extra-manpages${section} || cat package/extra-manpages${section} |
	while read command
	do
		mv -f -- manual/man${section}/"${command}".${section}{new} manual/man${section}/"${command}".${section}
		mv -f -- manual/docbook${section}/"${command}".xml{new} manual/docbook${section}/"${command}".xml
		mv -f -- guide/commands/"${command}".xml{new} guide/commands/"${command}".xml
	done
done
