#!/bin/sh

prefix=/usr/local
opt=true
dbg=true
plugins=$(cd plugins && find . -mindepth 1 -maxdepth 1 -type d | sed 's/\.\///' | xargs echo -n)

disable_plugin()
{
	plugins=$(echo $plugins | sed "s/$1//;s/  / /")
}

for arg; do
	case $arg in
	--prefix=*)
		prefix=`echo $arg | sed 's/--prefix=//'`
		;;
	--enable-opt)
		opt=true
		;;
	--disable-opt)
		opt=false
		;;
	--enable-dbg)
		dbg=true
		;;
	--disable-dbg)
		dbg=false
		;;
	--disable-plugin-*)
		disable_plugin `echo $arg | sed 's/--disable-plugin-//'`
		;;

	--help)
		echo 'Usage: ./configure [options]'
		echo 'Options:'
		echo '  --prefix=<prefix>       installation prefix (default: /usr/local)'
		echo '  --enable-opt            enable code optimizations (default)'
		echo '  --disable-opt           disable code optimizations'
		echo '  --enable-dbg            enable debugging (default)'
		echo '  --disable-dbg           disable debugging'
		echo '  --disable-plugin-<name> remove wallpaper plugin from build'
		echo '  --help                  print usage and exit'
		echo
		echo 'You may set CFLAGS and/or LDFLAGS when running configure, to pass extra'
		echo 'flags to the compiler and/or linker.'
		echo
		exit 0
		;;
	esac
done

check_header()
{
	if echo "#include <$2>" | cpp $CFLAGS >/dev/null 2>&1; then
		echo "Looking for $2 ... OK"
		eval have_$1=true
		return 0
	else
		echo "Looking for $2 ... not found"
		eval have_$1=false
		return 1
	fi
}

check_header xrandr X11/extensions/Xrandr.h || \
	echo "libXrandr is an optional dependency, but it's highly recommended to install it and re-run configure, if possible."
check_header png png.h || exit 1
check_header jpeg jpeglib.h || exit 1

echo 'Generating Makefile ...'
echo '# Generated by configure, do not edit. Edit Makefile.in instead' >Makefile
echo "PREFIX = $prefix" >>Makefile

[ -n "$CFLAGS" ] && echo "CFLAGS_cfg = $CFLAGS" >>Makefile
[ -n "$LDFLAGS" ] && echo "LDFLAGS_cfg = $LDFLAGS" >>Makefile

$opt && echo 'opt = -O3' >>Makefile
if $dbg; then
	echo 'dbg = -g' >>Makefile
else
	echo 'dbg = -DNDEBUG' >>Makefile
fi

if $have_xrandr; then
	echo 'CFLAGS_xrandr = -DHAVE_XRANDR' >>Makefile
	echo 'LDFLAGS_xrandr = -lXrandr' >>Makefile
fi

cat Makefile.in >>Makefile

echo 'Generating plugins/Makefile ...'
echo '# Generated by configure, do not edit.' >plugins/Makefile
if [ -n "$CFLAGS" ]; then
	echo "CFLAGS_cfg = $CFLAGS" >>plugins/Makefile
	echo 'export CFLAGS_cfg' >>plugins/Makefile
fi
if [ -n "$LDFLAGS" ]; then
	echo "LDFLAGS_cfg = $LDFLAGS" >>plugins/Makefile
	echo 'export LDFLAGS_cfg' >>plugins/Makefile
fi
echo '.PHONY: all clean install uninstall' >>plugins/Makefile
echo "all: $plugins" >>plugins/Makefile
echo "clean: $(echo $plugins     | sed 's/\(^\| \)/ clean-/g')" >>plugins/Makefile
echo "install: $(echo $plugins   | sed 's/\(^\| \)/ install-/g')" >>plugins/Makefile
echo "uninstall: $(echo $plugins | sed 's/\(^\| \)/ uninstall-/g')" >>plugins/Makefile
echo >>plugins/Makefile

for i in $plugins; do
	echo "# $i plugin" >>plugins/Makefile
	echo ".PHONY: $i clean-$i install-$i uninstall-$i" >>plugins/Makefile
	echo "$i:" >>plugins/Makefile
	echo "	\$(MAKE) -C $i" >>plugins/Makefile
	echo >>plugins/Makefile
	echo "clean-$i:" >>plugins/Makefile
	echo "	\$(MAKE) -C $i clean" >>plugins/Makefile
	echo >>plugins/Makefile
	echo "install-$i:" >>plugins/Makefile
	echo "	\$(MAKE) -C $i install" >>plugins/Makefile
	echo >>plugins/Makefile
	echo "uninstall-$i:" >>plugins/Makefile
	echo "	\$(MAKE) -C $i uninstall" >>plugins/Makefile
	echo >>plugins/Makefile
done
