#!/bin/sh
#
# Configure script for 'uncsv'
# Copyright (c) 2013 Bertrand Janin <b@janin.com>
#

UNCSV_VERSION="0.9.1"


# Some shells have shitty echos
alias echo=/bin/echo


# Some people run configure from their backyard
cd "`dirname $0`"


# Sad default, you can try CC=clang ./configure too
if [ -z "$CC" ]; then
	export CC=gcc
fi


# generate_makefile() - Generate the main Makefile
# $1 file to add at the end
generate_makefile() {
	echo "# Automagically generated by 'configure'"
	echo

	echo "CFLAGS+=-Wall -W -Wpointer-arith -Wbad-function-cast -Wcast-qual"
	echo "CFLAGS+=-Wstrict-prototypes -Wmissing-prototypes"
	echo "CFLAGS+=-Wmissing-declarations -Wnested-externs -Winline"
	echo "CFLAGS+=-DUNCSV_VERSION=\\\"$UNCSV_VERSION\\\" $X_CFLAGS"

	if [ "$DEBUG_MODE" = "Y" ]; then
		echo "CFLAGS+=-ggdb -O0"
	else
		echo "CFLAGS+=-O2"
	fi

	if [ -n "$X_OBJECTS" ]; then
		echo "EXTRA_OBJECTS=$X_OBJECTS"
	fi

	echo "CC?=$CC"
	echo "PREFIX?=$PREFIX"
	echo "MANDEST=$MANDEST"

	cat $1
}


# usage() - Spit out basic help
usage() {
	echo "usage: ./configure [-h] [-d] [platform]"
	echo
	echo "    -h   this help screen."
	echo "    -d   configure for debug mode (-ggdb -O0)"
	echo
	echo "By default, this script will attempt to detect your OS and will guess the most"
	echo "appropriate configuration. If it gets your system wrong, you can force it,"
	echo "here is the list of ''supported'' platforms:"
	echo
	echo "    bsd     - OpenBSD, FreeBSD, OS X, Darwin, ..."
	echo "    linux   - Linux"
	exit
}


# stuff_not_found() - Exit 'cause we're missing something
stuff_not_found() {
	message="$1"
	echo "not found"
	echo
	echo ERROR: $message
	exit 2
}


# Command-line arguments
while true; do
	case "$1" in
		-h|--help)
			usage
			exit 2
			;;

		-d|--debug)
			echo "Debug mode... enabled"
			DEBUG_MODE="Y"
			shift
			;;

		--prefix=*)
			PREFIX="${1##--prefix=}"
			shift
			;;

		# Skip all random long opts passed by packagers thinking this
		# is autoconf-compatible.
		--*)
			shift
			;;

		*)
			break
			;;
	esac
done


# Detect platform
echo -n "Target platform... "
OS=`uname`
if [ -n "$1" ]; then
	OS="$1"
fi
echo $OS


# Platform-specific configuration
case $OS in
	Linux|Unix|POSIX)
		X_CFLAGS="-D_GNU_SOURCE -D_LOCAL_TZ_FILE"
		X_OBJECTS=""
		MANDEST="share/man"
		;;

	*BSD)
		;;

	Darwin)
		MANDEST="share/man"
		;;

	*)
		echo
		echo "Sorry but '$OS' is currently unsupported, you can attemp 'bsd' or 'linux' as"
		echo "target and cross your fingers. In order to do that you just need to give either"
		echo "one as parameter to ./configure. You can get the official list of supported "
		echo "platform with ./configure -l"
		exit
esac


# Default paths
[ -z "$PREFIX" ]	&& PREFIX="/usr/local"
[ -z "$MANDEST" ]	&& MANDEST="man"


# Check for make
echo -n "make... "
cat <<EOF > fake_makefile
all:
	@echo found
EOF
if ! make -f fake_makefile 1>/dev/null 2>/dev/null; then
	stuff_not_found "Make not found (try to install make or gmake)"
fi
echo found
rm -f fake_makefile*


# Check if we have a working cc
echo -n "cc... "
cat <<EOF > fake_cc.c
#include <stdio.h>
main() { printf("woot\n"); }
EOF
if ! ${CC} fake_cc.c -o /dev/null 1>/dev/null 2>/dev/null; then
	stuff_not_found "No C compiler found (try to install gcc or clang)"
fi
echo ${CC}
rm -f fake_cc*



generate_makefile Makefile.src > Makefile
generate_makefile src/Makefile.src > src/Makefile

echo
echo "Configured for '$OS', run 'make' (or 'gmake') to compile."

