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

# 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


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

all:
EOF

if "$WHICH" qmake-qt4 >/dev/null 2>&1 ; then
    cat >> "$makefile" <<EOF
	cd ./src && qmake-qt4 -makefile -o Makefile 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 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 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 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
	make -C \$(DOCUMENTATION)/user_manual cleanall
	make -C \$(DOCUMENTATION)/technical_manual cleanall
	@(cd bin/ && \$(RM) vite)
	@(cd \$(DOCUMENTATION) && rm -rf html/ latex/)

distclean:	cleanall
	\$(RM) Makefile
	\$(RM) src/Makefile

#To remember how to create automaticaly a src.pro
#build:
#	cd ./src && qmake-qt4 -project "RESOURCES= vite.qrc" "OBJECTS_DIR=../bin" "DESTDIR=../bin" "CONFIG+=uitools" "QT+=opengl" "LIBS += -lglut" && qmake-qt4 -makefile -o Makefile  src.pro && make
#	@echo " "
#	@echo "Built and compiled! (release)"
#	@echo " "
EOF



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

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