#!/bin/sh

#
# dvdspanky configure - a configure script for dvdspanky
# Copyright (C) 2007  Jeffrey Grembecki
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

#functions
function _addconfig
{
	if [ -z "$3" ]; then
		echo "configuring $2"
	else
		echo "configuring $2 (>=$3)"
	fi
	PKGVER="$($1 --version)"
	if [ -z "$PKGVER" ]; then
		echo "library not installed"
		echo "failed"
		exit 1
	fi
	if [ ! -z "$3" ] && [ "$(echo $PKGVER $3 | awk '{if ($1 >= $2) print "ok";}')" != "ok" ]; then
		echo "library version $PKGVER not >= $3"
		echo "failed"
		exit 1
	fi
	INCCFLAGS="$(echo $INCCFLAGS $($1 --cflags))"
	INCLIBS="$(echo $INCLIBS $($1 --libs))"
}

function _findbin
{
	echo "configuring $1"
	NEWDEF="$(whereis $1 | awk '{print $2}')"
	if [ -z "$NEWDEF" ]; then
		echo "program not installed"
		echo "failed"
		exit 1
	fi
	echo "#define $2 \"${NEWDEF}\"" >> config.h
}

# init
MAKEINC=make.in
INCLIBS=
INCCFLAGS=
PREFIX="/usr/local"

# options
while [ ! -z "$1" ]; do
	OPTS=1
	if [ ! -z "$(echo $1 | sed -rn 's/^-[[:alpha:]]$/\0/p')" ]; then
		echo $1 = 1
		OPTA="$(echo $1 | sed -rn 's/^-(.)/\1/p')"
		OPTB=$2
		OPTS=2
	elif [ ! -z "$(echo $1 | sed -rn 's/^--[[:alpha:]].*/\0/p')" ]; then
		OPTA="$(echo $1 | sed -rn 's/^--(.*)=(.*)/\1/p')"
		if [ -z "$OPTA" ]; then
			OPTA="$(echo $1 | sed -rn 's/^--(.*)/\1/p')"
		fi
		OPTB="$(echo $1 | sed -rn 's/^--(.*)=(.*)/\2/p')"
		if [ -z "$OPTB" ]; then
			OPTB="$2";
			OPTS=2;
		fi
	else
		echo "unknown option $1"
		exit 1
	fi
	case "$OPTA" in
		prefix)
			if [ ! -z "$OPTB" ]; then
				PREFIX="$OPTB";
				shift $OPTS;
			else
				echo "no prefix argument given"
				exit 1;
			fi
			;;
		help)
			echo " --prefix=DIR  set the install prefix, such as /usr"
			exit 0
			;;
		*)
			echo "unknown option $OPTA"
			exit 1
			;;
	esac
done

# cleanup
rm ${MAKEINC} 2> /dev/null

# version
VERSION=$(cat dvdspanky.c | grep 'gp_title\[\]' | sed -rn 's/.*dvdspanky\s([^ ]*).*/\1/p')
echo "version is $VERSION"

# prefix
echo "prefix is $PREFIX"
PREFIX="$PREFIX"

# start config.h
echo "#ifndef __CONFIG_H
#define __CONFIG_H
" > config.h


# library/binary configuration
_findbin transcode TRANSCODE_BIN
_findbin mplayer MPLAYER_BIN
_findbin mplex MPLEX_BIN
_findbin tcprobe TCPROBE_BIN
_findbin feh FEH_BIN
_addconfig pcre-config pcre 5.0

# write makefile.include
echo "PREFIX=$PREFIX" >> ${MAKEINC}
echo "VERSION=$VERSION" >> ${MAKEINC}
echo "INCCFLAGS=$INCCFLAGS" >> ${MAKEINC}
echo "INCLIBS=$INCLIBS" >> ${MAKEINC}

# end config.h
echo "
#endif /* __CONFIG_H */
" >> config.h


# done
echo "success"

