#!/bin/sh

#
# A configure script
# 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/^(.*[^0-9.]|)([0-9][0-9]*(\.[0-9]*|)).*/\2/p')"
	PKGVER="$($1 --version | sed -rn 's/^(.*[^0-9.]|)([0-9][0-9]*(\.[0-9]*|)).*/\2/p')"
	if [ -z "$3" ]; then
		echo "configuring dvdspanky with $2"
	else
		echo "configuring dvdspanky with $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 _addconfigiso
{
	REQVER="$(echo $3 | sed -rn 's/^(.*[^0-9.]|)([0-9][0-9]*(\.[0-9]*|)).*/\2/p')"
	PKGVER="$($1 --version | sed -rn 's/^(.*[^0-9.]|)([0-9][0-9]*(\.[0-9]*|)).*/\2/p')"
	if [ -z "$3" ]; then
		echo "configuring isospanky with $2"
	else
		echo "configuring isospanky with $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
	ISOINCCFLAGS="$(echo $ISOINCCFLAGS $($1 --cflags))"
	ISOINCLIBS="$(echo $ISOINCLIBS $($1 --libs))"
}

function _findbin
{
	if [ -z "$3" ]; then
		VERSIONARG=--version
	else
		VERSIONARG=$3
	fi
	if [ -z "$2" ]; then
		echo "probing for $1"
	else
		echo "probing for $1 (>=$2)"
		REQVER="$(echo $2 | sed -rn 's/^(.*[^0-9.]|)([0-9][0-9]*(\.[0-9]*|)).*/\2/p')"
	fi
	PKGVER="$($1 $VERSIONARG 2>&1 | sed -rn 's/^(.*[^0-9.]|)([0-9][0-9]*(\.[0-9]*|)).*/\2/p')"
	if [ -z "$PKGVER" ]; then
		echo "** WARNING: $1 not installed **"
	fi
	if [ ! -z "$2" ] && [ "$(echo "$PKGVER>=$REQVER" | bc)" == "0" ]; then
		echo "** WARNING: $1 version $PKGVER not >= $REQVER **"
	fi
}

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

# check tools
echo checking for sed
if [ -z "$(sed --version 2> /dev/null | xargs echo)" ]; then
	echo sed not found
	exit 1
fi
echo checking for gcc
if [ -z "$(gcc --version 2> /dev/null | xargs echo)" ]; 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

# library/binary configuration
_addconfig pcre-config pcre 5.0
_findbin transcode
_findbin ffmpeg
_findbin mplayer
_findbin mplex "" ""
_findbin tcprobe "" -v
_findbin feh
_findbin sox 13.0

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

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

# create object targets and depends
echo "scanning depends"
for FILE in $(ls src/*.c); do
	OBJFILE="$(echo ${FILE} | sed -nr 's/^src\/(.*)\.c/obj\/\1\.o/p')"
	DEPENDS=""
	for HEADER in $(cat ${FILE} | sed -nr 's/^#include "(.*)"/\1/p' | grep -v config.h); do
		if [ ! -z "${HEADER}" ]; then
			if [ ! -z "${DEPENDS}" ]; then
				DEPENDS="${DEPENDS} src/${HEADER}"
			else
				DEPENDS="src/${HEADER}"
			fi
		fi
	done
	echo "\$(eval \$(call object_template,${OBJFILE},${FILE},${DEPENDS}))" >> ${MAKEINC}
	echo "" >> ${MAKEINC}
done

# done
mkdir -p obj
echo "success"

