From dd6442e8ddfa945a5209d511d49c263d575ec0e3 Mon Sep 17 00:00:00 2001
From: Andrea Dell'Amico <adellam@isti.cnr.it>
Date: Wed, 6 Feb 2019 15:45:57 +0100
Subject: [PATCH] Change the algorithms-updater script so that it installs some
 missing algorithms even when there is no update from subversion.

---
 .../templates/algorithms-updater.j2              | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/smartgears/dataminer_app/templates/algorithms-updater.j2 b/smartgears/dataminer_app/templates/algorithms-updater.j2
index b0e7556..a5c3006 100644
--- a/smartgears/dataminer_app/templates/algorithms-updater.j2
+++ b/smartgears/dataminer_app/templates/algorithms-updater.j2
@@ -7,7 +7,7 @@ PROCNUM=$$
 INFRA_REFERENCE={{ dataminer_infra_reference }}
 ADD_ALGORITHM_DIR={{ smartgears_user_home }}/algorithmInstaller
 ADD_ALGORITHM_PATH={{ smartgears_user_home }}/algorithmInstaller/addAlgorithm.sh
-ALGORITHMS_FILE={{ dataminer_wps_algorithms_dest }}/${INFRA_REFERENCE}/algorithms
+ALGORITHMS_FILE="{{ dataminer_wps_algorithms_dest }}/${INFRA_REFERENCE}/algorithms"
 OUT_DIR=$( mktemp -d -t algorithms-updater.XXXXXXXXXX )
 ALGORITHMS_TEMP_SCRIPT=$OUT_DIR/add_algorithms
 LOG_DIR={{ smartgears_user_home }}/wps_algorithms_install_log
@@ -19,6 +19,8 @@ ALGO_BASE_DIR={{ dataminer_wps_algorithms_dest }}
 ALGO_DIR=${ALGO_BASE_DIR}/${INFRA_REFERENCE}
 SVN_ALGORITHMS_URL={{ dataminer_wps_algorithms_svn }}
 SVN_UPDATE_STATUS=
+SVN_ALGORITHMS_NUM=
+INSTALLED_ALGORITHMS_NUM=
 # In seconds. 60*60*6=21600s (6h)
 UPDATER_PROCESS_MAX_RUNTIME=21600
 OLDPROC=
@@ -206,13 +208,17 @@ update_svn_repo() {
 create_log_dir
 check_lock_file
 update_svn_repo
+SVN_ALGORITHMS_NUM=$( wc -l "$ALGORITHMS_FILE" | awk '{print $1}' )
+INSTALLED_ALGORITHMS_NUM=$( wc -l "$ALGORITHMS_INSTALLED_FILE" | awk '{print $1}' )
 if [ $SVN_UPDATE_STATUS -ne 0 ] ; then
     if [ -d "${LOG_DIR}" ] ; then
         if [ ! -f "$ALGORITHMS_INSTALLED_FILE" ] || [ ! -s "$ALGORITHMS_INSTALLED_FILE" ] ; then
-                logger "algorithms-updater: nothing new to from SVN but the file that lists the installed algorithms does not exist or is empty. We are going to reinstall all the algorithms."
-            else
-                logger "algorithms-updater: nothing new to from SVN, exiting."
-                cleanup 0
+            logger "algorithms-updater: nothing new to from SVN but the file that lists the installed algorithms does not exist or is empty. We are going to reinstall all the algorithms."
+        elif [ $SVN_ALGORITHMS_NUM -gt $INSTALLED_ALGORITHMS_NUM ] ; then
+            logger "algorithms-updater: nothing new to from SVN but the installed algorithms are less than the expected number. We are going to run the installer."
+        else
+            logger "algorithms-updater: nothing new to from SVN and all the algorithms are installed, exiting."
+            cleanup 0
         fi 
     fi
 fi