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


# 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="/usr/lib/libQtCore.so.4"
    xmlFile="/usr/lib/libQtXml.so.4"
    if (test -f $coreFile) && (test -f $xmlFile) ; then
    	cp $coreFile .
    	cp $xmlFile .
    else
    	coreFile="/usr/lib64/libQtCore.so.4"
    	xmlFile="/usr/lib64/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
 	fi
    cd ..
	cp -R ../../../bin .

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

	cd Cmpl
	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`
    if (test -f $gccFile1) && (test -f $gccFile2) && (test -f $gccFile3) ; then
    	cp $gccFile1 .
    	cp $gccFile2 .
    	cp $gccFile3 .
    else
    	echo "Error: can not find the gcc libs."
    	exit 1
 	fi
 	
 	
 	cp -R ../../../bin/* .
 	
fi

cp -R ../../../doc .
cp ../../../AUTHORS .
cp ../../../LICENSE .
packageSrc="../../../data/${oSystem}/*"
cp  -R $packageSrc .

cd 	../../../

