#!/bin/bash
echo "**********************************************************"
echo "*                        MAGiC                           *"
echo "*                                                        *"
echo "*                        ERICCA                          *"
echo "*                                                        *"
echo "*         Departement de genie mecanique - UQTR          *"
echo "*                                                        *"
echo "*             Installation des modules MAGiC             *"
echo "*                                                        *"
echo "**********************************************************"
echo ""
echo $(date)
echo ""
echo "MAGiC installation"
echo "Checking / Installing dependencies"
tabdep=(gmsh gfortran cmake cmake-curses-gui subversion libvtk9-dev g++ gcc python3-dev python3-numpy tk bison flex libblas-dev liblapack-dev libmotif-dev libxi-dev libxmu-dev tcllib tklib tcl-dev tk-dev libfreetype-dev libx11-dev libgl1-mesa-dev libfreeimage-dev qtbase5-dev)
versionos=$(lsb_release -sr)
if [ "$1" == "tout" ];
    then
    tabdep=( "${tabdep[@]}" "kdevelop")
    tabdep=( "${tabdep[@]}" "kdesvn")
    tabdep=( "${tabdep[@]}" "kile")
    tabdep=( "${tabdep[@]}" "doxygen")
    tabdep=( "${tabdep[@]}" "openssh-client")
    tabdep=( "${tabdep[@]}" "openssh-server")
fi
echo "Dependance a verifier :"${#tabdep[*]}
for i in ${!tabdep[*]};
do
nomdep=${tabdep[i]}
dpkg -s $nomdep  >> /dev/null 2>&1
if [ $? == 1 ];
then
echo $nomdep...............FAIL
sudo apt-get -y install $nomdep  
else
echo $nomdep...............ok
fi

done

echo "Verification opencascade"
__DIR=`pwd`
cd /usr/local/include
if [ ! -d opencascade ];
then
echo "installation opencascade ..................FAIL"
echo "installation opencascade"
cd $__DIR
wget http://ericca.uqtr.ca/opencascade-7.1.0.tgz
tar xzvf opencascade-7.1.0.tgz
sudo ln -s /usr/include/locale.h /usr/include/xlocale.h
cd opencascade-7.1.0
mkdir build 
cd build
cmake ..
sudo make install -j8
fi
cd $__DIR


echo "Installation MAGiC"
__DIR=`pwd`
echo $__DIR
cd $__DIR
#if [ ! -d cadxfem ];
#then
#echo "Getting nutil from cadxfem"
#svn co https://cg-dev.ltas.ulg.ac.be/svn/cadxfem/nutil cadxfem/nutil
#else
#echo "Updating nutil from cadxfem"
#cd cadxfem/nutil
#svn up
#fi
cd $__DIR
if [ ! -d magic ];
then
echo "Getting magic"
svn co https://ericca.uqtr.ca/svn/magic magic
else
echo "Updating magic"
cd magic
svn up
fi
cd $__DIR/magic/exe
cmake .. -DCMAKE_BUILD_TYPE=Debug -DENABLE_TUTORIEL=ON -DENABLE_TEST=OFF -DENABLE_VTKDISPLAY=ON 
make -j8
echo "Check target"
cd $__DIR/magic/exe/app/mgoperation/
if [ -f mgoperation.exe ];
then
echo "installation complete..................OK"
else
echo "installation echouee..................FAIL"
fi
echo ""
echo $(date)
echo ""
echo "**********************************************************"
echo "*                        MAGiC                           *"
echo "*                                                        *"
echo "*                        ERICCA                          *"
echo "*                                                        *"
echo "*         Departement de genie mecanique - UQTR          *"
echo "*                                                        *"
echo "* Installation des modules MAGiC - nutil - Code aster    *"
echo "*                                                        *"
echo "**********************************************************"


