#!/bin/bash 

#************************************************************************
# QBASystem
# version 0.9.7
# Copyright (c) 2003-2004 Gianluca Romanin ( J_Zar )
#        <j_zar_AT_users.berlios.de>
#************************************************************************

#************************************************************************
#
#  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#************************************************************************



# inserting blanks...
echo
echo "-----------------------------------------------------------"

######################### Main stage
#

# define temp files
H_TEMP=".help_temp" 
L_TEMP=".l_temp" 
CONFIGURE_DEF=".configure.def" 
SUMMARY_LOG=".summary.log" 


# remove temp files if there are...
rm -f $L_TEMP
rm -f $H_TEMP
rm -f a.out

# Here set up the "help message" -------------------------
if   test "$1" = "--help" || test "$1" = "-h"  || test "$1" = "--h";
then
	CONFIGURE_OPTIONS=$(cat build.definition | grep "CONFIGURE_SCRIPT_OPTIONS" -0 -w | cut -f2 -d"=" ) ;
	CONFIGURE_FEATURES=$(cat build.definition | grep "CONFIGURE_SCRIPT_FEATURES" -0 -w | cut -f2 -d"=" ) ;
	CONFIGURE_PACKAGES=$(cat build.definition | grep "CONFIGURE_SCRIPT_PACKAGE_LIST" -0 -w | cut -f2 -d"=" ) ;
	
	cat help ;
	echo " " ;
	
	if [ "$CONFIGURE_OPTIONS" != "" ] ; 
	then
		echo " " ;
		echo "Custom options:" ;
		echo " " ;
		echo "  Add a custom option to configuration with" ;
# 		echo " " ;
		
		for i in $CONFIGURE_OPTIONS ; do
			DESCRIPTION=$(cat build.definition | grep "${i}_HELP_DESCRIPTION" -0 -w | cut -f2 -d"=" ) ;
			echo "  --$i	$DESCRIPTION" ;
		done
		echo " " ;
	fi
	
	if [ "$CONFIGURE_FEATURES" != "" ] ; 
	then
		echo " " ;
		echo "Features:" ;
		echo " " ;
		echo "  Enable/disable feature's related code with" ;
# 		echo " " ;
		
		for i in $CONFIGURE_FEATURES ; do
			echo "  --enable-$i" ;
			echo "  --disable-$i" ;
		done
		echo " " ;
	fi
	
	if [ "$CONFIGURE_PACKAGES" != "" ] ; 
	then
		echo " " ;
		echo "Package custom configuration:" ;
		echo " " ;
		echo "  Set custom DIR for a package with" ;
# 		echo " " ;
		
		for i in $CONFIGURE_PACKAGES ; do
			echo "  --with-$i=DIR" ;
		done
		echo " " ;
	fi	
	
	echo " " ;
	echo "-----------------------------------------------------------" ;
	echo "-----------------------------------------------------------" ;
	echo " " ;
	exit
fi

# We should locate a project file:
# if not defined in build.definition
# we'll retrieve by yourself.
# If more than one then, we take only the first.
PROJECT_FILE=$(cat build.definition | grep "PROJECT_FILE" -0 -w | cut -f2 -d"=" ) ;
if [ "$PROJECT_FILE" = "" ] ;
then 
	ls *.pro 2> proj_temp 1> /dev/null ;
	if cat proj_temp  | grep "No such file" -q ;	
	then
		rm -f proj_temp ;
		echo "Can't locate a project file!" ;
		exit 
	else
		ls --format=commas *.pro 1> proj_temp ;
		PROJECT_FILE=$(cat proj_temp | cut -f1 -d",") ;
		rm -f proj_temp ;
	fi
fi
# retrieve project path 
# KNOWN BUG: If user executes the script environment not in the src
# dir, then script system will surely fail!
PROJECT_PATH=$PWD ;	



echo "Project file is:      $PROJECT_FILE" ;
#echo "Project path is:      $PROJECT_PATH" ;

# cleanup phase
rm -f $CONFIGURE_DEF ;
rm -f $SUMMARY_LOG ;
rm -f config.h ;


# save project path variable
echo "PROJECT_PATH = $PROJECT_PATH" >> $CONFIGURE_DEF ;

# number of arguments + 1
NUM_OPT=$(($# + 1));

# if we have arguments, we update the user friendly output...
if [ "$NUM_OPT" != "1" ] ;
then 
	echo "CUSTOM_BUILD = true" >> $CONFIGURE_DEF
fi

# retrieve the project type: Qt or not Qt?
TMP=$(cat build.definition | grep "no_qt" -0 -w) ;
if [ "$TMP" = "" ];
then
	QT_PROJECT=1 ;
fi

# Init the warning configuration
echo "CONFIG	+= warn_on" >> $CONFIGURE_DEF


# parsing arguments-------------------------------------------->>
for (( d=1; d<$NUM_OPT; d++ )); do

	# retrieve the field $d
	ARG=$(echo $@ | cut -f$d -d" ") ;
	# retrieve the option
	OPT=$(echo $ARG | cut -f1 -d"=") ;
	# retrieve the value
	VAL=$(echo $ARG | cut -f2- -d"=") ;

	if [ "$OPT" = "--cflags" ];
	then
		T=""
		for (( y=1; y<128; y++ )); do
			# parse option value to find different subvalues
			V=$(echo $VAL | cut -f$y -d";") ;
			# checking to avoid duplicates or if we have finished
			if [ "$V" = "" ] || [ "$V" = "$T" ] ;
			then
				break
			fi
			T=$V ;
			echo "QMAKE_CFLAGS = $V" >> $CONFIGURE_DEF
		done
		STATUS="ok"
	fi

	if [ "$OPT" = "--cxxflags" ];
	then
		T=""
		for (( y=1; y<128; y++ )); do
			# parse option value to find different subvalues
			V=$(echo $VAL | cut -f$y -d";") ;
			# checking to avoid duplicates or if we have finished
			if [ "$V" = "" ] || [ "$V" = "$T" ] ;
			then
				break
			fi
			T=$V ;
			echo "QMAKE_CXXFLAGS = $V" >> $CONFIGURE_DEF
		done
		STATUS="ok"
	fi

	if [ "$OPT" = "--lflags" ];
	then
		T=""
		for (( y=1; y<128; y++ )); do
			# parse option value to find different subvalues
			V=$(echo $VAL | cut -f$y -d";") ;
			# checking to avoid duplicates or if we have finished
			if [ "$V" = "" ] || [ "$V" = "$T" ] ;
			then
				break
			fi
			T=$V ;
			echo "QMAKE_LFLAGS = $V" >> $CONFIGURE_DEF
		done
		STATUS="ok"
	fi

	if [ "$OPT" = "--dependency-dir" ];
	then
		T=""
		for (( y=1; y<6; y++ )); do
			# parse option value to find different subvalues
			V=$(echo $VAL | cut -f$y -d";") ;
			# checking to avoid duplicates or if we have finished
			if [ "$V" = "" ] || [ "$V" = "$T" ] ;
			then
				break
			fi
			T=$V ;
			echo "PATH$y = $V" >> $CONFIGURE_DEF
		done
		STATUS="ok"
	fi


	if [ "$OPT" = "--prefix" ];
	then
		echo "PREFIX = $VAL" >> $CONFIGURE_DEF
		STATUS="ok"
	fi
	
	if [ "$OPT" = "--disable-warnings" ];
	then
		echo "CONFIG	-= warn_on" >> $CONFIGURE_DEF
		echo "CONFIG	+= warn_off" >> $CONFIGURE_DEF
		STATUS="ok"
	fi

	if [ "$OPT" = "--static-link" ];
	then
		# correcting a wide bug ;-(
		if [ "$VAL" == "--static-link" ];
		then
			VAL="1";
		fi
		echo "STATIC_LIBPATH = $VAL" >> $CONFIGURE_DEF
		STATUS="ok"
	fi

	if [ "$OPT" = "--enable-debug" ];
	then
		echo "CONFIG += debug" >> $CONFIGURE_DEF
		DBG=$(cat build.definition | grep "enable_config_h" -q );
		if [ "$DBG" != "" ];
		then
			echo "#define	DEBUG	1" >> config.h.in
		fi
		STATUS="ok"
	fi
	
	if echo $OPT | grep -q -e "--enable-" ;
	then
		V=$(echo $OPT | cut -f4 -d"-") ;
		echo "ENABLED_FEATURES += $V" >> $CONFIGURE_DEF
		STATUS="ok"
	fi
	
	if echo $OPT | grep -q -e "--disable-" ;
	then
		V=$(echo $OPT | cut -f4 -d"-") ;
		echo "DISABLED_FEATURES += $V" >> $CONFIGURE_DEF
		STATUS="ok"
	fi
	
	if echo $OPT | grep -q -e "--with-" ;
	then
		V=$(echo $OPT | cut -f4 -d"-") ;
		echo "USER_DEFINED_PACKAGES += $V" >> $CONFIGURE_DEF
		echo "${V}_PATH += $VAL" >> $CONFIGURE_DEF
		STATUS="ok"
	fi
	
	if echo $OPT | grep -q -e "--" ;
	then
		V=$(echo $OPT | cut -f3 -d"-") ;
		echo $V ;
		CONF_OPT=$(cat build.definition | grep "CONFIGURE_SCRIPT_OPTIONS" -0 -w | cut -f2 -d"=" ) ;
		if [ "$CONF_OPT" != "" ] ; 
		then
			for i in $CONF_OPT ; do
				if [ "$V" == "$i" ] ;
				then
					echo "PROJECT_ACTIVE_OPTIONS += $V" >> $CONFIGURE_DEF ;
					if [ "$VAL" != "" ] ;
					then
						echo "${V}_OPTION_VALUE = $VAL" >> $CONFIGURE_DEF ;
					fi
					STATUS="ok" ;
				fi
			done
		fi
	fi

# OLD DEPRECATED STUFF!! ----------------------------->>
#	
# 	if [ "$OPT" = "--enable-features" ];
# 	then
# 		T=""
# 		for (( y=1; y<6; y++ )); do
# 			# parse option value to find different subvalues
# 			V=$(echo $VAL | cut -f$y -d";") ;
# 			# checking to avoid duplicates or if we have finished
# 			if [ "$V" = "" ] || [ "$V" = "$T" ] ;
# 			then
# 				break
# 			fi
# 			T=$V ;
# 			echo "ENABLED_FEATURES += $V" >> $CONFIGURE_DEF
# 		done
# 		STATUS="ok"
# 	fi
# 	if [ "$OPT" = "--disable-features" ];
# 	then
# 		T=""
# 		for (( y=1; y<6; y++ )); do
# 			# parse option value to find different subvalues
# 			V=$(echo $VAL | cut -f$y -d";") ;
# 			# checking to avoid duplicates or if we have finished
# 			if [ "$V" = "" ] || [ "$V" = "$T" ] ;
# 			then
# 				break
# 			fi
# 			T=$V ;
# 			echo "DISABLED_FEATURES += $V" >> $CONFIGURE_DEF
# 		done
# 		STATUS="ok"
# 	fi
# 
# 
# 
# 	if [ "$OPT" = "--libdir" ];
# 	then
# 		T=""
# 		for (( y=1; y<6; y++ )); do
# 			# parse option value to find different subvalues
# 			V=$(echo $VAL | cut -f$y -d";") ;
# 			# checking to avoid duplicates or if we have finished
# 			if [ "$V" = "" ] || [ "$V" = "$T" ] ;
# 			then
# 				break
# 			fi
# 			T=$V ;
# 			echo "LIBS += -L$V" >> $CONFIGURE_DEF
# 		done
# 		STATUS="ok"
# 	fi
#
#
# OLD DEPRECATED STUFF!! -----------------------------<< (END)


	if [ "$OPT" = "--includelib" ];
	then
		T=""
		for (( y=1; y<6; y++ )); do
			# parse option value to find different subvalues
			V=$(echo $VAL | cut -f$y -d";") ;
			# checking to avoid duplicates or if we have finished
			if [ "$V" = "" ] || [ "$V" = "$T" ] ;
			then
				break
			fi
			T=$V ;
			echo "LIBS += $V" >> $CONFIGURE_DEF
		done
		STATUS="ok"
	fi

	
	# if we have not parsed a valid argument
	# we score an error...
	if [ "$STATUS" = "ok" ];
	then
		STATUS="error"
	else
		echo
		echo "__ Wrong parameter!"
		echo "__ If unsure, type './configure --help'"
		echo 
		exit
	fi
done
# parsing arguments--------------------------------------------<< (END)


# rolling to go with qmake...

######################### QT Env variables checking
#
if  export | grep "QTDIR" -q ;
then
	RES="yes"
else
	RES="no"
fi

echo Checking whether QTDIR is set     ........    $RES   , $QTDIR

if echo $RES | grep "no" -q ;
then
	echo
	echo "__ QTDIR must be properly set."
	echo "__ Please see at http://www.trolltech.com, section developers"
	echo "__ and find the installation instructions."
	exit
fi

## section init: if qt project
if [ "$QT_PROJECT" = "1" ] ;
then

	######################### Qt headers ---> qglobal.h
	#
	if test -f $QTDIR/include/qglobal.h ;
	then
		QT_GLOBAL_HEADER="$QTDIR/include/qglobal.h" ;
		RES="yes"
	else
		find /usr -name qglobal.h 1> temp ;
		QT_GLOBAL_HEADER=$(cat temp) ;
		if [ "$QT_GLOBAL_HEADER" != "" ]
		then
			RES="yes"
		else
			RES="no"
		fi
	fi
	
	echo Checking for Qt headers    ........    $RES
	
	if test "$RES" = "no" ;
	then
		echo
		echo "__ I can't locate Qt headers. Just tried: $QTDIR/include"
		echo "__ If your headers are located in another path, please send a"
		echo "__ bug report to the QBAS developers ( http://qbas.berlios.de )."
		exit
	fi
	
	
	
	######################### QT Version >= $QT_MINIMUM_REQUIRED_VERSION (if it is set)
	#
	
	QT_MINIMUM_REQUIRED_VERSION=$(cat build.definition | grep "QT_MINIMUM_REQUIRED_VERSION" -0 -w | cut -f2 -d"=" ) ;
	QT_MINIMUM_REQUIRED_VERSION_STR=$(cat build.definition | grep "QT_MINIMUM_REQUIRED_VERSION_STR" -0 -w | cut -f2 -d"=" ) ;
	
	if [ "$QT_MINIMUM_REQUIRED_VERSION" != "" ] && [ "$QT_MINIMUM_REQUIRED_VERSION_STR" != "" ];
	then
		cat $QT_GLOBAL_HEADER | grep "QT_VERSION" -0 > temp ; 
		QVERSION=$(cat temp | grep "#define QT_VERSION" -0 -w | cut -f3 -d" " | cut -f2 -d"x") ;
		QVERSION_STR=$(cat temp | grep "QT_VERSION_STR" -0 -w | cut -f2 -d'"') ;
	
		if test $QVERSION -ge $QT_MINIMUM_REQUIRED_VERSION  ;
		then 
			RES="yes"
		else
			RES="no"
		fi
	
		rm -f temp ;
	
		echo Checking if Qt \>\= $QT_MINIMUM_REQUIRED_VERSION_STR    ........    $RES   , $QVERSION_STR
	
		if echo $RES | grep "no" -q ;
		then
			echo
			echo "__ Incorrect Qt version!"
			exit
		fi
	fi
	
	
	
	######################### QT library is thread type?
	#
	if test -e $QTDIR/lib/libqt-mt.so ;
	then
		QT_LIB="$QTDIR/lib" ;
		RES="yes"
		echo "CONFIG += thread" >> $CONFIGURE_DEF ;
	else
		find /usr -name libqt-mt.so 1> temp ;
		QT_LIB=$(cat temp) ;
		if [ "$QT_LIB" != "" ]
		then
			RES="yes"
			echo "CONFIG += thread" >> $CONFIGURE_DEF ;
			
			echo
			echo "__ Qt lib not in the expected path!"
			echo
			
			CRITICAL="1" ;
		else
			RES="no"
		fi
	fi
	
	echo Checking if Qt is -mt    ........    $RES 
	
	fi # end if !no_qt
	
	
	######################### qmake, uic, moc checking
	#
	if test -e $QTDIR/bin/qmake ;
	then
		QT_QMAKE_EXEC="$QTDIR/bin/qmake" ;
		RES="yes"
	else
		find /usr -name qmake 1> temp ;
		QT_QMAKE_EXEC=$(cat temp) ;
		if [ "$QT_QMAKE_EXEC" != "" ]
		then
			RES="yes"
			echo
			echo "__ qmake not in the expected path!"
			echo
			
			CRITICAL="1" ;
		else
			RES="no"
		fi
	fi
	
	echo Checking for qmake    ........    $RES , $QT_QMAKE_EXEC
	
	if test "$RES" = "no" ;
	then
		echo
		echo "__ I can't locate qmake."
		exit
	fi
	
	## section init: if qt project
	if [ "$QT_PROJECT" = "1" ] ;
	then
	
	if test -e $QTDIR/bin/uic ;
	then
		QT_UIC_EXEC="$QTDIR/bin/uic" ;
		RES="yes"
	else
		find /usr -name uic 1> temp ;
		QT_UIC_EXEC=$(cat temp) ;
		if [ "$QT_UIC_EXEC" != "" ]
		then
			RES="yes"
			echo
			echo "__ uic not in the expected path!"
			echo
			
			CRITICAL="1" ;
		else
			RES="no"
		fi
	fi
	
	echo Checking for uic    ........    $RES , $QT_UIC_EXEC 
	
	if test "$RES" = "no" ;
	then
		echo
		echo "__ I can't locate uic."
		exit
	fi
	
	if test -e $QTDIR/bin/moc ;
	then
		QT_MOC_EXEC="$QTDIR/bin/moc" ;
		RES="yes"
	else
		find /usr -name moc 1> temp ;
		QT_MOC_EXEC=$(cat temp) ;
		if [ "$QT_MOC_EXEC" != "" ]
		then
			RES="yes"
			echo
			echo "__ moc not in the expected path!"
			echo
			
			CRITICAL="1" ;
		else
			RES="no"
		fi
	fi
	
	echo Checking for moc    ........    $RES , $QT_MOC_EXEC
	
	if test "$RES" = "no" ;
	then
		echo
		echo "__ I can't locate moc."
		exit
	fi

fi # end if !no_qt




if [ "$CRITICAL" = "1" ] ;
then 
	echo
	echo "__ Critical exceptions occurred."
	echo "__ Check errors above if something goes wrong..."
	echo 
fi

#cleanup
rm -f temp ;


echo "-------------------------------------------"
echo    Now going with qmake...
echo "-------------------------------------------"

$QT_QMAKE_EXEC $PROJECT_FILE

