#!/bin/sh

# parsing options

TEMP=`getopt -o p:b:s:d:q::h --long prefix:,bindir:,sbindir:,datadir:,qtdir:\
,mkntfs:\
,ntfsresize:\
,mkfs.ext3:\
,mkfs.jfs:\
,help \
-n 'configure' -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true; do
	case "$1" in
		-p|--prefix) PREFIX=$2; shift 2 ;;
		-b|--bindir) BINDIR=$2; shift 2 ;;
		-s|--sbindir) SBINDIR=$2; shift 2 ;;
		-d|--datadir) DATADIR=$2; shift 2 ;;
		-q|--qtdir) QTDIR=$2; shift 2 ;;
		--mkntfs) MKNTFSPATH=$2; shift 2 ;;
		--ntfsresize) NTFSRESIZEPATH=$2; shift 2 ;;
		--mkfs.ext3) MKFSEXT3PATH=$2; shift 2 ;;
		--mkfs.jfs) MKFSJFSPATH=$2; shift 2 ;;
		-h|--help) cat <<EOT
\`configure' configures this package to adapt to many kinds of systems.

Usage: ./configure [OPTION]...

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
  -V, --version           display version information and exit

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --qtdir=QTDIR           where the root of Qt is installed [autodetect]
  --bindir=DIR            user executables [PREFIX/bin]
  --sbindir=DIR           system admin executables [PREFIX/sbin]  
  --datadir=DIR           read-only architecture-independent data [PREFIX/share]

External Tools:
  --mkntfs=PATH           path of mkntfs [/usr/sbin/mkntfs]
  --ntfsresize=PATH       path of ntfsresize [/usr/sbin/ntfsresize]
  --mkfs.ext3=PATH        path of mkfs.ext3 [/sbin/mkfs.ext3]
  --mkfs.jfs=PATH         path of mkfs.jfs [/sbin/mkfs.jfs]
EOT
			exit
			;;
		--) shift ; break ;;
		*) echo "unknown parameter $1";;
	esac
done

echo Configuring QTParted ...

PREFIX=${PREFIX:-/usr/local}
BINDIR=${BINDIR:-$PREFIX/bin}
SBINDIR=${SBINDIR:-$PREFIX/sbin}
DATADIR=${DATADIR:-$PREFIX/share}

MKNTFSPATH=${MKNTFSPATH:-/sbin/mkntfs}
NTFSRESIZEPATH=${NTFSRESIZEPATH:-/usr/sbin/ntfsresize}
MKFSEXT3PATH=${MKFSEXT3PATH:-/usr/sbin/mkfs.ext3}
MKFSJFSPATH=${MKFSJFSPATH:-/sbin/mkfs.jfs}
QTPARTEDDFPATH=${SBINDIR/qtparted_df}

if [ -z "$QTDIR" ]; then
	echo 
	echo \$QTDIR not set... trying to find qt manually
	qm=`type -p qmake`
	if [ -x "$qm" ]; then
		echo qmake found in $qm
	else 
		echo
		echo Please install Qt 3 development utilities. You may
		echo download them either from the vendor of your operating
		echo system or from www.trolltech.com
		exit 1;
	fi
	for p in /usr/lib/qt /usr/share/qt /usr/share/qt3 /usr/local/lib/qt /usr/local/share/qt /usr/lib/qt3 /usr/local/lib/qt3 ; do
		if [ -e "$p/mkspecs/linux-g++/qmake.conf" ]; then
			echo qmake.conf found in $p/mkspecs/linux-g++/qmake.conf
			QTDIR=$p
			break;
		fi;
	done
	if [ -z "$QTDIR" ]; then
		echo Unable to find qmake.conf. Please set QTDIR
		echo manually. Perhaps you need to install Qt 3
		echo development utilities. You may download them either
		echo from the vendor of your operating system or from
		echo www.trolltech.com
		exit 1;
	fi
else
	if [ ! -x "$QTDIR/bin/qmake" ]; then
		echo \$QTDIR points to $QTDIR, but $QTDIR/bin/qmake
		echo does not exist! Please point \$QTDIR to the correct
		echo location, or unset it if qmake is in your search path.
		exit 1;
	else
		qm=$QTDIR/bin/qmake
	fi
fi

if [ -z "$QMAKESPEC" ]; then
	echo 
	echo \$QMAKESPEC not set... i trying to use linux g++
    QMAKESPEC=linux-g++
fi

# create Makefile, and src/config.h

cat >Makefile <<EOT
exec_prefix = \${prefix}
datadir = \${exec_prefix}$DATADIR
bindir = \${exec_prefix}$BINDIR
sbindir = \${exec_prefix}$SBINDIR

all:
	QTDIR=$QTDIR make -C src
	cd src; lrelease qtparted.pro

install:
	mkdir -p \$(datadir)/qtparted/locale
	mkdir -p \$(datadir)/pixmaps
	mkdir -p \$(datadir)/applnk/System
	cp -a src/qtparted \$(sbindir)
	cp -a bin/qtparted_df \$(sbindir)
	cp -a src/ts/*qm \$(datadir)/qtparted/locale
	cp -a image/qtparted.xpm \$(datadir)/pixmaps
	cp -a menu/qtparted.desktop \$(datadir)/applnk/System

clean:
	QTDIR=$QTDIR make -C src clean
	rm -rf src/ts/*.qm
	rm -rf src/qtparted
	rm -rf src/Makefile
	rm -rf src/config.h
	rm -rf Makefile

distclean: clean
	-rm Makefile
	-rm src/config.h

EOT

cat >src/config.h <<EOT
#define DATADIR "$DATADIR/qtparted/locale"


#define QTPARTED_DF "qtparted_df"
#define QTPARTED_DF_PATH "$SBINDIR/qtparted_df"

#define MKNTFS "mkntfs"
#define MKNTFS_PATH "$MKNTFSPATH"

#define NTFSRESIZE "ntfsresize"
#define NTFSRESIZE_PATH "$NTFSRESIZEPATH"

#define MKFS_EXT3 "mkfs.ext3"
#define MKFS_EXT3_PATH "$MKFSEXT3PATH"

#define MKFS_JFS "mkfs.jfs"
#define MKFS_JFS_PATH "$MKFSJFSPATH"
EOT

# run qmake
( 
	cd src
	$qm qtparted.pro
)
# here we are back from src/

echo Done!
