#!/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
{
	REQVER="$(echo $3 | sed -rn 's/^([[:digit:]]*(\.[[:digit:]]*|)).*/\1/p')"
	PKGVER="$($1 --version | sed -rn 's/^([[:digit:]]*(\.[[:digit:]]*|)).*/\1/p')"
	if [ -z "$3" ]; then
		echo "configuring $2"
	else
		echo "configuring $2 (>=$REQVER)"
	fi
	if [ -z "$PKGVER" ]; then
		echo "library not installed"
		echo "failed"
		exit 1
	fi
	if [ ! -z "$3" ] && [ "$(echo \"$PKGVER\>$REQVER\" | bc)" == "0" ]; then
		echo "library version $PKGVER not >= $REQVER"
		echo "failed"
		exit 1
	fi
	INCCFLAGS="$(echo $INCCFLAGS $($1 --cflags))"
	INCLIBS="$(echo $INCLIBS $($1 --libs))"
}

function _findbin
{
	echo "configuring $1"
	NEWDEF="$(whereis $1 | sed -rn "s/^[^ ]*[ ]([^ ]*$1).*$/\1/p")"
	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"

# check tools
echo checking for sed
if [ "$(sed --version | sed -nr 's/^.*(version|v|V)[ ]*([[:digit:]]*\.[[:digit:]]*).*/\2>3/gp' | bc)" != "1" ]; then
	echo sed not found
	exit 1
fi
echo checking for whereis
if [ "$(whereis whereis | sed -nr 's/^(whereis).*/\1/pg')" != "whereis" ]; then
	echo whereis not found
	exit 1
fi
echo checking for gcc
if [ "$(gcc --version | sed -nr 's/.*(Free Software Foundation).*/\1/pg')" != "Free Software Foundation" ]; then
	echo gcc not found
	exit 1
fi

# 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

# 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

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

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

# 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"

