#!/bin/sh
# Configure script for kvideoencoder
 
print_help()
{
	echo "Usage: configure [OPTION]... [VAR=VALUE]"
	echo
	echo "Configuration: "
	echo "  -h, --help		Display this help and exit"
	echo 
	echo "By default, 'make install' will install all the files in"
	echo "/usr/local/bin, /usr/local/lib etc. You can specify"
	echo "another installation prefix using '--prefix'."
	echo
	echo "Installation directories:"
	echo "  --prefix=PREFIX			install architecture-independent files in PREFIX"
	echo "					[/usr/local]"
	echo "  --exec-prefix=EPREFIX		install architecture-dependent files in EPREFIX"
	echo "					[PREFIX]"
	echo
	echo "Qt directories:"
	echo "  --with-qt-libraries=DIR		where the Qt libraries are"
	echo "  --with-qt-includes=DIR		where the Qt includes are"
	echo "  --with-qt-bin=DIR		where the Qt tools (qmake, uic, moc) are"
	echo
	echo "KDE directories:"
	echo "  --with-kde=DIR			where the KDE root directory is"
	echo "  --with-kde-includes=DIR		where the KDE header files are"
	echo "  --with-kde-libraries=DIR	where the KDE libraries are"
	echo
	exit 0
}

# set the standard values
prefix="/usr/local"
eprefix=$prefix
qt_libs=""
qt_inc=""
qt_bin=""

# Check the command line parameters
TEMP=`getopt -n configure -o h --long help,prefix:,exec-prefix:,with-qt-libraries:,with-qt-bin:,with-kde:,with-kde-includes:,with-kde-libraries: -- "$@"`

if [ $? != 0 ]; then 
	exit 1; 
fi

eval set -- "$TEMP"

while true ; do
	case "$1" in
 		-h|--help) 		print_help; shift;;
 		--prefix) 		prefix=$2 ; eprefix=$prefix; shift 2;;
 		--exec-prefix)		eprefix=$2 ; shift 2;; 
 		--with-qt-libraries)	qt_libs=$2 ; shift 2;;
 		--with-qt-includes)	qt_inc=$2 ; shift 2;;
 		--with-qt-bin)		qt_bin=$2 ; shift 2;;
 		--with-kde)		kde_pref=$2 ; shift 2;;
 		--with-kde-includes)    kde_inc=$2 ; shift 2;;
 		--with-kde-libraries)	kde_libs=$2; shift 2;;
 		--) shift ; break ;;
	esac	
done

# Checking for g++...
printf "Checking for g++... "
compiler="`which g++`"
if [ "$compiler" = "" ]; then
	compiler="`which g++-3.3`"
	if [ "$compiler" = "" ]; then
		compiler="`which g++-3.4`"
		if [ "$compiler" = "" ]; then
			echo "none found".
			echo
			echo "Configure searched for g++, g++-3.3 and g++-3.4. If your compiler has got another name"
			echo "please tell me and I'll improve the configure script: <kvideoencoder@gmx.net>"
			exit 1
		fi
	fi
fi
echo $compiler;
rm -rf _cfg_prog
mkdir _cfg_prog
export PATH=$PATH:`pwd`/_cfg_prog
ln -s $compiler _cfg_prog/g++

# Checking for Qmake...
printf "Checking for qmake... "
qmake=""
if [ "$qt_bin" = "" ]; then
	qmake="`which qmake`"
	if [ "$qmake" = "" ]; then
		echo "not found!"
		echo
		echo "Make sure you have the Qt development packages installed!"
		exit 1
	fi
fi
if [ "$qmake" = "" ]; then
	qmake="$qt_bin/qmake"
	if [ ! -x $qmake ]; then
		echo "not found!"
		echo
		echo "Make sure you have the Qt development packages installed!"
		exit 1
	fi
fi
echo $qmake;

# Checking for kde-config
printf "Checking for kde-config..."
kde_config="`which kde-config`"

if [ "$kde_config" = "" ]; then
	echo "not found;"
	if [ "$kde_pref" = "" ]; then
		echo 
		echo "Configure cannot find the KDE directory."
		echo "Please try to set it with --with-kde=/location-of-kde"
		exit 1
	fi

	kde_config="$kde_pref/bin/kde-config";
	if [ ! -x $kde_config ]; then
		kde_config="";
	fi
	
	if [ "$kde_config" = "" ]; then 
		echo "not found";
	else 
		echo $kde_config;
	fi
else
	echo $kde_config;
fi		

# Checking for KDE
printf "Checking for KDE..."
if [ "$kde_config" != "" ]; then
	kde="`$kde_config --prefix`"
	if [ "$kde" = "" ]; then
		echo "not found; assuming $prefix"
		echo "Try to set it using --with-kde"
		kde_pref=$prefix;
	else 
		kde_pref=$kde;
		echo $kde		
	fi
fi

# Checking for KDE includes
printf "Checking for KDE includes..."
if [ "$kde_inc" = "" ]; then	
	kde_inc="$prefix/include/";
	if [ "$kde_pref" != "" ]; then kde_inc="$kde_pref/include"; fi
	if [ ! -r "$kde_inc/kapplication.h" ]; then
		kde_inc="$eprefix/include";
		if [ ! -r "$kde_inc/kapplication.h" ]; then
			kde_inc="$eprefix/include/kde";
			if [ ! -r "$kde_inc/kapplication.h" ]; then
				echo "not found."
				echo
				echo "Please install the KDE development packages,"
				echo "or try to set the KDE-Includes path with the commandline option --with-kde-includes"		
				exit 1
			fi
		fi
	fi
fi
echo $kde_inc

# Checking for KDE libraries
printf "Checking for KDE libraries..."
if [ "$kde_libs" = "" ]; then	
	kde_libs="$prefix/lib/";
	if [ "$kde_pref" != "" ]; then kde_libs="$kde_pref/lib"; fi
	if [ ! -r "$kde_libs/libkio.la" ]; then
		kde_libs="$eprefix/lib";
		if [ ! -r "$kde_libs/libkio.la" ]; then
			echo "not found."
			echo
			echo "Please install the KDE development packages,"
			echo "or try to set the KDE-Libraries path with the commandline option --with-kde-libraries"		
			exit 1
		fi
	fi
fi
echo $kde_libs

# Now create the makefile
echo "Creating: Makefile"
(cat > Makefile) << !MAKEFILE!
all: kvideoencoder

kvideoencoder:
	@cd src && make

install: kvideoencoder
	@install -D src/kvideoencoder \$(DESTDIR)/$eprefix/bin/kvideoencoder
	@mkdir -p \$(DESTDIR)/etc
	@echo $prefix > \$(DESTDIR)/etc/kvideoencoder
	@mkdir -p \$(DESTDIR)/$prefix/share/apps/kvideoencoder
	@install -D src/*qm \$(DESTDIR)/$prefix/share/apps/kvideoencoder/
	@install -D src/kvideoencoder.desktop \$(DESTDIR)/$prefix/share/applications/kde/kvideoencoder.desktop
 
uninstall:
	rm $eprefix/bin/kvideoencoder
	rm /etc/kvideoencoder
	rm -rf $prefix/share/apps/kvideoencoder/
	rm $prefix/share/applications/kvideoencoder.desktop
	
clean:
	rm -rf _cfg_prog
	@cd src && make clean
	
distclean:
	rm -rf _cfg_prog
	if [ -f src/kvideoencoder.pro~ ]; then mv src/kvideoencoder.pro~ src/kvideoencoder.pro; fi
	@cd src && make distclean
	rm Makefile
	rm src/Makefile	
   
!MAKEFILE!
echo "Creating: src/Makefile"

# Update *.pro file
if [ -f src/kvideoencoder.pro~ ]; then mv src/kvideoencoder.pro~ src/kvideoencoder.pro; fi
cp src/kvideoencoder.pro src/kvideoencoder.pro~
if [ "$qt_libs" != "" ]; then echo "QMAKE_LIBDIR_QT = $qt_libs" >> src/kvideoencoder.pro; fi
if [ "$qt_inc" != "" ]; then echo "QMAKE_INCDIR_QT = $qt_inc" >> src/kvideoencoder.pro; fi
echo "INCLUDEPATH = $kde_inc" >> src/kvideoencoder.pro
echo "unix:LIBS += -L$kde_libs" >> src/kvideoencoder.pro

# run qmake
cd src && $qmake

# OK!
echo
echo "Installation prefix is: $prefix"
echo "Installation prefix for the executable is: $eprefix"
echo
echo "Configure finished. Start make now."