#!/bin/sh
#

show_usage() {
cat <<EOT
Usage: $0 [OPTION]...

This script creates necessary configuration files to build/install.

Main options:
  --prefix=[path]     Base path for build/install.  Default: /usr/local
  --bindir=[path]     Directory for binaries.  Default: PREFIX/bin
  --datadir=[path]    Directory for data.  Default: PREFIX/share/qtoctave
  --qtdir=[path]      Directory where Qt is installed.
  --octavehelp=[path] octave.html help file. Default: DATADIR/octave_doc/octave.html
  --assistant         Install using assistant.
  --verbose           Show extra configure output.
  --help              This help text.

EOT
}

# which/make detection adapted from Qt
which_command() {
	OLD_HOME=$HOME
	HOME=/dev/null
	export HOME

	WHICH=`which which 2>/dev/null`
	if echo $WHICH | grep 'shell built-in command' >/dev/null 2>&1; then
		WHICH=which
	elif [ -z "$WHICH" ]; then
		if which which >/dev/null 2>&1; then
			WHICH=which
		else
			for a in /usr/ucb /usr/bin /bin /usr/local/bin; do
				if [ -x $a/which ]; then
					WHICH=$a/which
					break;
				fi
			done
		fi
	fi

	if [ -z "$WHICH" ]; then
		OLD_IFS=$IFS
		IFS=:
		for a in $PATH; do
			if [ -x $a/$1 ]; then
				echo "$a/$1"
				IFS=$OLD_IFS
				export IFS
				HOME=$OLD_HOME
				export HOME
				return 0
			fi
		done
		IFS=$OLD_IFS
		export IFS
	else
		a=`"$WHICH" "$1" 2>/dev/null`
		if [ ! -z "$a" -a -x "$a" ]; then
			echo "$a"
			HOME=$OLD_HOME
			export HOME
			return 0
		fi
	fi
	HOME=$OLD_HOME
	export HOME
	return 1
}
WHICH=which_command

# find a make command
if [ -z "$MAKE" ]; then
	MAKE=
	for mk in gmake make; do
		if $WHICH $mk >/dev/null 2>&1; then
			MAKE=`$WHICH $mk`
			break
		fi
	done
	if [ -z "$MAKE" ]; then
		echo "You don't seem to have 'make' or 'gmake' in your PATH."
		echo "Cannot proceed."
		exit 1
	fi
fi

show_qt_info() {
	printf "Be sure you have a proper Qt 4 build environment set up.  This means not\n"
	printf "just Qt, but also a C++ compiler, a make tool, and any other packages\n"
	printf "necessary for compiling C++ programs.\n"
	printf "\n"
	printf "If you are certain everything is installed, then it could be that Qt 4 is not\n"
	printf "being recognized or that a different version of Qt is being detected by\n"
	printf "mistake (for example, this could happen if \$QTDIR is pointing to a Qt 3\n"
	printf "installation).  At least one of the following conditions must be satisfied:\n"
	printf "\n"
	printf " 1) --qtdir is set to the location of Qt\n"
	printf " 2) \$QTDIR is set to the location of Qt\n"
	printf " 3) QtCore is in the pkg-config database\n"
	printf " 4) qmake is in the \$PATH\n"
	printf "\n"
	printf "This script will use the first one it finds to be true, checked in the above\n"
	printf "order.  #3 and #4 are the recommended options.  #1 and #2 are mainly for\n"
	printf "overriding the system configuration.\n"
	printf "\n"
}

while [ $# -gt 0 ]; do
	optarg=`expr "x$1" : 'x[^=]*=\(.*\)'`
	case "$1" in
		--prefix=*)
			PREFIX=$optarg
			shift
			;;

		--bindir=*)
			BINDIR=$optarg
			shift
			;;

		--datadir=*)
			DATADIR=$optarg
			shift
			;;

		--qtdir=*)
			EX_QTDIR=$optarg
			shift
			;;
		
		--assistant)
			ASSISTANT="-graph"
			shift
			;;

		--octavehelp=*)
			OCTAVEHELP=$optarg
			shift
			;;
		--verbose)
			QC_DEBUG="Y"
			shift
			;;
		--help) show_usage; exit ;;
		*) show_usage; exit ;;
	esac
done

PREFIX=${PREFIX:-/usr/local}
BINDIR=${BINDIR:-$PREFIX/bin}
DATADIR=${DATADIR:-$PREFIX/share/qtoctave}
OCTAVEHELP=${OCTAVEHELP:-$DATADIR/octave_doc/octave.html}

echo "Configuring QtOctave Tools ..."

if [ "$QC_DEBUG" = "Y" ]; then
echo
echo PREFIX=$PREFIX
echo BINDIR=$BINDIR
echo DATADIR=$DATADIR
echo EX_QTDIR=$EX_QTDIR
echo
fi

echo "Verifying Qt 4 build environment ... "

if [ "$QC_DEBUG" = "Y" ]; then
	echo
fi

qm=""



# qt4 check: --qtdir
if [ -z "$qm" ]; then
	qstr=$EX_QTDIR/bin/qmake
	if [ -x "$qstr" ]; then
		qm=$qstr
	fi
fi
if [ -z "$qm" ] && [ "$QC_DEBUG" = "Y" ]; then
	echo "Warning: qmake not found via --qtdir"
fi

# qt4 check: QTDIR
if [ -z "$qm" ]; then
	qstr=$QTDIR/bin/qmake
	if [ -x "$qstr" ]; then
		qm=$qstr
	fi
fi
if [ -z "$qm" ] && [ "$QC_DEBUG" = "Y" ]; then
	echo "Warning: qmake not found via \$QTDIR"
fi

# qt4 check: pkg-config
if [ -z "$qm" ]; then
	str=`pkg-config QtCore --variable=exec_prefix 2>/dev/null`
	if [ ! -z "$str" ]; then
		qstr=$str/bin/qmake
		if [ -x "$qstr" ]; then
			qm=$qstr
		fi
	fi
fi
if [ -z "$qm" ] && [ "$QC_DEBUG" = "Y" ]; then
	echo "Warning: qmake not found via pkg-config"
fi

# qt4 check: PATH
if [ -z "$qm" ]; then
	qstr=`$WHICH qmake 2>/dev/null`
	if [ -x "$qstr" ]; then
		qm=$qstr
	fi
fi
if [ -z "$qm" ] && [ "$QC_DEBUG" = "Y" ]; then
	echo "Warning: qmake not found via \$PATH"
fi

if [ -z "$qm" ]; then
	if [ "$QC_DEBUG" = "Y" ]; then
		echo " -> fail"
	else
		echo "fail"
	fi
	printf "\n"
	printf "Reason: Unable to find the 'qmake' tool.\n"
	printf "\n"
	show_qt_info
	exit 1;
fi
if [ "$QC_DEBUG" = "Y" ]; then
	echo qmake found in $qm
fi

gen_files() {
cat >$1/modules.cpp <<EOT
#line 1 "qtoctavedep.qcm"
/*
-----BEGIN QCMOD-----
name: QtOctave install path
-----END QCMOD-----
*/

class qc_qtoctavedep : public ConfObj
{
	public:
	qc_qtoctavedep(Conf *c) : ConfObj(c) {}
	QString name() const { return "QtOctave install path"; }
	QString shortname() const { return "qtoctavedep"; }
	bool exec()
	{
		QString slb="\"";
		QString _prefix=conf->getenv("PREFIX");
		conf->addDefine("CONFIG_PATH=\""+slb+slb+"\""+
			_prefix+slb+slb+"\"\"");
		conf->addDefine("ICON_PATH=\""+slb+slb+"\""+
			_prefix+slb+slb+"\"\"");
		conf->addDefine("QTOCTAVE_HELP_PATH=\""+slb+slb+"\""+
			_prefix+"/qtoctave_doc/index.html"+slb+slb+"\"\"");
		return true;
	}
};
EOT
}

#Builds install-tool
export PREFIX
export BINDIR
export DATADIR
export OCTAVEHELP
export EX_QTDIR
export QC_DEBUG

cd install-tool
(
	$qm -project
	$qm
	$MAKE
)

if [ "$?" != "0" ]; then
	rm -rf .qconftemp
	if [ "$QC_DEBUG" = "Y" ]; then
		echo " -> fail"
	else
		echo "fail"
	fi
	printf "\n"
	printf "Reason: There was an error compiling 'conf'.  See conf.log for details.\n"
	printf "\n"
	show_qt_info
	if [ "$QC_DEBUG" = "Y" ]; then
		echo "conf.log:"
		cat conf.log
	fi
	exit 1;
fi

cd ..

#Export variables for install-tool

QC_COMMAND=$0
export QC_COMMAND
QMAKE=$qm
export QMAKE
MAKETOOL=$MAKE
export MAKETOOL


install-tool/install-tool $ASSISTANT
ret="$?"
if [ "$ret" = "1" ]; then
	echo
	exit 1;
else
	if [ "$ret" != "0" ]; then
		if [ "$QC_DEBUG" = "Y" ]; then
			echo " -> fail"
		else
			echo "fail"
		fi
		echo
		echo "Reason: Unexpected error launching 'conf'"
		echo
		exit 1;
	fi
fi


echo
echo "Good, your configure finished.  Now run $MAKE."
echo
