#!/bin/bash
#
# Installation script for tomoprocess.
#
# Downloads and installs all the required libraries.
# Compiles tomoprocess and create the executable.
#
# Script versio: 0.3
# WARNING: experimental script.
#
# Christophe Leblanc.
# cleblancad@gmail.com
# 08/01/2016

#
# Global variables
#
abort=0
ok=1
has_apt=2

# Must be the three last.
# Need special names for g++ and gfortran
# (Namely gcc-c++ and gcc-gfortran)
last_tag=3
has_yum=$(($last_tag + 1))
has_zypper=$(($last_tag + 2))
has_urpmi=$(($last_tag + 3))

#
# Warns the user about the fact that this is an experimental script.
# Lets him/her the choice to continue at his/her own risks or not...
#
function WarnUser
{
echo " "
echo "/******************************/"
echo "/*        TOMOPROCESS         */"
echo "/******************************/"
echo " "
echo "This installation script is experimental. Successfull installation is not guaranted. Proceed on your own risk."
echo "I understand this warning. Proceed with the installation: "

select answer in yes no; do
  if [ x$answer = xno ]; then
    echo "Aborting installation..."
    return $abort
  else
    return $ok
  fi
done
}

#
# Checks if apt or yum is installed.
# If none of them is installed, aborts installation.
#
function CheckInstaller
{
echo "Checking if the system has apt or yum."

if hash apt-get 2>/dev/null; then
  echo "The system has apt."
  sudo apt-get update
  return $has_apt
elif hash yum 2>/dev/null; then
  echo "The system has yum."
  sudo yum check-update
  return $has_yum
elif hash zypper 2>/dev/null; then
  echo "The system has zypper."
  sudo zypper refresh
  return $has_zypper
elif hash urpmi 2>/dev/null; then
  echo "The system has urpmi"
  sudo urpmi.update -a
  return $has_urpmi
else
  echo "The system has neither apt nor yum. Please install one of them before proceeding. Aborting..."
  return $abort
fi
}

#
# General check routine for programs.
#
# $1 installer name
# $2 program name
function GeneralCheck
{
echo "Checking if the system has" $2
if hash $2 2>/dev/null; then
  echo "The system has" $2
  return $ok
else
  echo $2 "is needed in order to proceed with the installation. Install it now ?"

  select answer in yes no; do
    if [ x$answer == xyes ]; then
      echo "Installing" $2

      if [ $1 == $has_apt ]; then
        sudo apt-get install -y $2
        return $ok
      elif [ $1 == $has_yum ]; then
        sudo yum -y install $2
        return $ok
      elif [ $1 == $has_zypper ]; then
        sudo zypper --non-interactive install $2
        return $ok
      elif [ $1 == $has_urpmi ]; then
        sudo urpmi $2
        return $ok
      else
        echo "Unknown installation program, aborting..."
        return $abort
      fi

    else
      echo "Aborting installation..."
      return $abort
    fi
  done

fi
}

#
# General check routine for libraries.
#
# $1 installer name
# $2 library name
function LibraryCheck
{
echo "Checking if the system has" $2

if [ $1 == $has_apt ]; then
  sudo apt-get install -y $2
  return $ok
elif [ $1 == $has_yum ]; then
  sudo yum -y install $2
  return $ok
elif [ $1 == $has_zypper ]; then
  sudo zypper --non-interactive install $2
  return $ok
elif [ $1 == $has_urpmi ]; then
  sudo urpmi $2
  return $ok
else
  echo "Unknown installation program, aborting..."
  return $abort
fi
}

#
# General download routine.
#
# $1 file name
# $2 curl download command
# $3 new folder name (if any)
function GeneralDownload
{
if [ ! -f $1 ]; then
  echo "Downloading" $1

  if [ $# == 3 ]; then
    mkdir $3
  fi
  curl $2
fi
}

#
# Unpacks and installs gmm.
#
function InstallGMM
{
if [ ! -d gmm-4.2 ]; then
  echo "Unpacking and installing GMM..."
  gzip -d gmm.tar.gz
  tar -xf gmm.tar
  cd gmm-4.2
  ./configure
  make
  sudo make install
  cd ../
fi
}

#
# Unpacks ans install gmm.
#
function InstallITK
{
cd ITK
if [ ! -d build ]; then
  echo "Unpacking and installing Insight Toolkit..."
  mkdir build
  unzip itk.zip
  cd build
  cmake ../InsightToolkit-4.7.1 -DBUILD_TESTING=OFF -DBUILD_EXAMPLES=OFF -DITKV3_COMPATIBILITY=ON -DITK_USE_CONCEPT_CHECKING=OFF -DModule_AnalyseObjectMapIO=ON -DModule_SplitComponents=ON -DModule_HigherOrderAccurateGradient=ON -DModule_IOSTL=ON -DModule_ITKIODCMTK=ON -DModule-ITKIOMINC=ON -DModule_ITKIOPhilipsREC=ON -DModule_ITKLevelSetsv4Visualization=ON -DModule_ITKMINC=ON -DModule_ITKReview=ON -DModule_ITKVTGlue=ON -DModule_LesionSizingToolkit=ON -DModule_MGHIO=ON -DModule_SCIFIO=ON -DModule_SkullStrip=ON -DModule_SmoothingRecursiveYvvGaussianFilter=ON -DModule_SubdivisionQuadEdgeMeshFilter=ON -DModule_VariationalRegistration=ON

  # Gets the number of processors.
  nbp=$(cat /proc/cpuinfo | grep processor | wc -l 2>&1)
  make -j$nbp
  cd ../
fi

cd ../
}

#
# Unpacks and installs CGAL
#
function InstallCGAL
{
cd CGAL
if [ ! -d build ]; then
  echo "Unpacking and installing CGAL..."
  mkdir build
  gzip -d cgal.tar.gz
  tar -xf cgal.tar
  cd build
  cmake ../CGAL-4.6

  # Gets the number of processors.
  nbp=$(cat /proc/cpuinfo | grep processor | wc -l 2>&1)
  make -j$nbp
  cd ../
fi

cd ../
}

#
# Unpacks and installs Tomoprocess
#
function InstallTomoprocess
{
if [ ! -d tomoprocess ]; then
  echo "Unpacking and installing Tomoprocess..."
  workingDir=$(pwd)
  gzip -d tomoprocess.tar.gz
  tar -xf tomoprocess.tar
  cd tomoprocess
  mkdir build
  cd build

  cmake ../ -DITK_DIR="$workingDir/ITK/build" -DCGAL_DIR="$workingDir/CGAL/build"

  # Gets the number of processors.
  nbp=$(cat /proc/cpuinfo | grep processor | wc -l 2>&1)
  make -j$nbp
  cd ../../
fi
}

#
# Does all the jobs
#

WarnUser
if [ $? == $ok ]; then
  CheckInstaller
  installer=$?
  
  gcc_name="g++"
  gfortran_name="gfortran"

  if [ $installer -ge $last_tag ]; then
    gcc_name="gcc-c++"
    gfortran_name="gcc-gfortran"
  fi

  if [ $installer -gt $abort ]; then
    GeneralCheck $installer curl
    if [ $? == $ok ]; then
      GeneralCheck $installer cmake
      if [ $? == $ok ]; then
        GeneralCheck $installer tar
        if [ $? == $ok ]; then
          GeneralCheck $installer gzip
          if [ $? == $ok ]; then
            GeneralCheck $installer unzip
            if [ $? == $ok ]; then
              GeneralCheck $installer $gcc_name
              if [ $? == $ok ]; then
                GeneralCheck $installer $gfortran_name
                if [ $? == $ok ]; then
                  GeneralCheck $installer git
                  if [ $? == $ok ]; then
                    LibraryCheck $installer libboost-all-dev
                    if [ $? == $ok ]; then
                      LibraryCheck $installer libvtk6-dev
                      if [ $? == $ok ]; then
                        LibraryCheck $installer liblapack-dev
                        if [ $? == $ok ]; then
                          LibraryCheck $installer libgmp-dev
                          if [ $? == $ok ]; then
                            LibraryCheck $installer libmpfr-dev
                              if [ $? == $ok ]; then
                                LibraryCheck $installer libcdd-dev
                                if [ $? == $ok ]; then
                                  LibraryCheck $installer libgsl0-dev
				    if [ $? == $ok ]; then
				      LibraryCheck $installer libmatheval-dev
                                      if [ $? == $ok ]; then
                                        GeneralDownload gmm.tar.gz "-o gmm.tar.gz http://download.gna.org/getfem/stable/gmm-4.2.tar.gz"

                                        GeneralDownload ITK/itk.zip "-L -o ./ITK/itk.zip http://downloads.sourceforge.net/project/itk/itk/4.7/InsightToolkit-4.7.1.zip" ITK

                                        GeneralDownload CGAL/cgal.tar.gz "-o ./CGAL/cgal.tar.gz https://gforge.inria.fr/frs/download.php/file/34704/CGAL-4.6.tar.gz" CGAL

                                        InstallGMM
                                        InstallITK
                                        InstallCGAL
                                        InstallTomoprocess
				      fi
                                    fi
                                fi
                            fi
                          fi
                        fi
                      fi
                    fi
                  fi
                fi
              fi
            fi
          fi
        fi
      fi
    fi
  fi
fi
