#!/bin/bash
# This code is part of CMPL
#
# Copyright (C) 2007, 2008, 2009, 2010, 2011 Thomas Schleiff - Halle(Saale), 
# Germany and Mike Steglich - Technical University of Applied Sciences
# Wildau, Germany 
#
# Coliop and CMPL are projects 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 


# test of the OS
oSystem="";
if [[ $OSTYPE == *darwin* ]] ; then 
	oSystem="osx"
	osDir="osx"
elif  [[ $OSTYPE == *linux* ]] ; then
	oSystem="linux"
elif  [[ $OSTYPE == *msys* ]] ; then
	oSystem="win"
fi

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

isCMPL=0
if [ $oSystem == "osx" ] ; then
	if test -d bin/cmpl.app; then
		isCMPL=1
	fi
elif [ $oSystem == "linux" ] ; then
    if test -f bin/cmpl; then
		isCMPL=1
	fi
elif [ $oSystem == "win" ] ; then
    if test -f bin/cmpl.exe; then
		isCMPL=1
	fi
fi

if [ $isCMPL == 0 ] ; then 
	echo "Please compile CMPL before creating the package"
	exit 1
fi

if test -d Package ;  then
	cd Package
else
	mkdir Package
    cd Package
fi

if test -d $oSystem ;     then 	
 	rm -R $oSystem 
fi	
mkdir $oSystem 
cd $oSystem  
mkdir Cmpl


if [ $oSystem == "osx" ]; then 

  	cp -R ../../bin/cmpl.app Cmpl
   	cp -R ../../bin Cmpl
    
   	cd Cmpl
   	macdeployqt cmpl.app  -no-plugins

   	cp -R cmpl.app/Contents/Frameworks .
   	cp cmpl.app/Contents/MacOS/cmpl bin

   	rm -R cmpl.app
   	rm -R bin/cmpl.app
   	

elif [ $oSystem == "linux" ] ; then
	
	cd Cmpl 
	mkdir bin
	cd bin

	coreFile=`find /usr/lib* | grep .libQtCore.so.4$`
    xmlFile=`find /usr/lib* | grep  .libQtXml.so.4$`

    if (test -f $coreFile) && (test -f $xmlFile) ; then
    	cp $coreFile .
    	cp $xmlFile .
    else
    	echo "Error: can not find the QT libs."
    	exit 1
    	fi
    cd ..
	cp -R ../../../bin .

elif [ $oSystem == "win" ] ; then

	cd Cmpl
	mkdir Frameworks
	cd Frameworks
	
	coreFile=`which QtCore4.dll`
    xmlFile=`which QtXml4.dll`
    if (test -f $coreFile) && (test -f $xmlFile) ; then
    	cp $coreFile .
    	cp $xmlFile .
    else
    	echo "Error: can not find the QT libs."
    	exit 1
 	fi
 	gccFile1=`which libstdc++-6.dll`
    gccFile2=`which mingwm10.dll`
    gccFile3=`which libgcc_s_dw2-1.dll`
    gccFile4=`which libpthread-2.dll`
    if (test -f $gccFile1) && (test -f $gccFile2) && (test -f $gccFile3) && (test -f $gccFile4); then
    	cp $gccFile1 .
    	cp $gccFile2 .
    	cp $gccFile3 .
    	cp $gccFile4 .
    else
    	echo "Error: can not find the gcc libs."
    	exit 1
 	fi
 	cd ..
 	mkdir bin
  	cp -R ../../../bin .
 
fi

if test -d ../../../Thirdparty ;  then
	cp -R ../../../Thirdparty .
else
	echo "Error: can not find the solver in Thirdparty"
	exit 1
fi

if test -d ../../../data/Coliop ;  then
	cp -R ../../../data/Coliop Coliop3
else
	echo "Error: can not find Coliop in data"
	exit 1
fi

cp ../../../data/gurobiCmpl/gurobiCmpl.py bin/gurobiCmpl.py

cp -R ../../../doc .
cp ../../../AUTHORS .
cp ../../../LICENSE .
#cp ../../../README .


packageSrc="../../../data/${oSystem}/*"
cp  -R $packageSrc .
rm gurobiPython*

mv coliop3.opt Coliop3/coliop3.opt

if test -f helpBrowser
then 
	mv helpBrowser Coliop3/helpBrowser
fi

rm cmpl.opt


cd 	../../../

echo "Package made in Package/${oSystem}"

