Bug 16733: Adjust koha-indexer
[koha.git] / debian / scripts / koha-list
index 1e19f1a..0dd72e1 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# koha-instances -- List all Koha instances.
+# koha-list -- List all Koha instances.
 # Copyright 2010  Catalyst IT, Ltd
 # 
 # This program is free software: you can redistribute it and/or modify
 
 set -e
 
-is_enabled() {
-    if grep '^[[:space:]]*Include /etc/koha/apache-shared-disable.conf' \
-            "/etc/apache2/sites-available/$name" > /dev/null
-    then
-        return 1
+# include helper functions
+if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
+    . "/usr/share/koha/bin/koha-functions.sh"
+else
+    echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
+    exit 1
+fi
+
+show_instances()
+{
+    local show=$1
+    local show_email=$2
+    local show_sip=$3
+
+    for instance in $( get_instances ); do
+        case $show in
+          "all")
+              if instance_filter_email       $instance $show_email && \
+                 instance_filter_letsencrypt $instance $show_letsencrypt && \
+                 instance_filter_plack       $instance $show_plack && \
+                 instance_filter_sip         $instance $show_sip; then
+                    echo $instance
+              fi ;;
+          "enabled")
+              if is_enabled $instance; then
+                  if instance_filter_email       $instance $show_email && \
+                     instance_filter_letsencrypt $instance $show_letsencrypt && \
+                     instance_filter_plack       $instance $show_plack && \
+                     instance_filter_sip         $instance $show_sip; then
+                      echo $instance
+                  fi
+              fi ;;
+          "disabled")
+              if ! is_enabled $instance; then
+                  if instance_filter_email       $instance $show_email && \
+                     instance_filter_letsencrypt $instance $show_letsencrypt && \
+                     instance_filter_plack       $instance $show_plack && \
+                     instance_filter_sip         $instance $show_sip; then
+                      echo $instance
+                  fi
+              fi ;;
+        esac
+    done
+}
+
+
+instance_filter_sip()
+{
+    local instancename=$1
+    local show_sip=$2;
+
+    case $show_sip in
+        "all")
+            return 0 ;;
+        "enabled")
+            if is_sip_enabled $instancename; then
+                return 0
+            fi ;;
+        "disabled")
+            if ! is_sip_enabled $instancename; then
+                return 0
+            fi ;;
+    esac
+
+    # Didn't match any criteria
+    return 1
+}
+
+instance_filter_plack()
+{
+    local instancename=$1
+    local show_plack=$2;
+
+    case $show_plack in
+        "all")
+            return 0 ;;
+        "enabled")
+            if is_plack_enabled $instancename; then
+                return 0
+            fi ;;
+        "disabled")
+            if ! is_plack_enabled $instancename; then
+                return 0
+            fi ;;
+    esac
+
+    # Didn't match any criteria
+    return 1
+}
+
+instance_filter_letsencrypt()
+{
+    local instancename=$1
+    local show_letsencrypt=$2;
+
+    case $show_letsencrypt in
+        "all")
+            return 0 ;;
+        "enabled")
+            if is_letsencrypt_enabled $instancename; then
+                return 0
+            fi ;;
+        "disabled")
+            if ! is_letsencrypt_enabled $instancename; then
+                return 0
+            fi ;;
+    esac
+
+    # Didn't match any criteria
+    return 1
+}
+
+instance_filter_email()
+{
+    local instancename=$1
+    local show_email=$2;
+
+    case $show_email in
+        "all")
+            return 0 ;;
+        "enabled")
+            if is_email_enabled $instancename; then
+                return 0
+            fi ;;
+        "disabled")
+            if ! is_email_enabled $instancename; then
+                return 0
+            fi ;;
+    esac
+
+    # Didn't match any criteria
+    return 1
+}
+
+set_show()
+{
+    local show_param=$1
+
+    if [ "$show" = "all" ]; then
+        show=$show_param
     else
-        return 0
+        die "Error: --enabled and --disabled are mutually exclusive."
     fi
 }
 
-help() {
-    echo <<eoh
+set_show_email()
+{
+    local email_param=$1
+
+    if [ "$show_email" = "all" ]; then
+        show_email=$email_param
+    else
+        die "Error: --email and --noemail are mutually exclusive."
+    fi
+}
+
+set_show_letsencrypt()
+{
+    local letsencrypt_param=$1
+
+    if [ "$show_letsencrypt" = "all" ]; then
+        show_letsencrypt=$letsencrypt_param
+    else
+        die "Error: --letsencrypt and --noletsencrypt are mutually exclusive."
+    fi
+}
+
+set_show_plack()
+{
+    local plack_param=$1
+
+    if [ "$show_plack" = "all" ]; then
+        show_plack=$plack_param
+    else
+        die "Error: --plack and --noplack are mutually exclusive."
+    fi
+}
+
+set_show_sip()
+{
+    local sip_param=$1
+
+    if [ "$show_sip" = "all" ]; then
+        show_sip=$sip_param
+    else
+        die "Error: --sip and --nosip are mutually exclusive."
+    fi
+}
+
+usage()
+{
+    local scriptname=$0
+
+    cat <<EOH
 Lists Koha instances, optionally only those that are enabled or have
 email turned on.
     
-Usage: $0 [--enabled] [--email] [-h]
+Usage: $scriptname [--enabled|--disabled] [--email|--noemail] [--sip|--nosip] [-h]
 Options:
-    --enabled       only show instances that are enabled
-    --email         only show instances that have email enabled
-    --noemail       only show instances that do not have email enabled
-    -h              this help
+    --enabled       Show enabled instances
+    --disabled      Show disabled instances
+    --email         Show instances with email enabled
+    --noemail       Show instances with email disabled
+    --sip           Show instances with SIP enabled
+    --nosip         Show instances with SIP disabled
+    --plack         Show instances with Plack enabled
+    --noplack       Show instances with Plack disabled
+    --letsencrypt   Show instances with letsencrypt enabled
+    --noletsencrypt Show instances with letsencrypt disabled
+    --help | -h     Show this help
 
 The filtering options can be combined, and you probably want to do this
-(except --email and --noemail, that's just silly.)
-eoh
+(except --email and --noemail, or --enabled and --disabled, that's just silly.)
+EOH
 }
 
-enabled=no
-email=no
-noemail=no
-args=$(getopt -l enabled,email,noemail -o h -n $0 -- "$@")
+show="all"
+show_email="all"
+show_sip="all"
+show_plack="all"
+show_letsencrypt="all"
+
+args=$(getopt -l help,enabled,disabled,email,noemail,sip,nosip,plack,noplack,letsencrypt,noletsencrypt -o h -n $0 -- "$@")
 set -- $args
+
 while [ ! -z "$1" ]
 do
     case "$1" in
-         -h) help; exit;;
-    --email) email=yes;;
-  --enabled) enabled=yes;;
-  --noemail) noemail=yes;;
-          *) break;;
+      -h|--help) usage; exit;;
+        --email) set_show_email "enabled" ;;
+      --noemail) set_show_email "disabled" ;;
+          --sip) set_show_sip "enabled" ;;
+        --nosip) set_show_sip "disabled" ;;
+        --plack) set_show_plack "enabled" ;;
+      --noplack) set_show_plack "disabled" ;;
+  --letsencrypt) set_show_letsencrypt "enabled" ;;
+--noletsencrypt) set_show_letsencrypt "disabled" ;;
+      --enabled) set_show "enabled" ;;
+     --disabled) set_show "disabled" ;;
+              *) break;;
     esac
     shift
 done
 
-find /etc/koha/sites -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | 
-sort |
-while read name
-do
-    [ "$enabled" = yes ] && ! is_enabled "$name" && continue
-    [ "$email" = yes ] && [ ! -e /var/lib/koha/$name/email.enabled ] && continue
-    [ "$noemail" = yes ] && [ -e /var/lib/koha/$name/email.enabled ] && continue
-    echo "$name"
-done
+show_instances $show $show_email $show_sip
+
+exit 0