#!/usr/bin/env sh

set -e

BOLD="\033[1m"
RESET="\033[0m"
LIGHT_RED="\033[91m"
LIGHT_GREEN="\033[92m"
LIGHT_CYAN="\033[96m"

logging(){
	local type=$1; shift
	printf "${LIGHT_CYAN}${BOLD}configure${RESET} [%b] : %b\n" "$type" "$*"
}
log_info(){
	logging "${LIGHT_GREEN}info${RESET}" "$@"
}
log_error(){
	logging "${LIGHT_RED}error${RESET}" "$@" >&2
}

# find and print x11 header path
get_xlib_include_path(){
	local result=""

	for inc in \
		/usr/X11/include          \
		/usr/X11R6/include        \
		/usr/X11R5/include        \
		/usr/X11R4/include        \
								\
		/usr/include              \
		/usr/include/X11          \
		/usr/include/X11R6        \
		/usr/include/X11R5        \
		/usr/include/X11R4        \
								\
		/usr/local/X11/include    \
		/usr/local/X11R6/include  \
		/usr/local/X11R5/include  \
		/usr/local/X11R4/include  \
								\
		/usr/local/include/X11    \
		/usr/local/include/X11R6  \
		/usr/local/include/X11R5  \
		/usr/local/include/X11R4  \
								\
		/usr/X386/include         \
		/usr/x386/include         \
		/usr/XFree86/include/X11  \
								\
		/usr/local/include        \
		/usr/athena/include       \
		/usr/local/x11r5/include  \
		/usr/lpp/Xamples/include  \
								\
		/usr/openwin/include      \
		/usr/openwin/share/include
	do
		if [ -f "$inc/X11/Xlib.h" -a -f "$inc/X11/extensions/XShm.h" ]; then
			result=$inc
			break
		fi
	done
	echo $result
}

show_help(){
cat <<EOF
Usage :
$0              Auto-configure and make MinilibX
$0 clean        Execute the clean rule of both Makefile.gen
EOF
}

clean(){
	log_info 'Execute "make clean" from "makefile.gen"'
	${MAKE} -f Makefile.gen clean
	log_info 'Execute "make clean" from "test/makefile.gen"'
	${MAKE} -f Makefile.gen -C test/ --no-print-directory clean
}

parse_args(){
	case "$1" in
		--help | -h)
			show_help
			exit 0;;
		clean)
			clean
			exit 0;;
		"") return;;
		*)
			log_error "unknown command \"$1\"\nRun \"./configure --help\" for usage."
			exit 1;;
	esac
}

main(){
	local xlib_inc="$(get_xlib_include_path)"

	case $(uname) in
		FreeBSD) MAKE=gmake ;;
		*) MAKE=make ;;
	esac

	parse_args "$@"
	if [ -z "$xlib_inc" ]; then
		log_error "Can't find a suitable X11 include directory."
		exit 1
	fi
	log_info "Found X11 include path directory: $xlib_inc"

	log_info 'Generate "makefile.gen" from template "makefile.mk"'
		echo "INC=$xlib_inc"  	 	    	>  Makefile.gen 
		cat Makefile.mk | grep -v %%%%  	>> Makefile.gen
	log_info 'Generate "test/makefile.gen" from template "test/makefile.mk"'
		echo "INC=$xlib_inc"				>  test/Makefile.gen
		cat test/Makefile.mk | grep -v %%%% >> test/Makefile.gen

	log_info 'Execute "make all" from file "makefile.gen"'
		${MAKE} -f Makefile.gen all
	log_info 'Execute "make all" from file "test/makefile.gen"'
		(cd test ; ${MAKE} -f Makefile.gen all )
}

main "$@"