#!/bin/bash


if( ! pkg-config --exists xtst ); then
    echo "Error: you will need libxtst to compile this program";
    exit 1;
fi;

if( ! pkg-config --atleast-version=4.2 QtCore ); then
    echo "Error: you need at least Qt version 4.2 to use this program";
    exit 1;
fi;

if ( ! qmake ); then
    echo "Error: you need qmake!";
    exit 1;
fi;

if( ! ( qmake --version | grep -qi 'Qt version 4' ) ); then
    echo "WARNING: Falling back on qmake-qt4; this may or may not work";
    echo "WARNING: if it fails please see the compilation instructions on 
    qjoypad.sourceforge.net";
    QMAKE_EXEC="qmake-qt4";
else
    QMAKE_EXEC="qmake";
fi;
    

devdir="/dev/input"
prefix="/usr/local"
installdir="";
plain_keys=""
debug_option="";
build_mode="release";
for arg in $*
do
	case $arg in
		--help)	echo "
Usage: ./config [--devdir=\"dir\"] [--prefix=\"prefix\"] [--help]

Options:
     --devdir=DIR   Set the path where QJoyPad will look for your joystick
                    devices to be DIR. If your devices are /dev/js0, /dev/js1,
                    etc., this should be just \"/dev\". By default, this is
                    /dev/input.
 
     --prefix=DIR   Set the path where QJoyPad and its components will be
                    installed. By default, this is /usr/local.
 
     --plain_keys   Force QJoyPad to use standard XWindows keynames without
                    filtering them for appearance. This will make displays
                    less attractive and readable, but will save processor power
                    and ensure that you see the right names for keys you press.
                    
     --install-dir=DIR  Sets an optional installation path that will be prepended
                        to prefix during installation.
     --debug        Causes Qjoypad to spit out debugging messages (debug messages
                    will be  compiled in; to get rid of the messages you'll have
                    to recompile).
    --qmake4bin=EXEC   Overrides the qmake executable used by this script.
	EXEC may be relative to PATH or an absolute pathname.
    --help          Show this message.
"; exit 0;;
        --devdir=*) devdir=${arg##*=}
        	if [[ ! -d $devdir ]]
            then
            	echo "Invalid device directory given: $devdir"
                exit 1
            fi ;;
        --prefix=*) prefix=${arg##*=}
            if [[ ! -d $prefix ]]
            then
            	echo "Invalid prefix directory given: $prefix"
                exit 1
            fi;;
        --install-dir=*) installdir=${arg##*=}
            if [[ ! -d $installdir ]]
            then
                echo "invalid destination directory given: $installdir";
                exit 1
            fi;;
		--plain_keys) plain_keys="PLAIN_KEYS";;
        --debug)
            debug_option+="_DEBUG";
            build_mode="debug";;
	--qmake4bin=*) QMAKE_EXEC=${arg##*=};;
        *)  echo "Unrecognized argument: \"$arg\". Try ./config --help for help."
	esac
done

if ( ! $QMAKE_EXEC -makefile DEVDIR=$devdir PREFIX=$prefix \
    "DEFINES += $plain_keys $debug_option" \
    INSTALL_PREFIX=${installdir}/${prefix}/ \
    "CONFIG += $build_mode" \
    qjoypad.pro ); then
	echo "Config failed.  If you overrode the qmake exec, make sure it
is valid!  Otherwise, email virtuoussin13@users.sourceforge.net for help";
exit 1;
fi

echo "
Configuring QJoyPad installation...
------------------------------------------------------------

Device directory: $devdir
-- Devices will be looked for in:
     $devdir/js0
     $devdir/js1
     etc.

Prefix directory: $prefix
-- Files to be installed in:
     $prefix/bin
     $prefix/doc
     $prefix/share/pixmaps"

if [[ -n $plain_keys ]]; then
	echo "
-- Using regular XWindows key names.";
fi

echo "	 
---------------------------------------------------------
If these settings are okay, go ahead and run 'make' and
then 'make install'.

To make changes, run ./config --help for details.
"
