#!/bin/bash
# This code is part of  CMPL  
#
# Copyright (C) 2007 - 2016 Thomas Schleiff - Halle(Saale), 
# Germany and Mike Steglich - Technical University of Applied Sciences
# Wildau, Germany 
#
# CMPL is a project of the Technical University of 
# Applied Sciences Wildau and the Institute for Operations Research 
# and Business Management at the Martin Luther University 
# Halle-Wittenberg.
# Please visit the project homepage <www.coliop.org>
# 
# CMPL is free software; you can redistribute it and/or modify it 
# under the terms of the GNU General Public License as published by 
# the Free Software Foundation; either version 3 of the License, or 
# (at your option) any later version.
# 
# CMPL is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
# License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
# @author  Mike Steglich - April 2016

QtPath=/usr/local/qt5/bin/
export QTLibs=/usr/local/qt5/lib

PATH=$QtPath:$PATH

# usage cmplMake
function usage () 
{
	if [ $oSystem == "win" ]; then 
		echo "Usage: make-cmpl [ test | clean | package ]"
	else
		echo "Usage: make-cmpl [ test | clean | package | getExternals ]"
	fi
	
	echo "Usage: make-cmpl [ test | clean | package |  ]"
	
    echo " "
    echo "test                          - test routine "
    echo "clean                         - removes the obj and moc files in folder obj/"
    echo "package                       - creates a distribution package in folder Package/[linux/osx/win]"
    if [ $oSystem != "win" ]; then 
    	echo "getExternals                  - downloads and builds OS,GLPK and Coliop"
    fi
    exit 1
}


# test for qMake, GCC 
isQmake=1
type -p qmake &>/dev/null || isQmake=0
isGcc=1
type -p g++ &>/dev/null || isGcc=0

if [ $isQmake == 0 ]; then
	echo "cmplMake error: qMake not found"
	exit 1
fi

if [ $isGcc == 0 ]; then
	echo "cmplMake error: g++ not found"
	exit 1
fi



oSystem="";
if [[ $OSTYPE == *darwin* ]] ; then 
	oSystem="osx"
elif  [[ $OSTYPE == *linux* ]] ; then
	if [[ $MACHTYPE =~ .*arm.* ]] ; then
		oSystem="raspbian"
	else 
		oSystem="linux"
	fi
elif  [[ $OSTYPE == *msys* ]] ; then
	oSystem="win"
fi

if [ $oSystem == "" ] ; then
	echo "cmplMake error: Can not recognize your OS"
	exit 1
fi

if [ $# -gt 1 ] ; then
    usage
    exit 1
fi

if [ $# == 0 ] ; then 
	cd Cmpl 
	./cmplMake
	cd ..
	if [ $oSystem != "raspbian" ]; then 
   		cd Coliop4
	    ./cmplMake
    	cd ..
	fi
fi

if [ $# == 1 ] ; then
	cd Cmpl
	./cmplMake $1
	cd ..
        #cd Coliop4
        #./cmplMake $1
        #cd ..
	if [ $1 == "package" ]; then
		if [ -d Cmpl/Package ] ; then 
			if [ -d Package ] ; then 
				rm -R Package
			fi
			mv Cmpl/Package .
		fi
		
		cp README Package/$oSystem/Cmpl
		cp AUTHORS Package/$oSystem/Cmpl
		
		if [ $oSystem != "win" ]; then 
			rm pyCmpl/pyCmpl/*.pyc &>/dev/null
		fi


		cp -R pyCmpl Package/$oSystem/Cmpl
		cp -R cmplServer Package/$oSystem/Cmpl
		rm -R Package/$oSystem/Cmpl/pyCmpl/test
		
		if [ -d Cmpl/Thirdparty/pypy ] ; then 
			cp -R Cmpl/Thirdparty/pypy Package/$oSystem/Cmpl/
		fi
		
		if [ -d jCmpl/dist ] ; then 
			mkdir Package/$oSystem/Cmpl/jCmpl
			cp -R jCmpl/dist/* Package/$oSystem/Cmpl/jCmpl
			rm Package/$oSystem/Cmpl/jCmpl/README.TXT
			cp jCmpl/README Package/$oSystem/Cmpl/jCmpl
			cp jCmpl/AUTHORS Package/$oSystem/Cmpl/jCmpl
			cp jCmpl/LICENSE Package/$oSystem/Cmpl/jCmpl
			cp jCmpl/Libs/LICENSE Package/$oSystem/Cmpl/jCmpl/lib/
			cp jCmpl/Libs/NOTICE Package/$oSystem/Cmpl/jCmpl/lib/
		else 
			echo "cmplMake warning: Please build the jCmpl before installing"
		fi
		
	elif [ $1 == "clean" ]; then
		rm pyCmpl/pyCmpl/*.pyc
	elif [ $1 == "test" ]; then
	
		cd pyCmpl/test
		./test-all
		cd ../..
		
		if [ -f jCmpl/test/jCmplTest/dist/jCmplTest.jar ] ; then
			cd jCmpl/test
			./test-all
			cd ../..
		else
			echo "cmplMake warning: Can't test jCmpl because jCmplTest.jar not found"
		fi
		
	fi
fi

exit 0
