#
# This file is part of the ViTE project.
#
# This software is governed by the CeCILL-A license under French law
# and abiding by the rules of distribution of free software. You can
# use, modify and/or redistribute the software under the terms of the
# CeCILL-A license as circulated by CEA, CNRS and INRIA at the following
# URL: "http://www.cecill.info".
# 
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided
# only with a limited warranty and the software's author, the holder of
# the economic rights, and the successive licensors have only limited
# liability.
# 
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards
# their requirements in conditions enabling the security of their
# systems and/or data to be ensured and, more generally, to use and
# operate it in the same conditions as regards security.
# 
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL-A license and that you accept its terms.
#
#
# ViTE developers are (for version 0.* to 1.0):
#
#        - COULOMB Kevin
#        - FAVERGE Mathieu
#        - JAZEIX Johnny
#        - LAGRASSE Olivier
#        - MARCOUEILLE Jule
#        - NOISETTE Pascal
#        - REDONDY Arthur
#        - VUCHENER Clément 
#
#
#!/bin/sh

echo $* >> ./config.log

relpath=`dirname $0`
relpath=`(cd "$relpath"; /bin/pwd)`
WHICH=`which which 2>/dev/null`
UNAME_SYSTEM=`(uname -s) 2>/dev/null`  || UNAME_SYSTEM=unknown
makefile="Makefile"

#
# External libraries default values 
#

# OTF
with_otf=1
otfdir="$PWD/externals/otf"
otfincdir="$otfdir/otflib/"
otflibdir="$otfdir/otflib/.libs"
otfmake="
otf : $otflibdir/libotf.a
	( cd $otfdir && ./configure && make )

otfclean :
	( cd $otfdir && make clean )
"
otfnotinc="
otf :
otfclean :
"

# TAU
with_tau=1
taudir="$PWD/externals/tau"
tauincdir="$taudir/include"
taulibdir="$taudir/src/TraceInput"
taumake="
tau :
	( cd $taulibdir && make )

tauclean :
	( cd $taulibdir && make clean )
"
taunotinc="
tau :
tauclean :
"

# QTColor
qtcolordir="$PWD/externals/qtcolorpicker"
qtcolorincdir="$qtcolordir/src"
qtcolorsrcdir="$qtcolordir/src"

config_specified=0

# Project file
project="src.pro"

# For qmake
config="\"CONFIG+="
defines="\"DEFINES+="
libs="\"LIBS+="
includepath="\"INCLUDEPATH+="
dependpath="\"DEPENDPATH+="
sources="\"SOURCES+="
headers="\"HEADERS+="

echo ""
echo "-------------------------------"
echo "-------------------------------"
echo ""
echo "     Visual Trace Explorer"
echo "       - configuration -"
echo ""
echo ""
echo " type './configure -h' for help "
echo ""
echo ""
echo " Default mode is:"
echo "      * RELEASE mode"
echo ""
echo "-------------------------------"
echo "-------------------------------"
echo ""
echo ""

usage()
{
  echo "ViTE configuration shell-script"
  echo "Usage:"
  echo " ./configure [ options ]"
  echo
  echo "Options:"
  echo "  -h, --help"
  echo "        print this page"
  echo ""
  echo "  --release"
  echo "        compiles in release mode (by default)"
  echo ""
  echo "  --debug"
  echo "        compiles in debug mode"
  echo ""
  echo "  --enable_spinning_logo"
  echo "        makes the rabbit to turn when no trace is loaded"
  echo ""
  echo "  --disable_otf"
  echo "        disables the otf support" 
  echo ""
  echo "  --otf_dir=<path>"
  echo "        path to the OTF library (An OTF release is provided in the externals/otf directory)"
  echo ""
  echo "  --otf_incdir=<path>"
  echo "        include path to the OTF headers"
  echo ""
  echo "  --otf_libdir=<path>"
  echo "        library path to the OTF libraries"
  echo ""
  echo "  --disable_tau"
  echo "        disables the tau support"
  echo ""
  echo "  --tau_dir=<path>"
  echo "        path to the TAU library"
  echo ""
  echo "  --tau_incdir=<path>"
  echo "        include path to the TAU headers"
  echo ""
  echo "  --tau_libdir=<path>"
  echo "        library path to the TAU libraries"
  echo ""
}


while [ 0 -lt $# ]; do
    OPT=$1
    shift 1
    case $OPT in
        --help|-h)  # help
            usage
            exit 0
            ;;
        --enable_spinning_logo) # good openGL spinning rabbit :)
            defines="$defines SPINNING_LOGO"
            ;;
        --release)
            config_specified=1;
            config="$config release"
            ;;
        --debug)
            config_specified=1;
            config="$config debug"
            ;;
        --disable_otf) # otf support
            with_otf=0;
            ;;
        --otf_dir=*) # otf directory
            with_otf=2;
            otfdir=`echo $OPT | awk -F"--otf_dir=" '{print $2}'`
            otfincdir="$otfdir/include"
            otflibdir="$otfdir/lib"
            ;;
        --otf_incdir=*) # otf include directory
            with_otf=2;
            otfincdir=`echo $OPT | awk -F"--otf_incdir=" '{print $2}'`
            ;;
        --otf_libdir=*)
            with_otf=2;
            otflibdir=`echo $OPT | awk -F"--otf_libdir=" '{print $2}'`
            ;;
        --disable_tau) # tau support
            with_tau=0;
            ;;
        --tau_dir=*) # tau directory
            taudir=`echo $OPT | awk -F"--tau_dir=" '{print $2}'`
            tauincdir="$taudir/include"
            taulibdir="$taudir/lib"
            ;;
        --tau_incdir=*) # tau include directory
            tauincdir=`echo $OPT | awk -F"--tau_incdir=" '{print $2}'`
            ;;
        --tau_libdir=*)
            taulibdir=`echo $OPT | awk -F"--tau_libdir=" '{print $2}'`
            ;;
        # Internal options (Don't appear in --help)
        --qtcolor_dir=*) # qtcol directory
            qtcolordir=`echo $OPT | awk -F"--qtcolor_dir=" '{print $2}'`
            qtcolorincdir="$qtcolordir/include"
            qtcolorlibdir="$qtcolordir/lib"
            ;;
        --qtcolor_incdir=*) # qtcolor include directory
            qtcolorincdir=`echo $OPT | awk -F"--qtcolor_incdir=" '{print $2}'`
            ;;
        --qtcolor_libdir=*)
            qtcolorlibdir=`echo $OPT | awk -F"--qtcolor_libdir=" '{print $2}'`
            ;;
        *)
            usage
            echo "";
            echo "invalid option : $OPT";
            echo "";
            echo "exit";
            echo "";
            exit 0; 
    esac

done

#end the qmake options
if [ $config_specified -eq 1 ]; then 
    config="$config\"";
else
    config="$config release\"";
fi

#
# External libraries
#

# OTF
if [ $with_otf -ge 1 ]; then
    if [ $with_otf = 2 ] && [ ! -f "$otflibdir/libotf.a" ]; then
	echo "ERROR: OTF library not found in $otflibdir"
	echo "Please specify the correct directory with --otf_dir=dir or use the one provided by ViTE"
	exit;
    else
	defines="$defines WITH_OTF"
	sources="$sources parser/ParserOTF.cpp parser/ParserEventOTF.cpp parser/ParserDefinitionOTF.cpp"
	headers="$headers parser/ParserOTF.hpp parser/ParserEventOTF.hpp parser/ParserDefinitionOTF.hpp"
	includepath="$includepath $otfincdir"
	libs="$libs -L$otflibdir $otflibdir/libotf.a"
    fi
fi

# TAU
if [ $with_tau -ge 1 ]; then
    if [ $with_tau = 2 ] && [ ! -f "$taulibdir/libTAU_tf.a" ]; then
	echo "ERROR: TAU library not found in $taulibdir"
	echo "Please specify the correct directory with --tau_dir=dir or use the one provided by ViTE"
	exit;
    else
	defines="$defines WITH_TAU"
	sources="$sources parser/ParserTau.cpp"
	headers="$headers parser/ParserTau.hpp parser/TauStructs.hpp"
	includepath="$includepath $tauincdir"
	if [ $with_tau -eq 1 ]; then
	    libs="$libs -L$taulibdir -lTAU_traceinput"
	else
            libs="$libs -L$taulibdir -lTAU_tf"
	fi
    fi
fi

# QT Color Picker
dependpath="$dependpath $qtcolorsrcdir"
includepath="$includepath $qtcolorincdir"

#
# End External libraries
#
defines="$defines\"";
libs="$libs\"";
includepath="$includepath\"";
dependpath="$dependpath\"";
sources="$sources\"";
headers="$headers\"";

options="$defines $config $libs $sources $headers $includepath $dependpath";

# 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 >&2 "You don't seem to have 'make' or 'gmake' in your PATH."
        echo >&2 "Cannot proceed."
        exit 1
    fi
fi


makecmd=$MAKE
if "$WHICH" qmake-qt4 >/dev/null 2>&1 ; then
    qmakecmd="qmake-qt4 -makefile -o Makefile"
elif "$WHICH" qmake >/dev/null 2>&1 && [ "$UNAME_SYSTEM" = "Darwin" ] ; then # Mac
    qmakecmd="qmake -spec macx-xcode "
    makecmd="xcodebuild -project vite.xcodeproj -configuration Debug clean build"
elif "$WHICH" qmake >/dev/null 2>&1 ; then
    qmakecmd="qmake -makefile -o Makefile"
elif "$WHICH" qmake-mac >/dev/null 2>&1 ; then # Mac the return
    qmakecmd="qmake-mac -makefile -o Makefile"
else # error
    echo >&2 "Warning: Impossible to find qmake or qmake-qt4."
    echo >&2 "Be sure that you have installed Qt and qmake (if it is not included in the Qt package)"
    rm -f Makefile
    exit
fi

# Creates the makefile.in
cat > "Makefile.in" <<EOF
VITEDIR  = $relpath
DOCSDIR  = ./docs
QMAKECMD = $qmakecmd
MAKECMD  = $makecmd
EOF

# Creates the makefile
cat > "$makefile" <<EOF
########################################################################
##                                                                    ##
## This file was autogenerated by configure, all changes will be lost ##
##                                                                    ##
################### It has by done for ViTE project ####################
##                                                                    ##
########################################################################

include Makefile.in

all: otf tau
	( cd src && \$(QMAKECMD) $project $options)
	( cd src && \$(MAKECMD)  )
	@echo " "
	@echo "Compilation completed! File created in 'bin' folder."
	@echo " "

doc:
	doxygen Doxyfile
	$MAKE -C \${DOCSDIR}/latex
	$MAKE -C \${DOCSDIR}/user_manual
	$MAKE -C \${DOCSDIR}/technical_manual
	@echo " "
	@echo "Documentation generated successfully!"
	@echo "It could be find in the \${DOCSDIR} folder."
	@echo " "

plugins: 
	( cd plugins && VITEDIR=\$(VITEDIR) $MAKE )

clean:
	( cd plugins && VITEDIR=\$(VITEDIR) $MAKE clean )
	( cd src     && $MAKE clean )
	\$(RM) log.txt
	@echo " "
	@echo "Cleaned!"
	@echo " "

cleanall: clean 
	( cd plugins && VITEDIR=\$(VITEDIR) $MAKE cleanall )
	$MAKE -C \${DOCSDIR}/user_manual cleanall
	$MAKE -C \${DOCSDIR}/technical_manual cleanall
	@( cd \${DOCSDIR} && rm -rf html/ latex/)
	@( cd bin/ && \$(RM) vite)

distclean: cleantau cleanotf cleanall
	( cd plugins && VITEDIR=\$(VITEDIR) $MAKE distclean )
	\$(RM) Makefile
	\$(RM) src/Makefile

.PHONY: plugins clean cleanall distclean

EOF

if [ $with_otf -eq 1 ]; then
    cat >> "$makefile" <<EOF
otf : $otflibdir/libotf.a
$otflibdir/libotf.a:
	( cd $otfdir && ./configure && make )

cleanotf :
	( cd $otfdir && make clean )

EOF
else
    cat >> "$makefile" <<EOF
otf :
cleanotf :
EOF
fi

if [ $with_tau -eq 1 ]; then
    cat >> "$makefile" <<EOF
tau : $taulibdir/libTAU_traceinput.a
$taulibdir/libTAU_traceinput.a:
	( cd $taulibdir && make )

cleantau :
	( cd $taulibdir && make clean )
EOF
else
    cat >> "$makefile" <<EOF
tau :
cleantau :
EOF
fi

# basename erase the path of acces of the command 
MAKE=`basename $MAKE`

if [ $with_otf -ge 1 ]; then
    echo OTF support [OK]
else 
    echo OTF support [DISABLE]
fi

if [ $with_tau -ge 1 ]; then 
    echo TAU support [OK]
else
    echo TAU spport [DISABLE]
fi

echo ViTE is now configured for building. Just run \'$MAKE\'.
