##########################################################
# Checking the availability of R pacakages and installing
# them if needed
# Authors : Najda Villefranque and Frédéric Hourdin
##########################################################
set -eu  # exit on most (not all!) error codes, and error on unset variables

upgrade_dependencies=FALSE

file_list_packages=$1

echo "$R_LIBS_USER"
ls "$R_LIBS_USER"

function install_package() {
  Rscript -e "install.packages(\"$1\", repos=\"https://cran.rstudio.com\", upgrade_dependencies=$upgrade_dependencies, lib=\"$R_LIBS_USER\")"
}

function install_package_from_github() {
  set -xv
  Rscript -e "library(devtools); options(download.file.method = 'wget') ; install_github(\"$1\", ref=\"$2\", upgrade_dependencies=$upgrade_dependencies, lib=\"$R_LIBS_USER\")"
  set +xv
}

function install_package_from_archive() {
  Rscript -e "install.packages(\"$1\", repos=NULL, type=\"source\", upgrade_dependencies=$upgrade_dependencies, lib=\"$R_LIBS_USER\")"
}

function install_package_compat() {
  Rscript -e "options(repos = c(CRAN = \"https://cran.rstudio.com\")); oldr::install.compatible.packages(\"$1\", lib=\"$R_LIBS_USER\")"
}

# we need oldr to be able to install packages using tar archives
#if [[ ! -d $R_LIBS_USER/oldr ]] ; then
#  install_package_from_archive "https://github.com/duckmayr/oldr/archive/26205c4fbb3edf6495bd71f933d04a0a8e9dfea1.tar.gz"
#fi

for p_ in $(cat $file_list_packages); do 

  ##########################
  # parse the line in file #
  ##########################
  from_github=0
  from_archive=0
  from_cran=0
  if [[ "$p_" == *":"* ]] ; then 
    from_github=1
    # parse package name and commit 
    fp=$(echo $p_|cut -d: -f1)
    commit=$(echo $p_|cut -d: -f2)
    case $fp in
      *"dgpsi-R"*) p=dgpsi ;;
      *"tinydancer"*) p=tinydancer;;
    esac
  else
    if [[ "$p_" == *"="* ]] ; then 
      # parse package name and version 
      p=$(echo $p_|cut -d= -f1)
      version=$(echo $p_|cut -d= -f2)
      if [[ "$version" == *"auto"* ]]; then
        # will be downloaded using the CRAN database
        from_archive=0
        from_cran=1
      else 
        from_archive=1
        from_cran=0
      fi
    else
      echo "Unkonwn format specification in $file_list_packages" ; exit 1 
    fi
  fi

  ##########################
  #   install the package  #
  ##########################
  if [[ -d $R_LIBS_USER/$p ]]; then
    echo "Found <$p> in $R_LIBS_USER"
  else
    if [ $from_github -eq 1 ] ; then 
      install_package_from_github "$fp" "$commit"
    fi 

    if [ $from_archive -eq 1 ] ; then
      install_package_from_archive "https://cran.r-project.org/src/contrib/Archive/$p/${p}_$version.tar.gz"
      test ! -d $R_LIBS_USER/$p && from_cran=1
    fi

    if [ $from_cran -eq 1 ] ; then
        install_package "$p"
    fi
  fi

  ##########################
  # check package install  #
  ##########################
  if [[ -d $R_LIBS_USER/$p ]] ; then
      echo "<$p> successfully installed"
  else
      echo "STOP: Problem encountered when installing R package <$p>"; exit 1
  fi
done

echo "$0: Installed all requested R packages"
