#!/bin/sh

prefix=/usr/local

libs=`cat << EOF
glib-2.0
gtk+-2.0
gtk+-x11-2.0
EOF
`
clear_config() {
	rm -f config.mak 2>/dev/null
}

add_config() {
	printf "%s\n" "$1" >> config.mak
}

add_cflags() {
	add_config "CFLAGS += $1"
}

add_ldflags() {
	add_config "LDFLAGS += $1"
}

get_pkgconf_cflags() {
	pkg-config --cflags "$1" 2>/dev/null
}

get_pkgconf_ldflags() {
	pkg-config --libs "$1" 2>/dev/null
}

add_pkgconf_cflags() {
	fallback="$2"
	flags=$(get_pkgconf_cflags "$1")
	[ -z "$flags" ] && flags="$fallback"
	add_cflags "$flags"
}

add_pkgconf_ldflags() {
	fallback="$2"
	flags=$(get_pkgconf_ldflags "$1")
	[ -z "$flags" ] && flags="$fallback"
	add_ldflags "$flags"
}

add_lib() {
	add_pkgconf_cflags "$1" "$2"
	add_pkgconf_ldflags "$1" "$3"
}

check_program() {
	program="$1"
	needed="$2"
	result=$(which "$program" 2>/dev/null)
	if [ "$needed" = "1" ] && [ -z "$result" ] ; then
		echo "error: needed program $program not found" >&2
		kill -9 $$
	fi
	printf "%s\n" "$program"
}

usage() {
	echo "supported arguments"
	echo "--prefix=/path 		default: $prefix"
	echo "--exec_prefix=/path	default: $prefix/bin"
	echo "--bindir=/path		default: $prefix/bin"
	echo "--libdir=/path		default: $prefix/lib"
	echo "--includedir=/path	default: $prefix/include"
	echo "--sysconfdir=/path	default: $prefix/etc"
	echo "--help : show this text"
	exit 1
}

ismac() {
	uname -s | grep Darwin >/dev/null
}

isbsd() {
	uname -s | grep BSD >/dev/null
}

spliteq() {
	arg=$1
	echo "${arg#*=}"
	#alternatives echo "$arg" | cut -d= -f2-
	# or echo "$arg" | sed 's/[^=]*=//'
}

parsearg() {
	case "$1" in
	--prefix=*) prefix=`spliteq $1`;;
	--exec_prefix=*) exec_prefix=`spliteq $1`;;
	--bindir=*) bindir=`spliteq $1`;;
	--libdir=*) libdir=`spliteq $1`;;
	--includedir=*) includedir=`spliteq $1`;;
	--sysconfdir=*) sysconfdir=`spliteq $1`;;
	--help) usage;;
	esac
}

while true ; do
	case $1 in
	-*) parsearg "$1"; shift;;
	*) break ;;
	esac
done

[ -z "$exec_prefix" ] && exec_prefix=$prefix
[ -z "$libdir" ] && libdir=$prefix/lib
[ -z "$includedir" ] && includedir=$prefix/include
[ -z "$sysconfdir" ] && sysconfdir=$prefix/etc
[ -z "$bindir" ] && bindir=$exec_prefix/bin
[ -z "$CC" ] && CC=cc

clear_config

add_config "prefix = $prefix"
add_config "exec_prefix = $exec_prefix"
add_config "bindir = $bindir"
add_config "libdir = $libdir"
add_config "includedir = $includedir"
add_config "sysconfdir = $sysconfdir"

add_config "CC ?= $CC"
[ -z "$CPPFLAGS" ] || add_config "CPPFLAGS ?= $CPPFLAGS"
[ -z "$CFLAGS" ] ||   add_config "CFLAGS ?= $CFLAGS"

add_cflags "-DXCHATLIBDIR=\\\"$libdir/xchat\\\""
add_cflags "-DXCHATSHAREDIR=\\\"$prefix/share/xchat\\\""
add_cflags "-DUSE_OPENSSL"

for lib in $libs ; do add_lib "$lib" ; done
add_lib openssl "" "-lssl -lcrypto"

prog1=$(check_program "gdk-pixbuf-csource" 1)
add_config "PIXMAPCONVERT = $prog1"

# plugin stuff ...

use_plugin=

perl=$(check_program "perl" 0)
if [ -n "$perl" ] ; then
	printf "perl detected, plugin activated\n"
	PERL_CFLAGS=`$perl -MExtUtils::Embed -e ccopts 2>/dev/null`
	PERL_LDFLAGS=`$perl -MExtUtils::Embed -e ldopts 2>/dev/null | sed 's/-lgdbm //'`
	add_config "PERL_CFLAGS = $PERL_CFLAGS"
	add_config "PERL_LDFLAGS = $PERL_LDFLAGS"
	add_config "PLUGINS += perl.so"
	if [ -z "$use_plugin" ] ; then
		add_cflags "-DUSE_PLUGIN"
		use_plugin=1
	fi
	# there are actually 2 plugins, "old" and new, but all scripts i've seen only use old
	add_cflags "-DOLD_PERL"
fi

python=$(check_program "python" 0)
if [ -n "$python" ] ; then
	printf "python detected, plugin activated\n"
	printf "WARNING: python plugin needs environment variable PYTHONHOME\n"
	printf "  set to python's prefix.\n"
	printf "  lack of that environment variable will cause xchat exitting\n"
	printf "  when the plugin is loaded.\n"
	printf "  on sabotage linux, use \"PYTHONHOME=/ ixchat\"\n"
	printf "  to launch the program.\n"

	PY_CFLAGS=$(get_pkgconf_cflags python)
	[ -z "$PY_CFLAGS" ] && PY_CFLAGS=$(get_pkgconf_cflags python-2.7)
	if  [ -z "$PY_CFLAGS" ] ; then
		for i in "$prefix" /usr /usr/local/ ; do
			printf "searching Python.h in $i/include/python2.7 ... "
                        if [ -e "$i"/include/python2.7/Python.h ] ; then
				PY_CFLAGS=-I"$i"/include/python2.7
				printf "SUCCESS\n"
				break;
			else
				printf "FAIL\n"
			fi
		done
	fi
	PY_LDFLAGS=$(get_pkgconf_ldflags python)
	[ -z "$PY_LDFLAGS" ] && PY_LDFLAGS=$(get_pkgconf_ldflags python-2.7)
	[ -z "$PY_LDFLAGS" ] && PY_LDFLAGS=-lpython2.7
	if [ ! -z "$PY_CFLAGS" ] ; then
		add_config "PY_CFLAGS = $PY_CFLAGS"
		add_config "PY_LDFLAGS = $PY_LDFLAGS"
		add_config "PLUGINS += python.so"
		if [ -z "$use_plugin" ] ; then
			add_cflags "-DUSE_PLUGIN"
			use_plugin=1
		fi
	else
		printf "python not found, skipping plugin\n"
	fi
fi

tcl=$(check_program "tclsh" 0)
if [ -n "$tcl" ] ; then
	printf "tcl detected, plugin activated\n"
	TCL_CFLAGS=$(get_pkgconf_cflags tcl)
	TCL_LDFLAGS=$(get_pkgconf_ldflags tcl)
	tcl_error=
	# tcl 8.5 does not come with a pkgconfig file
	# tcl 8.6. comes with a buggy one that fails to include its own library
	# https://sourceforge.net/tracker/?func=detail&aid=3598298&group_id=10894&atid=110894
	# hopefully tcl 8.7 will come with a *working* pkgconfig file ...
	# until that happens, we need to fall back to its proprietary tclConfig system.
	if [ -z "$TCL_LDFLAGS" ] || ! echo "$TCL_LDFLAGS" | grep "[-]ltcl" > /dev/null ; then
		tcl_found=
		for i in "$libdir" "$prefix"/lib/ "$prefix"/lib64/ /lib /lib64 /usr/lib /usr/lib64 ; do
			printf "searching tclConfig.sh in $i ... "
			if [ -e "$i"/tclConfig.sh ] ; then
				. "$i"/tclConfig.sh
				TCL_LDFLAGS="$TCL_LIB_SPEC $TCL_LIBS"
				tcl_found=1
				printf "SUCCESS\n"
				break
			else
				printf "FAIL\n"
			fi
		done
		if [ -z "$tcl_found" ] ; then
			echo "warning: tclConfig.sh not found and no pkgconfig entry available"
			echo "disabling tcl plugin"
			tcl_error=1
		fi
	fi
	if [ -z "$tcl_error" ] ; then
		add_config "TCL_CFLAGS = $TCL_CFLAGS"
		add_config "TCL_LDFLAGS = $TCL_LDFLAGS"
		add_config "PLUGINS += tcl.so"
		if [ -z "$use_plugin" ] ; then
			add_cflags "-DUSE_PLUGIN"
			use_plugin=1
		fi
	fi
fi

for i in libsexy gtkspell-2.0 ; do
	spell_flags=$(get_pkgconf_cflags $i)
	if [ ! -z "$spell_flags" ] ; then
		add_cflags "$spell_flags"
		spell_flags=$(get_pkgconf_ldflags $i)
		add_ldflags "$spell_flags"
		if [ "$i" = "libsexy" ] ; then
			add_cflags "-DUSE_LIBSEXY"
		else
			add_cflags "-DUSE_GTKSPELL"
		fi
		break
	fi
done

if isbsd || ismac ; then
	add_config "INSTALL_FLAGS=-m"
fi


echo done, now run make \&\& make install

