#!/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

CDPATH=

if command -v >/dev/null clang++
then
	extra_flags=''
	major_version="`clang++ --version|sed -ne 's/^.*version *\([[:digit:]]*\)\..*$/\1/p'`"
	if test "${major_version}" -gt 3
	then
		extra_flags="${extra_flags}"' -Wno-suggest-destructor-override -Wno-suggest-override -Wno-disabled-macro-expansion -Wno-global-constructors -Wno-exit-time-destructors'
	fi
	if test "${major_version}" -gt 14
	then
		extra_flags="${extra_flags}"' -Wno-unsafe-buffer-usage'
	fi
	cxx=clang++
	cppflags=''
	cxxflags='-g -O3 -std=gnu++11 -Weverything -integrated-as -Wno-weak-vtables -Wno-padded -Wno-missing-prototypes -Wno-c++98-compat'"${extra_flags}"
	ldflags='-g'
elif command -v >/dev/null g++
then
	cxx=g++
	cppflags=''
	cxxflags=' -g -O3 -std=gnu++11 -Wall -Wextra -Wshadow -Wcast-qual -Wsynth -Woverloaded-virtual -Wcast-align'
	ldflags='-g'
elif command -v >/dev/null owcc
then
	cxx=owcc
	cppflags=''
	cxxflags=' -g -Wall -Wextra -Wc,-xs -Wc,-xr'
	ldflags='-g'
else
	echo "Cannot find clang++, g++, or owcc." 1>&2
	exit 100
fi

# Create and populate the build directory.
install -d -m 0755 build
(
	cd source && find . -type d
) | (
	cd build
	while read i
	do
		mkdir -p "$i"
	done
)
(
	cd source && find . -type f
) | (
	cd build	# The syntax of ln -s makes more sense if we are in the target directory.
	while read i
	do
		dir="`dirname $i`"
		# Try to create a hard link, and fall back to a symbolic link.
		# The source and build directories aren't necessarily on the same disc volume.
		ln -f ../source/"$i" "${dir}" 2>/dev/null || ln -s -f ../source/"$i" "${dir}"
	done
)

# Rebuild.
cwd="`pwd -P`"
base_plus_version="`basename "${cwd}"`"
base="`echo "${base_plus_version}" | sed -e 's/-[[:digit:]][[:alnum:].]*$//'`"
pod2man="`command -v pod2man`"
if test _"${base}" != _"${base_plus_version}"
then
	version="`echo "${base_plus_version}" | sed -e 's/^.*-\([[:digit:]][[:alnum:].]*\)$/\1/'`"
	printf '#define VERSION "%s"\n' "${version}" > build/version.h
else
	printf '#define VERSION "%s"\n' "unknown" > build/version.h
fi
sources="redo.cpp popt-bool-string.cpp popt-bool.cpp popt-compound-2arg.cpp popt-compound.cpp popt-integral.cpp popt-named.cpp popt-signed.cpp popt-simple.cpp popt-string-list.cpp popt-string-pair-list.cpp popt-string-pair.cpp popt-string.cpp popt-table.cpp popt-top-table.cpp popt-unsigned.cpp popt.cpp jobserver.cpp FileDescriptorOwner.cpp wait.cpp lockfile.cpp"
(cd build && "${cxx}" ${cppflags} ${cxxflags} ${ldflags} -o redo $sources)
manuals="redo redo-ifcreate redo-ifchange cubehash"
if test -n "${pod2man}"
then
	for i in ${manuals}
	do
		(cd build && pod2man --center "redo package" --release "v1.0" "$i".pod > "$i".1)
	done
fi

# 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
for section in 1 2 3 4 5 6 7 8 9
do
	rm -f -- manual/man${section}
	ln -f -s -- . manual/man${section}
done
for locale in en_GB.UTF-8 en.UTF-8
do
	rm -f -- manual/"${locale}"
	ln -f -s -- . manual/"${locale}"
done
cat package/commands1 |
while read -r c
do
	rm -f -- command/"$c"{new}
	cp -p -f -- build/"$c" command/"$c"{new}
	strip command/"$c"{new}
	if test -n "${pod2man}"
	then
		rm -f -- manual/"$c".1{new}
		cp -p -f -- build/"$c".1 manual/"$c".1{new}
	fi
done
cat package/aliases1 |
while read -r c a
do
	rm -f -- command/"$a"{new}
	ln -f -- command/"$c"{new} command/"$a"{new}
	if test -n "${pod2man}"
	then
		rm -f -- manual/"$a".1{new}
		cp -p -f -- build/"$a".1 manual/"$a".1{new}
	fi
done
cat package/commands1 |
while read -r c
do
	mv -f -- command/"$c"{new} command/"$c"
	if test -n "${pod2man}"
	then
		mv -f -- manual/"$c".1{new} manual/"$c".1
	fi
done
cat package/aliases1 |
while read -r c a
do
	mv -f -- command/"$a"{new} command/"$a"
	if test -n "${pod2man}"
	then
		mv -f -- manual/"$a".1{new} manual/"$a".1
	fi
done
