#!/bin/bash

# pev configure script
# run with ./configure before make step

makef_libpe=lib/libpe/Makefile
makef_pev=src/Makefile
makef=Makefile
prefix=/usr
os=
gccver=
tmpmakef=
tmptestc=
new_cc=
new_strip=
new_libpe=
new_zipfile='pev.zip'

# test code for ISO C99 standard
testc="
int main(void)
{
	for (int i=0, j=0; i<10; i++)
		j = 2*i;

	return 0;
}"

usage()
{
	echo -e "Usage: ./configure OPTIONS\n
 --prefix=PREFIX          change PREFIX to install files (default: $prefix)
 -h, --help               show this help and exit"
	exit 0
}

quit()
{
	echo "$1"
	echo -e "\nError in configure script. You need to satisfy all dependencies and
run this script again. Maybe you can try to run 'make' and analyze the output."
	rm -f $tempfile $tmptestc
	exit 1
}

# replace(regex, file)
replace()
{
	tempfile=$(mktemp /tmp/pev.XXXX)

	(sed "$1" "$2" > $tempfile 2> /dev/null && \
	[ -s $tempfile ] && \
	mv $tempfile "$2") || quit fail
	rm -f $tempfile
}

check()
{
	for i in $*; do
		echo -n "Checking for $i... "
		which $i 2>&1 > /dev/null && echo yes || quit no
	done
}

# parse args
[ $# -gt 1 ] && usage
for arg in "$@"; do
	[ "$1" == "--help" -o "$1" == "-h" ] && usage
	# get prefix path, if passed
	[ ${arg:0:9} == "--prefix=" ] || usage
	[ -n ${arg:9} ] && prefix=${arg:9}
done

# check for needed programs
check which
check sed make ld test mkdir rmdir install strip rm mv mktemp uname cut mktemp

# get os type
os=$(uname -a | cut -d' ' -f1)
echo -n "Getting OS type... "
[ -n $os ] && echo "$os" || (echo "unable to retrieve OS type" && exit 1)

# replace prefix in Makefiles
echo -n "Configuring prefix to "$prefix"... "
prefix=$(echo $prefix | sed 's/\//\\\//g') # escape slashes
replace "s/^PREFIX=.*/PREFIX=$prefix/" "$makef_libpe"
replace "s/^PREFIX=.*/PREFIX=$prefix/" "$makef_pev"
echo "ok"

# apply specific OS patches in Makefiles
case "$os" in
	Darwin)
		new_cc='gcc'
		new_strip='strip -x'
		new_libpe='-shared -Wl,-install_name,$(LIBNAME).so.1 -o $(LIBNAME).so $(LIBNAME).o'
		;;
	CYGWIN*WOW64)
		new_cc='x86_64-w64-mingw32-gcc.exe'
		new_strip='strip --strip-unneeded'
		new_libpe='-shared -o $(LIBNAME).dll $(LIBNAME).o'
		new_zipfile='pev-$(VERSION)_x86-64.zip'
		;;
	CYGWIN*)
		new_cc='i686-pc-mingw32-gcc.exe'
		new_strip='strip --strip-unneeded'
		new_libpe='-shared -o $(LIBNAME).dll $(LIBNAME).o'
		new_zipfile='pev-$(VERSION)_x86.zip'
		;;
	*) # Default: Linux
		new_cc='gcc'
		new_strip='strip --strip-unneeded'
		new_libpe='-shared -Wl,-soname,$(LIBNAME).so.1 -o $(LIBNAME).so $(LIBNAME).o'
		;;
esac

echo -n "Patching Makefiles for "$os"... "

# libpe patches
replace "s/^CC=.*/CC=$new_cc/" "$makef_libpe"
replace "s/STRIP=.*/STRIP=$new_strip/" "$makef_libpe"
replace "s/-shared.*\.o$/$new_libpe/" "$makef_libpe"

# pev patches
replace "s/^CC=.*/CC=$new_cc/" "$makef_pev"

# top level Makefile patches
replace "s/^ZIPFILE=.*/ZIPFILE=$new_zipfile/" "$makef"

echo ok

check $new_cc

# get gcc version
gccver=$($new_cc -v 2>&1 | sed -n '$s/.*\([0-9]\.[0-9]\.[0-9]\).*/\1/p')
echo "Checking for gcc version... $gccver"

# compile test.c in a temp directory to check c99 support
echo -n "Checking for ISO C99 support in gcc... "
tmptestc=$(mktemp /tmp/pev.XXXX)
echo "$testc" > $tmptestc.c || quit fail
$new_cc -std=c99 -o $tmptestc $tmptestc.c 2> /dev/null && echo yes || quit no
rm -f $tmptestc $tmptestc.c
