From 681c725d94fc0cd45f615c84a0862625d1ff7915 Mon Sep 17 00:00:00 2001
From: Thomas Juerges <4-jurges@users.noreply.git.astron.nl>
Date: Mon, 26 Oct 2020 06:44:18 +0100
Subject: [PATCH] Automatically translate paths beginning in ${HOME} to
 /hosthome

This allows to specify only
    tools/ConfigDb/update_ConfigDb.sh tools/ConfigDb/LOFAR_ConfigDb.json
instead of
    tools/ConfigDb/update_ConfigDb.sh /hosthome/workspace/git/LOFAR2.0/tango/tools/ConfigDb/LOFAR_ConfigDb.json
---
 tools/ConfigDb/load_ConfigDb.sh   | 36 +++++++++++++++++++++++++------
 tools/ConfigDb/update_ConfigDb.sh | 36 +++++++++++++++++++++++++------
 2 files changed, 58 insertions(+), 14 deletions(-)

diff --git a/tools/ConfigDb/load_ConfigDb.sh b/tools/ConfigDb/load_ConfigDb.sh
index cc7123223..6b5fbb11d 100755
--- a/tools/ConfigDb/load_ConfigDb.sh
+++ b/tools/ConfigDb/load_ConfigDb.sh
@@ -1,10 +1,32 @@
-if [ ${#} -ne 1 ]; then
-    echo "You must provide a file name for the TANGO_HOST DB dump!"
-    exit -1
-# Check if the filename begins with /hosthome/, /opt/lofar2.0/tango or /opt/lofar2.0/:
-elif [ ${1:0:10} != /hosthome/ -a ${1:0:20} != /opt/lofar2.0/tango/ -a ${1:0:14} != /opt/lofar2.0/ ]; then
-    echo "You must provide a full file path that begins with one of \"/hosthome/\", \"/opt/lofar2.0/tango/\" or \"/opt/lofar2.0/\".  Why is that?  Because the file will be loaded from within the Docker container and only some of the host's file system directories are mounted in the container."
+function help()
+{
+    echo -e "\nERROR:  ${1}\nYou must provide a file that can be accessed from within the Docker container.  This is possible for files that\n\t- Have a path in \${HOME} or\n\t- Have a full file path that begins with one of \"/hosthome/\", \"/opt/lofar2.0/tango/\" or \"/opt/lofar2.0/\".\n\nWhy is that?  Because the file will be loaded from within the Docker container and only some of the host's file system directories are mounted in the container."
     exit -2
+}
+
+if [ ${#} -eq 1 ]; then
+    file=${1}
+else
+    help "A file name must be provided."
+fi
+
+# Check if the filename begins with /hosthome/, /opt/lofar2.0/tango or
+# /opt/lofar2.0/ or if it is in the ${HOME} directory:
+if [ ${1:0:10} != /hosthome/ -a ${1:0:20} != /opt/lofar2.0/tango/ -a ${1:0:14} != /opt/lofar2.0/ ]; then
+    pushd $(dirname ${file}) >/dev/null
+    full_path=${PWD}
+    popd >/dev/null
+    # Check if the file's directory begins with ${HOME}.  Then it can be
+    # accessed via /hosthome.  The replacement will then shorten the result.
+    home_replaced=${full_path#${HOME}}
+    if [ ${#home_replaced} -ne ${#full_path} ]; then
+        if [ ! -f ${file} ]; then
+            help "The file \"${1}\" does not exist."
+        fi
+
+        # The file can be accessed through /hosthome.  Modify the parameter.
+        file=/hosthome${home_replaced}/$(basename ${file})
+    fi
 fi
 
-docker exec -it ${TANGO_CONTAINER_ENV} dsconfig json2tango --write ${1}
+docker exec -it ${TANGO_CONTAINER_ENV} dsconfig json2tango --write ${file}
diff --git a/tools/ConfigDb/update_ConfigDb.sh b/tools/ConfigDb/update_ConfigDb.sh
index 003ddd7ea..1c6587a0f 100755
--- a/tools/ConfigDb/update_ConfigDb.sh
+++ b/tools/ConfigDb/update_ConfigDb.sh
@@ -1,10 +1,32 @@
-if [ ${#} -ne 1 ]; then
-    echo "You must provide a file name for the TANGO_HOST DB dump!"
-    exit -1
-# Check if the filename begins with /hosthome/, /opt/lofar2.0/tango or /opt/lofar2.0/:
-elif [ ${1:0:10} != /hosthome/ -a ${1:0:20} != /opt/lofar2.0/tango/ -a ${1:0:14} != /opt/lofar2.0/ ]; then
-    echo "You must provide a full file path that begins with one of \"/hosthome/\", \"/opt/lofar2.0/tango/\" or \"/opt/lofar2.0/\".  Why is that?  Because the file will be loaded from within the Docker container and only some of the host's file system directories are mounted in the container."
+function help()
+{
+    echo -e "\nERROR:  ${1}\nYou must provide a file that can be accessed from within the Docker container.  This is possible for files that\n\t- Have a path in \${HOME} or\n\t- Have a full file path that begins with one of \"/hosthome/\", \"/opt/lofar2.0/tango/\" or \"/opt/lofar2.0/\".\n\nWhy is that?  Because the file will be loaded from within the Docker container and only some of the host's file system directories are mounted in the container."
     exit -2
+}
+
+if [ ${#} -eq 1 ]; then
+    file=${1}
+else
+    help "A file name must be provided."
+fi
+
+# Check if the filename begins with /hosthome/, /opt/lofar2.0/tango or
+# /opt/lofar2.0/ or if it is in the ${HOME} directory:
+if [ ${1:0:10} != /hosthome/ -a ${1:0:20} != /opt/lofar2.0/tango/ -a ${1:0:14} != /opt/lofar2.0/ ]; then
+    pushd $(dirname ${file}) >/dev/null
+    full_path=${PWD}
+    popd >/dev/null
+    # Check if the file's directory begins with ${HOME}.  Then it can be
+    # accessed via /hosthome.  The replacement will then shorten the result.
+    home_replaced=${full_path#${HOME}}
+    if [ ${#home_replaced} -ne ${#full_path} ]; then
+        if [ ! -f ${file} ]; then
+            help "The file \"${1}\" does not exist."
+        fi
+
+        # The file can be accessed through /hosthome.  Modify the parameter.
+        file=/hosthome${home_replaced}/$(basename ${file})
+    fi
 fi
 
-docker exec -it ${TANGO_CONTAINER_ENV} dsconfig json2tango --write --update ${1}
+docker exec -it ${TANGO_CONTAINER_ENV} dsconfig json2tango --write --update ${file}
-- 
GitLab