#
# 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

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"

with_otf=0
config_specified=0

# where will we concatenate options like macros, libs...
compilation_src_pro="src.pro"

# For qmake
config="\"CONFIG+="
defines="\"DEFINES+="
libs="\"LIBS+="
includes="\"INCLUDES+="
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 "  --enable_otf"
  echo "        enables the otf support. The otf release used by default is in the src/general directory"
## Will be added later
#  echo "  --otf_include_dir=<path>    set the otf directory path"
#  echo "  --otf_library_dir=<path>    set the otf library path (<path>/libotf.a)"
  exit 0
}


while [ 0 -lt $# ]; do
    OPT=$1
    shift 1
    case $OPT in
        --help|-h)  # help
            usage
            ;;
        --enable_otf) # otf support
            with_otf=1;
            defines="$defines WITH_OTF"
            libs="$libs general/otf/otflib/.libs/libotf.a -Lgeneral/otf/otflib/.libs"
            sources="$sources parser/ParserOTF.cpp parser/ParserEventOTF.cpp parser/ParserDefinitionOTF.cpp"
            headers="$headers parser/ParserOTF.hpp parser/ParserEventOTF.hpp parser/ParserDefinitionOTF.hpp"
            includes="$includes "
            #compilation_src_pro="\"DEFINES+=WITH_OTF\" \"LIBS+=general/otf/otflib/.libs/libotf.a -Lgeneral/otf/otflib/.libs\" \"SOURCES+=parser/ParserOTF.cpp parser/ParserEventOTF.cpp parser/ParserDefinitionOTF.cpp\" \"HEADERS+=parser/ParserOTF.hpp parser/ParserEventOTF.hpp parser/ParserDefinitionOTF.hpp\" src.pro "
            ;;
        --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"
            ;;
        *)
            echo "invalid option : $OPT";
            exit 0;
    esac

done

#end the qmake options
if [ $config_specified == 1 ]; then 
    config="$config\"";
else
    config="$config release\"";
fi
defines="$defines\"";
libs="$libs\"";
includes="$includes\"";
sources="$sources\"";
headers="$headers\"";

compilation_src_pro="$defines $config $libs $includes $sources $headers";

# 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

OTFLIB_DIR=./src/general/otf

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

if [ $with_otf == 1 ]; then # set the directory... should be decided by the user, we will see after how to do beautiful options :)

cat >> "$makefile" <<EOF
OTFLIB_DIR = ./src/general/otf

all:
	make -C \$(OTFLIB_DIR)
	rm -rf \$(OTFLIB_DIR)/otflib/.libs/*so*
	rm -rf \$(OTFLIB_DIR)/otflib/.libs/*dylib* # For mac
EOF
else
cat >> "$makefile" <<EOF
    
all:
EOF

fi

if "$WHICH" qmake-qt4 >/dev/null 2>&1 ; then
    cat >> "$makefile" <<EOF
	cd ./src && qmake-qt4 -makefile -o Makefile $compilation_src_pro && make
EOF
elif "$WHICH" qmake >/dev/null 2>&1 && [ "$UNAME_SYSTEM" = "Darwin" ] ; then # Mac
    cat >> "$makefile" <<EOF
	cd src/ && qmake -spec macx-xcode $compilation_src_pro && xcodebuild -project vite.xcodeproj -configuration Debug clean build
EOF
elif "$WHICH" qmake >/dev/null 2>&1 ; then
    cat >> "$makefile" <<EOF
	cd ./src && qmake -makefile -o Makefile $compilation_src_pro && make
EOF
elif "$WHICH" qmake-mac >/dev/null 2>&1 ; then # Mac the return
    cat >> "$makefile" <<EOF
	cd ./src && qmake-mac -makefile -o Makefile $compilation_src_pro && make
EOF
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
    cat >>"$makefile" <<EOF
	@echo " "
	@echo "Compilation completed! File created in 'bin' folder."
	@echo " "

DOCUMENTATION = ../documentation/
doc:
	doxygen Doxyfile
	make -C \$(DOCUMENTATION)/latex
	make -C \$(DOCUMENTATION)/user_manual
	make -C \$(DOCUMENTATION)/technical_manual
	@echo " "
	@echo "Documentation generated successfully!"
	@echo "It could be find in the \$(DOCUMENTATION) folder."
	@echo " "

clean:
	cd ./src && make clean
	\$(RM) log.txt
	@echo " "
	@echo "Cleaned!"
	@echo " "


cleanall:	clean
EOF

if [ $with_otf == 1 ]; then
    cat >>"$makefile" <<EOF
	make -C \$(OTFLIB_DIR) clean
EOF
fi
    cat >>"$makefile" <<EOF
	make -C \$(DOCUMENTATION)/user_manual cleanall
	make -C \$(DOCUMENTATION)/technical_manual cleanall
	@(cd bin/ && \$(RM) vite)
	@(cd \$(DOCUMENTATION) && rm -rf html/ latex/)

distclean:	cleanall
EOF
if [ $with_otf == 1 ]; then
    cat >>"$makefile" <<EOF
	make -C \$(OTFLIB_DIR) distclean
EOF
fi
    cat >>"$makefile" <<EOF
	\$(RM) Makefile
	\$(RM) src/Makefile

EOF


if [ $with_otf == 1 ]; then
    echo >&2 "Configuration of the otf library"
    cd $OTFLIB_DIR && ./configure
fi
# basename erase the path of acces of the command 
MAKE=`basename $MAKE`

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