From d84bbbbdeb7271466ae42b62401476348cb99853 Mon Sep 17 00:00:00 2001
From: Mark Tompsett <mtompset@hotmail.com>
Date: Mon, 28 May 2018 16:45:29 +0000
Subject: [PATCH] Bug 20692: Extreme toggle checking for plack

TEST PLAN
---------
sudo koha-plack --disable
sudo koha-plack --disable
sudo koha-plack --enable

sudo koha-plack --disable
echo $?
sudo koha-plack --disable
echo $?
sudo koha-list --noplack
echo $?
sudo koha-plack --enable
echo $?
sudo koha-plack --enable
echo $?
sudo koha-list --plack
echo $?

git bz apply 20692
sudo perl ~/misc4dev/cp_debian_files.pl
then repeat the test.

It should also be noted, because the enable
and disable are now split, if for some reason
they become out of sync, an enable or disable
should correct them.

Because these are bash scripts, the test tool
(/home/vagrant/qa-test-tools/koha-qa.pl)
does not apply.

Signed-off-by: Liz Rea <wizzyrea@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 debian/scripts/koha-functions.sh | 54 ++++++++++++++++++--
 debian/scripts/koha-plack        | 84 +++++++++++++++++++++++++++++---
 2 files changed, 126 insertions(+), 12 deletions(-)

diff --git a/debian/scripts/koha-functions.sh b/debian/scripts/koha-functions.sh
index 3a04db7851..899feba5fe 100755
--- a/debian/scripts/koha-functions.sh
+++ b/debian/scripts/koha-functions.sh
@@ -199,6 +199,40 @@ is_indexer_running()
     fi
 }
 
+is_plack_enabled_opac()
+{
+    local instancefile=$1
+
+    if [ "$instancefile" = "" ]; then
+        return 1
+    fi
+
+    # remember 0 means success/true in bash.
+    if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-opac-plack.conf' \
+            "$instancefile" ; then
+        return 0
+    else
+        return 1
+    fi
+}
+
+is_plack_enabled_intranet()
+{
+    local instancefile=$1
+
+    if [ "$instancefile" = "" ]; then
+        return 1
+    fi
+
+    # remember 0 means success/true in bash.
+    if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-intranet-plack.conf' \
+            "$instancefile" ; then
+        return 0
+    else
+        return 1
+    fi
+}
+
 is_plack_enabled()
 {
     local site=$1
@@ -208,10 +242,22 @@ is_plack_enabled()
         return 1
     fi
 
-    if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-opac-plack.conf' \
-            "$instancefile" && \
-       grep -q '^[[:space:]]*Include /etc/koha/apache-shared-intranet-plack.conf' \
-            "$instancefile" ; then
+    if is_plack_enabled_opac $instancefile ; then
+        enabledopac=1
+    else
+        enabledopac=0
+    fi
+    if is_plack_enabled_intranet $instancefile ; then
+        enabledintra=1
+    else
+        enabledintra=0
+    fi
+
+    # remember 0 means success/true in bash.
+    if [ "$enabledopac" != "$enabledintra" ] ; then
+        echo "$site has a plack configuration error. Enable or disable plack to correct this."
+        return 0
+    elif [ "$enabledopac" = "1" ] ; then
         return 0
     else
         return 1
diff --git a/debian/scripts/koha-plack b/debian/scripts/koha-plack
index 68f899113b..b14bac5f9f 100755
--- a/debian/scripts/koha-plack
+++ b/debian/scripts/koha-plack
@@ -181,14 +181,48 @@ enable_plack()
     local instancename=$1
     local instancefile=$(get_apache_config_for "$instancename")
 
-    if ! is_plack_enabled $instancename; then
-        # Uncomment the plack related lines for OPAC and intranet
+    alreadyopac=0
+    alreadyintra=0
+    failopac=0
+    failintra=0
+    if ! is_plack_enabled_opac $instancefile; then
+        # Uncomment the plack related lines for OPAC
         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:\1:' "$instancefile"
+        if ! is_plack_enabled_opac $instancefile; then
+            [ "${quiet}" != "yes" ] && warn "Plack not enabled for ${instancename} OPAC"
+            failopac=1
+        else
+            [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename} OPAC"
+        fi
+    else
+        [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename} OPAC"
+        alreadyopac=1
+    fi
+    if ! is_plack_enabled_intranet $instancefile; then
+        # Uncomment the plack related lines for intranet
         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:\1:' "$instancefile"
-        [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename}"
+        if ! is_plack_enabled_intranet $instancefile; then
+            [ "${quiet}" != "yes" ] && warn "Plack not enabled for ${instancename} Intranet"
+            failintra=1
+        else
+            [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename} Intranet"
+        fi
+    else
+        [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename} Intranet"
+        alreadyintra=1
+    fi
+
+    # Fail if it was already plack enabled.
+    if [ $alreadyopac -eq 1 ] && [ $alreadyintra -eq 1 ] ; then
+        return 1
+    elif [ "$alreadyopac" != "$alreadyintra" ]; then
+        [ "${quiet}" != "yes" ] && warn "$instancename had a plack configuration error. Please confirm it is corrected."
+    fi
+
+    # Succeed if both or any plack were turned on.
+    if [ $failopac -eq 0 ] ||  [ $failintra -eq 0 ] ; then
         return 0
     else
-        [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename}"
         return 1
     fi
 }
@@ -198,14 +232,48 @@ disable_plack()
     local instancename=$1
     local instancefile=$(get_apache_config_for "$instancename")
 
-    if is_plack_enabled $instancename; then
-        # Comment out the plack related lines for OPAC and intranet
+    alreadyopac=0
+    alreadyintra=0
+    failopac=0
+    failintra=0
+    if is_plack_enabled_opac $instancefile ; then
+        # Comment the plack related lines for OPAC
         sed -i 's:^\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:#\1:' "$instancefile"
+        if is_plack_enabled_opac $instancefile ; then
+            [ "${quiet}" != "yes" ] && warn "Plack not disabled for ${instancename} OPAC"
+            failopac=1
+        else
+            [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename} OPAC"
+        fi
+    else
+        [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename} OPAC"
+        alreadyopac=1
+    fi
+    if is_plack_enabled_intranet $instancefile; then
+        # Comment the plack related lines for intranet
         sed -i 's:^\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:#\1:' "$instancefile"
-        [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename}"
+        if is_plack_enabled_intranet $instancefile; then
+            [ "${quiet}" != "yes" ] && warn "Plack not disabled for ${instancename} Intranet"
+            failintra=1
+        else
+            [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename} Intranet"
+        fi
+    else
+        [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename} Intranet"
+        alreadyintra=1
+    fi
+
+    # Fail if it was already plack disabled.
+    if [ $alreadyopac -eq 1 ] &&  [ $alreadyintra -eq 1 ] ; then
+        return 1
+    elif [ "$alreadyopac" != "$alreadyintra" ]; then
+        [ "${quiet}" != "yes" ] && warn "$instancename had a plack configuration error. Please confirm it is corrected."
+    fi
+
+    # Succeed if both or any plack were turned off.
+    if  [ $failopac -eq 0 ] || [ $failintra -eq 0 ] ; then
         return 0
     else
-        [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename}"
         return 1
     fi
 }
-- 
2.20.1