#!/bin/bash
#
# Configure script, for most users only needed for backwards compatibility with
# pqiv 1.0 (pre-gtk3). I still recommend to use it in automated builds to
# ensure continued compatibility.
#

tempdir() {
	NAME=tmp_${RANDOM}
	while [ -d $NAME ]; do
		NAME=tmp_${RANDOM}
	done
	mkdir ${NAME}
	echo ${NAME}
}

OLD_OPTIONS="no-sorting no-compositing no-fading no-commands no-config-file no-inotify no-animations binary-name"

PREFIX=/usr
DESTDIR=
GTK_VERSION=0
CROSS=
BINARY_EXTENSION=

# Help and options
help() {
	cat >&2 <<EOF
pqiv configuration

options:
  --prefix=..           Set the prefix for installed files
  --destdir=..          Set the destdir for installed files (for package maintainers)
  --gtk-version=2       Use GTK+-2.0 even if GTK+-3.0 is available
  --cross=..            Set a cross-compiler prefix (e.g. \`i686-pc-mingw32-')

EOF
}

maintainer_update_info() {
	if [ -n "$_MAINTAINER_UPDATE_INFO_SEEN" ]; then
		return
	fi
	export _MAINTAINER_UPDATE_INFO_SEEN=1

	if [ -n "$BASH" ]; then
		echo -e "\033[31;1m"
	else
		echo "\033[31;1m"
	fi

	cat >&2 <<EOF
pqiv configuration notice

pqiv 1.0 is a rewrite from scratch using GTK+-3.0 and Cairo. The more
throughout use of glib instead of Unix APIs should enhance compatibility
considerably.

Therefore, the feature options are gone. The parameter 
  \`$1'
you entered won't have any effect except for you seeing this message.

The new release has been tested in a lot of Linuxes and mingw. I expect
some bugs to appear nonetheless. Please report them to
  https://github.com/phillipberndt/pqiv

Thanks!
EOF
	if [ -n "$BASH" ]; then
		echo -e "\033[0m"
	else
		echo "\033[0m"
	fi
}

while [ $# -gt 0 ]; do
	PARAMETER=${1%=*}
	VALUE=${1#*=}

	case $PARAMETER in
		--prefix)
			PREFIX=$VALUE
			;;
		--destdir)
			DESTDIR=$VALUE
			;;
		--gtk-version)
			GTK_VERSION=$VALUE
			;;
		-h)
			help
			;;
		--help)
			help
			;;
		--cross)
			CROSS=$VALUE
			;;
		*)
			for OLD_OPTION in $OLD_OPTIONS; do
				if [ "$PARAMETER" = "--${OLD_OPTION}" ]; then
					maintainer_update_info $PARAMETER
					OPTION_DONE=$PARAMETER
					continue
				fi
			done
			if [ "-$OPTION_DONE" = "-$PARAMETER" ]; then
				shift
				continue
			fi
			echo "Unknown option: $1" >&2
			help
			exit 1
	esac
	shift
done

# If cross-compiling, check if cc is present (usually it's not)
if [ -n "$CROSS" -a -z "$CC" ]; then
	echo -n "Checking for cross-compiler cc.. "
	if ! which ${CROSS}cc >/dev/null 2>&1; then
		if which ${CROSS}clang >/dev/null 2>&1; then
			export CC=clang
		elif which ${CROSS}gcc >/dev/null 2>&1; then
			export CC=gcc
		else
			echo
			echo
			echo "No compiler found. Please set the appropriate CC environment variable." >&2
			exit 1
		fi
		echo "using ${CROSS}${CC}"
	else
		echo "ok"
	fi
fi

# Determine binary extension (for Windows)
echo -n "Determining executable extension.. "
DIR=`tempdir`
cd $DIR
echo 'int main(int argc, char *argv[]) { return 0; }' > test.c
${CROSS}${CC:-cc} test.c
RV=$?
rm -f test.c
EXECUTABLE=`ls`
rm -f $EXECUTABLE
cd ..
rmdir $DIR
if [ "$RV" != 0 ]; then
	echo "The compiler can't compile executables!?" >&2
	echo
	exit 1
fi
EXECUTABLE_EXTENSION=${EXECUTABLE#a}
if [ "$EXECUTABLE_EXTENSION" = ".out" ]; then
	EXECUTABLE_EXTENSION=
fi
echo ${EXECUTABLE_EXTENSION:-(none)}

# Do a rudimental prerequisites check to have user-friendlier error messages
while read -r LINE; do
	for VAR in LIBS_GTK3 LIBS_GTK2; do
		if [ "${LINE#$VAR=}" != "${LINE}" ]; then
			eval ${VAR}=\"${LINE#$VAR=}\"
		fi
	done
done < Makefile

if ! which ${CROSS}pkg-config >/dev/null 2>&1; then
	echo "Did not find required tool ${CROSS}pkg-config!" >&2
	exit 1
fi

echo -n "Checking if GTK, cairo and glib are installed.. "
if ${CROSS}pkg-config --exists $LIBS_GTK3; then
	echo "ok"
else
	if ${CROSS}pkg-config --exists $LIBS_GTK2; then
		if [ "$GTK_VERSION" = 3 ]; then
			echo "failed."
			echo
			echo "GTK 2 was found, but you manually specified --gtk-version=3, which was not found." >&2
			echo "If you want GTK3, install the development packages for" >&2
			echo " ${LIBS_GTK3}" >&2
			exit 1
		fi
		echo "ok, found GTK 2"
		GTK_VERSION=2
	else
		echo "failed."
		echo
		echo "Please install either the development packages for " >&2
		echo " ${LIBS_GTK3}" >&2
		echo "or for" >&2
		echo " ${LIBS_GTK2}" >&2
		exit 1
	fi
fi

echo "Writing config.make."
cat > config.make <<EOF
DESTDIR=$DESTDIR
PREFIX=$PREFIX
GTK_VERSION=$GTK_VERSION
CROSS=$CROSS
EXECUTABLE_EXTENSION=$EXECUTABLE_EXTENSION
EOF
[ -n "${CC}" ] && echo "CC=${CC}" >> config.make
echo
echo "Done. Run make all install to install pqiv."
exit 0
