From ccf9a266e000b3d8d9259220ed6c040143cadda8 Mon Sep 17 00:00:00 2001
From: Florian Uhlig <f.uhlig@gsi.de>
Date: Mon, 7 Feb 2022 16:22:33 +0100
Subject: [PATCH] Update autoinstaller script

Move code into functions to get a clean main routine.
Add checking of prerequisites before compilation.
Add support for FairSoft versions after jun19 since these FairSoft
versions require a different build scheme.

Add CBM specific install instructions for OpenSuse and Fedora for
for packages which are not covered by the FairSoft documentation.
This is sqlite3 which is only needed by CBM and tbb and gsl which
showed problems on newer systems recently.
---
 autoinstall_framework.sh | 501 ++++++++++++++++++++++++++-------------
 1 file changed, 339 insertions(+), 162 deletions(-)

diff --git a/autoinstall_framework.sh b/autoinstall_framework.sh
index 858ba06f10..28187e3735 100755
--- a/autoinstall_framework.sh
+++ b/autoinstall_framework.sh
@@ -24,32 +24,57 @@
 # 13.03.2015 - initial version
 # by David Emschermann
 
-exec &> >(tee -a .autoinstall_framework.log) # So the user can check up a complete log if he wishes.
+main()
+{
+  exec &> >(tee -a .autoinstall_framework.log) # So the user can check up a complete log if he wishes.
 
+  setup_env_variables
 
-# choose your root version
-export ROOTVER=6
+  parse_command_line "$@"
 
-# put your desired variants here:
-export FSOFTDEV=jun19p3
-export FROOTDEV=v18.4.0
+  echo FSOFTVER: $FSOFTVER
+  echo FROOTVER: $FROOTVER
 
-export FSOFTPRO=jun19p3
-export FROOTPRO=v18.2.1
+  check_prerequisites
 
-export FSOFTOLD=jun19p3
-export FROOTOLD=v18.2.0
+  install_fairsoft
 
-# set default version to pro
-export FSOFTVER=$FSOFTPRO
-export FROOTVER=$FROOTPRO
+  install_fairroot
 
+  install_cbmroot
+}
 
-SETUP_FAIRSOFT=0 && SETUP_FAIRROOT=0 && SETUP_CBMROOT=0;
-[ $# -eq "0" ] && SETUP_FAIRSOFT=1 && SETUP_FAIRROOT=1 && SETUP_CBMROOT=1 : echo "Some Error has occurred"  # Default behaviour with no flags, all three should be installed.
+setup_env_variables()
+{
+  # choose your root version
+  export ROOTVER=6
 
-while test $# -gt 0; do
-	case "$1" in
+  # put your desired variants here:
+  export FSOFTDEV=apr21p2
+  export FROOTDEV=v18.6.7
+
+  export FSOFTPRO=jun19p3
+  export FROOTPRO=v18.2.1
+
+  export FSOFTOLD=jun19p3
+  export FROOTOLD=v18.2.0
+
+  # set default version to pro
+  export FSOFTVER=$FSOFTPRO
+  export FROOTVER=$FROOTPRO
+
+  # My experience tells me that the default could be 2, even if there is only 1 processor, as 1 processing and 1 queued job normally is optimal Will leave it at 1 though.
+  export NUMOFCPU=`[ -f /proc/cpuinfo ] && grep -i processor /proc/cpuinfo | wc -l || echo 1`
+  export CBMSRCDIR=`pwd`
+}
+
+parse_command_line()
+{
+  SETUP_FAIRSOFT=0 && SETUP_FAIRROOT=0 && SETUP_CBMROOT=0;
+  [ $# -eq "0" ] && SETUP_FAIRSOFT=1 && SETUP_FAIRROOT=1 && SETUP_CBMROOT=1 : echo "Some Error has occurred"  # Default behaviour with no flags, all three should be installed.
+
+  while test $# -gt 0; do
+  	case "$1" in
 	-s|-fs|-fairsoft|--fairsoft)
 		echo "*** FairSoft to be installed"
 		SETUP_FAIRSOFT="1";
@@ -98,28 +123,23 @@ while test $# -gt 0; do
 		# ./autobuild_framework dev 1 0 0
 		# would install FairSoft but not FairRoot nor CbmRoot.
 
-		if [ $1 -gt 0 ];
-		then
-		SETUP_FAIRSOFT="1";
-		echo "FairSoft flaged for install"
+		if [ $1 -gt 0 ]; then
+		  SETUP_FAIRSOFT="1";
+		  echo "FairSoft flaged for install"
 		fi
 
-		if [ ! -z $2 ];   # This combersome and longform if statement exist due to known issues regarding ampersands within bash cases.
-		then
-		if [ $2 -gt 0 ];
-		then
-		SETUP_FAIRROOT="1";
-		echo "FairRoot flaged for install"
-		fi
+		if [ ! -z $2 ]; then   # This combersome and longform if statement exist due to known issues regarding ampersands within bash cases.
+  		  if [ $2 -gt 0 ]; then
+		    SETUP_FAIRROOT="1";
+		    echo "FairRoot flaged for install"
+		  fi
 		fi
 
-		if [ ! -z $3 ];   # This combersome and longform if statement exist due to known issues regarding ampersands within bash cases.
-		then
-		if [ $3 -gt 0 ];
-		then
-		SETUP_CBMROOT="1";
-		echo "CbmRoot flaged for install"
-		fi
+		if [ ! -z $3 ]; then   # This combersome and longform if statement exist due to known issues regarding ampersands within bash cases.
+  		  if [ $3 -gt 0 ]; then
+  		    SETUP_CBMROOT="1";
+		    echo "CbmRoot flaged for install"
+		  fi
 		fi
 
 		# To prevent the pausing a script with maybe autoinstall_framework called the old way.
@@ -143,165 +163,322 @@ while test $# -gt 0; do
 		echo "Example case to install only FairRoot and CbmRoot (and not FairSoft)"
 		echo "./autoinstall_framework.sh -fr -cr"
 		exit 0;;
-  esac
-done
+    esac
+  done
+}
 
-# My experience tells me that the default could be 2, even if there is only 1 processor, as 1 processing and 1 queued job normally is optimal Will leave it at 1 though.
-export NUMOFCPU=`[ -f /proc/cpuinfo ] && grep -i processor /proc/cpuinfo | wc -l || echo 1`
-export CBMSRCDIR=`pwd`
+check_prerequisites()
+{
 
-#-----------------------------------------------------------------------------------------------------
+  nof_missing_packages=0
+  missing_packages=""
 
-echo FSOFTVER: $FSOFTVER
-echo FROOTVER: $FROOTVER
+  year=$(echo $FSOFTVER | cut -c4,5)
 
-#-----------------------------------------------------------------------------------------------------
-#
-##   FairSoft
-#
+  # Array with binaries which are needed for the compilation
+  programs=(cmake curl gcc g++ gfortran make patch sed bzip2 unzip gzip tar svn git flex lsb_release wget automake autoconf libtoolize)
 
-if [ $SETUP_FAIRSOFT -ge 1 ]; then
-  echo "Setting up FairSoft ..."
+  for executable in "${programs[@]}"; do
+    check_executable $executable
+  done
 
-  # check if sqlite3 is available
-  if [ -f /usr/include/sqlite3.h ] ; then
-   echo
-   echo "Sqlite3 is available"
-   echo
-  else
-   echo
-   echo "Sqlite3 is not available"
-   echo
-   echo "On Debian, please install as follows:"
-   echo "sudo apt install libsqlite3-dev"
-   echo
-   echo "On OpenSuSE, please install as follows:"
-   echo "zypper install libsqlite3-0 sqlite3-devel"
-   echo
-   echo "afterwards, restart autoinstall_framework.sh"
-   echo
-   sleep 5
-   exit
+  check_file_exist /usr/include/sqlite3.h sqlite3-dev
+  check_file_exist /usr/include/X11/Xlib.h x11-dev
+  check_file_exist /usr/include/X11/Xft/Xft.h xft-dev
+  check_file_exist /usr/include/X11/extensions/Xext.h xext-dev
+  check_file_exist /usr/include/X11/xpm.h xpm-dev
+  check_file_exist /usr/include/GL/glu.h mesa-dev
+  check_file_exist /usr/include/curses.h ncurses-dev
+  check_file_exist /usr/include/bzlib.h bz2-dev
+  check_file_exist /usr/include/openssl/ssl.h ssl-dev
+  check_file_exist /usr/include/gsl/gsl_types.h gsl-dev
+  check_file_exist /usr/include/tbb/tbb.h tbb-dev
+
+  check_file_exist /usr/bin/curl-config curl-dev
+  check_file_exist /usr/bin/xml2-config xml2-dev
+
+
+  if [ $year -lt 20 ]; then
+    check_file_exist /usr/bin/xmkmf xutils-dev
+    check_file_exist /usr/bin/krb5-config krb5-dev
   fi
 
-  cd ..
-  git clone https://github.com/FairRootGroup/FairSoft fairsoft_src_${FSOFTVER}_root${ROOTVER}
-  cd fairsoft_src_${FSOFTVER}_root${ROOTVER}
-  git tag -l
-  git checkout -b $FSOFTVER $FSOFTVER
+  tmp=$(which python2-config)
+  if [ $? -ne 0 ]; then
+    tmp=$(which python3-config)
+    if [ $? -ne 0 ]; then
+      nof_missing_packages=$((nof_missing_packages+1))
+      missing_packages="python-dev, $missing_packages"
+    fi
+  fi
 
-  if [ $ROOTVER -eq 6 ]; then
-    sed s/build_root6=no/build_root6=yes/ automatic.conf > automatic1_root.conf
-  else 
-    cp automatic.conf automatic1_root.conf
+  if [ $nof_missing_packages -ne 0 ]; then
+    echo ""
+    echo "The following packages are missing and need to be installed"
+    echo $missing_packages
+    echo ""
+    echo "For further information which packages need to be installed"
+    echo "check the documentation on the FairSoft GitHub page at"
+    if [ $year -lt 20 ]; then
+      echo ""
+      echo "https://github.com/FairRootGroup/FairSoft/tree/$FSOFTVER"
+      echo ""
+      echo "For Linux check dependencies.md"
+      echo "For macosx check dependencies_macosx.md"
+    else
+      echo "https://github.com/FairRootGroup/FairSoft/blob/$FSOFTVER/legacy/dependencies.md"
+    fi
+    echo ""
+    echo "and follow the given instructions."
+    echo
+    # check if sqlite3 is available
+    missing=0
+    if [[ "$missing_packages" =~ .*"sqlite3".* ]]; then
+      missing=$((missing+1))
+    fi
+    if [[ "$missing_packages" =~ .*"gsl".* ]]; then
+      missing=$((missing+1))
+    fi
+    if [[ "$missing_packages" =~ .*"tbb".* ]]; then
+      missing=$((missing+1))
+    fi
+    if [[ $missing -ne 0 ]];then
+      if [[ "$missing_packages" =~ .*"sqlite3".* ]]; then
+        echo
+        echo "Sqlite3 is not available which may not be part of the above installation instructions"
+        echo
+        echo "On Debian or Ubuntu, please install as follows:"
+        echo "sudo apt install libsqlite3-dev"
+        echo
+        echo "On OpenSuSE, please install as follows:"
+        echo "zypper install libsqlite3-0 sqlite3-devel"
+        echo
+        echo "On Fedora, please install as follows:"
+        echo "dnf install sqlite-devel"
+        echo
+        echo "afterwards, restart autoinstall_framework.sh"
+        echo
+      fi
+      if [[ "$missing_packages" =~ .*"tbb".* ]]; then
+        echo
+        echo "The tbb development package is not available which may not be part of the above installation instructions"
+        echo
+        echo "On Debian or Ubuntu, please install as follows:"
+        echo "sudo apt install libtbb-dev"
+        echo
+        echo "On OpenSuSE, please install as follows:"
+        echo "zypper install tbb-devel"
+        echo
+        echo "On Fedora, please install as follows:"
+        echo "dnf install tbb-devel"
+        echo
+        echo "afterwards, restart autoinstall_framework.sh"
+        echo
+      fi
+      if [[ "$missing_packages" =~ .*"gsl".* ]]; then
+        echo
+        echo "The gsl development package is not available which may not be part of the above installation instructions"
+        echo
+        echo "On Debian or Ubuntu, please install as follows:"
+        echo "sudo apt install libgsl-dev"
+        echo
+        echo "On OpenSuSE, please install as follows:"
+        echo "zypper install gsl-devel"
+        echo
+        echo "On Fedora, please install as follows:"
+        echo "dnf install gsl-devel"
+        echo
+        echo "afterwards, restart autoinstall_framework.sh"
+        echo
+      fi
+    fi
+    exit
   fi
+  echo
+  echo "All needed external software is installed"
+  echo
+}
+
+check_executable()
+{
+  # check if executable is available
+  # pass result into a temporary variable to have a quite mode
+  tmp=$(which $1)
+  if [ $? -ne 0 ]; then
+    nof_missing_packages=$((nof_missing_packages+1))
+    missing_packages="$1, $missing_packages"
+  fi
+}
 
-  FSOFTINSTALLPATH=`pwd | sed s/fairsoft_src_/fairsoft_/`
-  sed /SIMPATH_INSTALL/d automatic1_root.conf > automatic2_path.conf
-  echo "  SIMPATH_INSTALL=$FSOFTINSTALLPATH/installation" >> automatic2_path.conf
+check_file_exist()
+{
+  file=$1
+  package=$2
 
-  sed s/compiler=/compiler=gcc/ automatic2_path.conf > automatic3_gcc.conf
+  if [ ! -f $file ] ; then
+    nof_missing_packages=$((nof_missing_packages+1))
+    missing_packages="$package, $missing_packages"
+  fi
+}
 
-  ./configure.sh automatic3_gcc.conf
 
-  cd $CBMSRCDIR
-  echo "done installing FairSoft"
-fi
+install_fairsoft()
+{
+  if [ $SETUP_FAIRSOFT -ge 1 ]; then
+    echo "Setting up FairSoft ..."
 
-#-----------------------------------------------------------------------------------------------------
-#
-##   FairRoot
-#
+    cd ..
+    git clone https://github.com/FairRootGroup/FairSoft fairsoft_src_${FSOFTVER}_root${ROOTVER}
+    cd fairsoft_src_${FSOFTVER}_root${ROOTVER}
+    git tag -l
+    git checkout -b tag_$FSOFTVER $FSOFTVER
 
-if [ $SETUP_FAIRROOT -ge 1 ]; then
-  echo "Setting up FairRoot ..."
+    year=$(echo $FSOFTVER | cut -c4,5)
+    if [ $year -lt 20 ]; then
+      install_fairsoft_before_2020
+    else
+      install_fairsoft_since_2020
+    fi
 
-  # set SIMPATH
-  cd ..
-  echo "SIMPATH	before: $SIMPATH"
-  cd fairsoft_${FSOFTVER}_root${ROOTVER}/installation/
-  export SIMPATH=`pwd`
-  echo "SIMPATH	now   : $SIMPATH"
-  cd $CBMSRCDIR
+    cd $CBMSRCDIR
+    echo "done installing FairSoft"
+  fi
+}
 
-  echo PATH=$SIMPATH/bin:$PATH
-  export PATH=$SIMPATH/bin:$PATH
-
-  cd ..
-  git clone https://github.com/FairRootGroup/FairRoot.git fairroot_src_$FROOTVER-fairsoft_${FSOFTVER}_root${ROOTVER}
-  cd fairroot_src_$FROOTVER-fairsoft_${FSOFTVER}_root${ROOTVER}
-  git tag -l
-  git checkout -b $FROOTVER $FROOTVER
-  mkdir build
-  cd build
-  cmake \
-    -DCMAKE_CXX_COMPILER=$($SIMPATH/bin/fairsoft-config --cxx) \
-    -DCMAKE_C_COMPILER=$($SIMPATH/bin/fairsoft-config --cc) \
-    -DCMAKE_INSTALL_PREFIX=../../fairroot_$FROOTVER-fairsoft_${FSOFTVER}_root${ROOTVER} \
-    ..
-  nice make install -j$NUMOFCPU
+install_fairsoft_before_2020()
+{
+  if [ $ROOTVER -eq 6 ]; then
+    sed s/build_root6=no/build_root6=yes/ automatic.conf > automatic1_root.conf
+  else
+    cp automatic.conf automatic1_root.conf
+  fi
 
-  cd $CBMSRCDIR
-  echo "done installing FairRoot"
-fi
+  FSOFTINSTALLPATH=`pwd | sed s/fairsoft_src_/fairsoft_/`
+  sed /SIMPATH_INSTALL/d automatic1_root.conf > automatic2_path.conf
+  echo "  SIMPATH_INSTALL=$FSOFTINSTALLPATH/installation" >> automatic2_path.conf
 
-#-----------------------------------------------------------------------------------------------------
-#
-##   CbmRoot
-#
+  sed s/compiler=/compiler=gcc/ automatic2_path.conf > automatic3_gcc.conf
 
-if [ ${SETUP_CBMROOT} -eq "1" ]; 
-then
-  echo "Setting up CbmRoot ..."
+  ./configure.sh automatic3_gcc.conf
+  result=$?
+  if [ $result -ne 0 ]; then
+    echo
+    echo "Something went wrong with the installation."
+    echo "Please check the output above."
+    exit
+  fi
 
-  # set SIMPATH
-  cd ..
-  echo "SIMPATH	before: $SIMPATH"
-  cd fairsoft_${FSOFTVER}_root${ROOTVER}/installation/
-  export SIMPATH=`pwd`
-  echo "SIMPATH	now   : $SIMPATH"
   cd $CBMSRCDIR
+}
 
-  # set FAIRROOTPATH
-  cd ..
-  cd fairroot_$FROOTVER-fairsoft_${FSOFTVER}_root${ROOTVER}
-  export FAIRROOTPATH=`pwd`
-  echo "FAIRROOTPATH: $FAIRROOTPATH"
-  cd $CBMSRCDIR
 
-  echo PATH=$SIMPATH/bin:$PATH
-  export PATH=$SIMPATH/bin:$PATH
+install_fairsoft_since_2020()
+{
+  FSOFTINSTALLPATH=`pwd | sed s/fairsoft_src_/fairsoft_/`
 
-  cd ..
+  cmake -S $PWD -B $PWD/build -C $PWD/FairSoftConfig.cmake -DCMAKE_INSTALL_PREFIX=$FSOFTINSTALLPATH/installation
+  if [ $? -ne 0 ]; then
+    echo "Something went wrong with the FairSoft configuration"
+    exit
+  fi
 
-  cd $CBMSRCDIR
-  mkdir build
-  cd build
-  cmake \
-    -DCMAKE_CXX_COMPILER=$($SIMPATH/bin/fairsoft-config --cxx) \
-    -DCMAKE_C_COMPILER=$($SIMPATH/bin/fairsoft-config --cc) \
-    ..
-  nice make -j$NUMOFCPU
-  cd ..
-
-echo "done installing CbmRoot"
-
-[ -z $ANSWER ] && (
-cat << EOT
+  cmake --build $PWD/build -j$NUMOFCPU
+  if [ $? -ne 0 ]; then
+    echo "Something went wrong with the FairSoft build"
+    exit
+  fi
+}
+
+install_fairroot()
+{
+  if [ $SETUP_FAIRROOT -ge 1 ]; then
+    echo "Setting up FairRoot ..."
+    cd ..
+    echo "SIMPATH	before: $SIMPATH"
+    cd fairsoft_${FSOFTVER}_root${ROOTVER}/installation/
+    export SIMPATH=`pwd`
+    echo "SIMPATH	now   : $SIMPATH"
+    cd $CBMSRCDIR
+
+    echo PATH=$SIMPATH/bin:$PATH
+    export PATH=$SIMPATH/bin:$PATH
+
+    cd ..
+    git clone https://github.com/FairRootGroup/FairRoot.git fairroot_src_$FROOTVER-fairsoft_${FSOFTVER}_root${ROOTVER}
+    cd fairroot_src_$FROOTVER-fairsoft_${FSOFTVER}_root${ROOTVER}
+    git tag -l
+    git checkout -b tag_$FROOTVER $FROOTVER
+    mkdir -p build
+    cd build
+    cmake \
+      -DCMAKE_CXX_COMPILER=$($SIMPATH/bin/fairsoft-config --cxx) \
+      -DCMAKE_C_COMPILER=$($SIMPATH/bin/fairsoft-config --cc) \
+      -DCMAKE_INSTALL_PREFIX=../../fairroot_$FROOTVER-fairsoft_${FSOFTVER}_root${ROOTVER} \
+      ..
+    nice make install -j$NUMOFCPU
+
+    cd $CBMSRCDIR
+    echo "done installing FairRoot"
+  fi
+}
+
+install_cbmroot()
+{
+  if [ ${SETUP_CBMROOT} -eq "1" ]; then
+    echo "Setting up CbmRoot ..."
+
+    cd ..
+    echo "SIMPATH	before: $SIMPATH"
+    cd fairsoft_${FSOFTVER}_root${ROOTVER}/installation/
+    export SIMPATH=`pwd`
+    echo "SIMPATH	now   : $SIMPATH"
+    cd $CBMSRCDIR
+
+    cd ..
+    cd fairroot_$FROOTVER-fairsoft_${FSOFTVER}_root${ROOTVER}
+    export FAIRROOTPATH=`pwd`
+    echo "FAIRROOTPATH: $FAIRROOTPATH"
+    cd $CBMSRCDIR
+
+    echo PATH=$SIMPATH/bin:$PATH
+    export PATH=$SIMPATH/bin:$PATH
+
+    cd ..
+
+    cd $CBMSRCDIR
+    mkdir -p build
+    cd build
+    cmake \
+      -DCMAKE_CXX_COMPILER=$($SIMPATH/bin/fairsoft-config --cxx) \
+      -DCMAKE_C_COMPILER=$($SIMPATH/bin/fairsoft-config --cc) \
+      ..
+
+    nice make -j$NUMOFCPU
+    if [ $? -ne 0 ]; then
+      echo "Something went wrong with the CbmRoot installation"
+      exit
+    fi
+
+    cd ..
+
+    echo "done installing CbmRoot"
+
+    [ -z $ANSWER ] && (
+  cat << EOT
 Since the system is now installed. Shall I switch to the new environment?
 source build/config.sh
 Reply Yes or Y for confirmation ????
 EOT
-) && read ANSWER
-
-if (echo "$ANSWER" | sed -n '/^\(Y\|y\)/!{q10}')
-then
-  echo "A yes detected."
-  source build/config.sh;
-  echo "Environmental variables and paths updated"
-fi
+    ) && read ANSWER
 
-fi
+    if (echo "$ANSWER" | sed -n '/^\(Y\|y\)/!{q10}'); then
+      echo "A yes detected."
+      source build/config.sh;
+      echo "Environmental variables and paths updated"
+    fi
+  fi
+}
 
 #####################################################################################
 
+main "$@"
-- 
GitLab