#! /usr/bin/env bash

# Ensure we are in the correct working directory
cd "$(dirname "$(which "$0")")/.." || exit 1

# Determine the version of Filed
version=''
eval "$(( grep '^# *define  *FILED_VERSION' filed.c; echo '|version=FILED_VERSION' ) | cpp -E | grep '^|' | sed 's@^|@@')"
if [ -z "${version}" ]; then
	echo "Unable to determine which version of Filed we are compiling.  Aborting." >&2

	exit 1
fi

# Cleanup
rm -rf workdir-buildPrecompiled-*

# Compile everything, all at once
idx=-1
for tryCompilerDir in "$(readlink -f ~/root/cross-compilers)" "$(readlink -f ~/devel/build-cc/TMP)"; do
	setup_cc="${tryCompilerDir}/setup-cc"

	platforms=(
		$("${setup_cc}" 2>/dev/null | tail -n +2)
	)

	for platformBase in "${platforms[@]}"; do
		for platformAdditional in "" ".seccomp" ".fakechroot" ".internaldirhandle"; do
			case "${platformAdditional}" in
				*.seccomp|*.seccomp.*)
					# seccomp only works on i386/x86_64 for now
					case "${platformBase}" in
						i?86-*|x86_64-*)
							;;
						*)
							continue
							;;
					esac
					;;
			esac

			platform="${platformBase}${platformAdditional}"
			echo "-- ${platform}"

			idx=$[$idx + 1]
			(
				workdir="workdir-buildPrecompiled-${idx}-$(openssl rand 20 -hex)-platform-${platform}" || exit 1
				mkdir "${workdir}" || exit 1
				cd "${workdir}" || exit 1

				make_extra=(
					-f ../Makefile
					srcdir=..
				)

				extra_cflags=""
				while true; do
					case "${platform}" in
						*.seccomp|*.seccomp.*)
							platform="$(echo "${platform}" | sed 's@\.seccomp@@')"
							make_extra+=(FILED_DO_SECCOMP=1)
							;;
						*.fakechroot|*.fakechroot.*)
							platform="$(echo "${platform}" | sed 's@\.fakechroot@@')"
							extra_cflags="${extra_cflags} -DFILED_FAKE_CHROOT=1"
							;;
						*.internaldirhandle|*.internaldirhandle.*)
							platform="$(echo "${platform}" | sed 's@\.internaldirhandle@@')"
							extra_cflags="${extra_cflags} -DFILED_DONT_REDIRECT_DIRECTORIES=1"
							;;
						*)
							break
							;;
					esac
				done

				case "${platform}" in
					*-musl-*|*-musl)
						make_extra+=(FILED_EXTRA_LDFLAGS="-static")
						;;
				esac

				eval $("${setup_cc}" "${platform}")

				make_extra+=("CC=${CC}")
				if [ -n "${extra_cflags}" ]; then
					make_extra+=(FILED_EXTRA_CFLAGS="${extra_cflags}")
				fi

				make "${make_extra[@]}" > filed.log 2>&1 </dev/null || mv filed.log filed-failed.log
			) &
		done
	done
done

# Wait for that to get done
wait

# Rename the files into place
mkdir -p compiled
for binary in workdir-buildPrecompiled-*/filed; do
	platform="$(echo "${binary}" | sed 's@^.*-platform-@@;s@/.*$@@')"
	mv "${binary}" "compiled/filed-${version}-${platform}"
done
for errorLog in workdir-buildPrecompiled-*/filed-failed.log; do
	platform="$(echo "${errorLog}" | sed 's@^.*-platform-@@;s@/.*$@@')"
	mv "${errorLog}" "compiled/filed-${version}-${platform}-error.log"
done

# Cleanup
rm -rf workdir-buildPrecompiled-*

exit 0
