#!/bin/sh

test_libc_features() {
	CFLAGS="-D_GNU_SOURCE"
	: ${CC:=cc}
	2>/dev/null $CC -xc $CFLAGS -o /dev/null - <<-EOF
	#include <string.h>
	int main(void) {
		char dst[4];
		strlcpy(dst, "1234", sizeof dst);
		return 0;
	}
	EOF
}

copy_mk() {
	cmd="cp Makefile.$1 Makefile"
	echo "+ $cmd"; $cmd
}

usage() {
	cat <<-HELP
	Usage: configure [-h]
	Example: build a static binary and install to your home directory

	./configure
	CFLAGS="-static" make
	PREFIX=\$HOME/local make install
	HELP
	exit 1
}

while [ $# -gt 0 ]; do
	case $1 in
		-h) usage ;;
		 *) echo "configure: unused argument: $1" ;;
	esac
	shift
done

case "${TARGET_OS:-`uname`}" in
	Darwin)
		copy_mk macos
		;;
	Linux)
		test_libc_features && copy_mk linux || copy_mk linux-compat
		;;
	*)
		copy_mk bsd
		;;
esac
