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

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 == "raspbian" ] ; 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 

	echo "Copying Cmpl and Coliop"

  	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
  	
  	cp -R ../../../../Coliop4/bin/Coliop4.app bin
   
   	echo "... done"
   	 	

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

	echo "Copying Cmpl and Coliop"

	cd Cmpl 
	mkdir bin
	cd bin

	cp ${QTLibs}/libQt5Xml.so.5 .
	cp ${QTLibs}/libQt5Core.so.5 .

	cp ${QTLibs}/libicui18n.so.54 .
	cp ${QTLibs}/libicuuc.so.54 .
	cp ${QTLibs}/libicudata.so.54 .

	cp ${QTLibs}/libQt5PrintSupport.so.5 . 
	cp ${QTLibs}/libQt5Widgets.so.5 .
	cp ${QTLibs}/libQt5Gui.so.5 .
	cp ${QTLibs}/libQt5DBus.so.5 .

	mkdir platforms
	cd platforms

	cp ${QTLibs}/../plugins/platforms/libqxcb.so .

	cd ..

	cp ${QTLibs}/libQt5XcbQpa.so.5 .

	cp /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 .
	
	cd ..

	cp -R ../../../bin .

	cd bin 
	cp -R ../../../../../Coliop4/bin/* .

	cd ..

	echo "...done"

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

	echo "Copying Cmpl and Coliop"

	cd Cmpl 
	mkdir bin
	cd bin

	cp ${QTLibs}/libQt5Xml.so.5 .
	cp ${QTLibs}/libQt5Core.so.5 .

	cp /usr/lib/arm-linux-gnueabihf/libicui18n.so.48 .
	cp /usr/lib/arm-linux-gnueabihf/libicuuc.so.48 .
	cp /usr/lib/arm-linux-gnueabihf/libicudata.so.48 .

	cd ..

	cp -R ../../../bin .

	mkdir Coliop3
	cd Coliop3

	cp -R ../../../../../Coliop3/dist/* .
	cp ../../../../../Coliop3/AUTHORS .
	cp ../../../../../Coliop3/README .
	cp ../../../../../Coliop3/LICENSE .

	cd ..

	echo "...done"
fi

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

	echo "Copying Cmpl and Coliop"
	
    	cd Cmpl
   
  	mkdir bin
  	
  	cp -R ../../../bin .
  	
  	cd bin 
   	cp -R ../../../../../Coliop4/bin/* .
      	
   	echo "...done"
   	
   	echo "Copying Solvers"
	if test -d ../../../../Thirdparty ;  then
		cp -R ../../../../Thirdparty/CBC/cbc.exe .
		cp -R ../../../../Thirdparty/GLPK/glpsol.exe .
	else
		echo "Error: can not find the solver in Thirdparty"
		exit 1
	fi
	echo "...done"
	
	cd ..
else 
 
	echo "Copying Solvers"
	if test -d ../../../Thirdparty ;  then
		cp -R ../../../Thirdparty .
	else
		echo "Error: can not find the solver in Thirdparty"
		exit 1
	fi
	echo "...done"
fi




echo "Copying others"
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*
if [ $oSystem == "raspbian" ] ; then
	mv Coliop3.opt Coliop3
fi


if [ $oSystem == "win" ] ; then
    mv manual.bat bin
    mv cmpl.opt-run bin/cmpl.opt
else 
	mv manual bin
fi

echo "...done"

rm cmpl.opt


cd 	../../../

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

