#!/bin/sh
#
# koha-list -- List all Koha instances.
# Copyright 2010  Catalyst IT, Ltd
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.


set -e

# 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()
{
    for instance in $( get_instances ); do
        case $show in
          "all")
              if filter_ok $instance; then
                  echo $instance
              fi ;;
          "enabled")
              if is_enabled $instance; then
                  if filter_ok $instance; then
                      echo $instance
                  fi
              fi ;;
          "disabled")
              if ! is_enabled $instance; then
                  if filter_ok $instance; then
                      echo $instance
                  fi
              fi ;;
        esac
    done
}

filter_ok()
{
    local instance=$1

    if instance_filter_email         $instance && \
       instance_filter_elasticsearch $instance && \
       instance_filter_letsencrypt   $instance && \
       instance_filter_plack         $instance && \
       instance_filter_z3950         $instance && \
       instance_filter_sip           $instance; then
        return 0;
    else
        return 1;
    fi
}

instance_filter_sip()
{
    local instance=$1

    case $show_sip in
        "all")
            return 0 ;;
        "enabled")
            if is_sip_enabled $instance; then
                return 0
            fi ;;
        "disabled")
            if ! is_sip_enabled $instance; then
                return 0
            fi ;;
    esac

    # Didn't match any criteria
    return 1
}

instance_filter_plack()
{
    local instance=$1

    case $show_plack in
        "all")
            return 0 ;;
        "enabled")
            if is_plack_enabled $instance; then
                return 0
            fi ;;
        "disabled")
            if ! is_plack_enabled $instance; then
                return 0
            fi ;;
    esac

    # Didn't match any criteria
    return 1
}

instance_filter_letsencrypt()
{
    local instance=$1

    case $show_letsencrypt in
        "all")
            return 0 ;;
        "enabled")
            if is_letsencrypt_enabled $instance; then
                return 0
            fi ;;
        "disabled")
            if ! is_letsencrypt_enabled $instance; then
                return 0
            fi ;;
    esac

    # Didn't match any criteria
    return 1
}

instance_filter_email()
{
    local instance=$1

    case $show_email in
        "all")
            return 0 ;;
        "enabled")
            if is_email_enabled $instance; then
                return 0
            fi ;;
        "disabled")
            if ! is_email_enabled $instance; then
                return 0
            fi ;;
    esac

    # Didn't match any criteria
    return 1
}

instance_filter_z3950()
{
    local instance=$1

    case $show_z3950 in
        "all")
            return 0 ;;
        "enabled")
            if is_z3950_enabled $instance; then
                return 0
            fi ;;
        "disabled")
            if ! is_z3950_enabled $instance; then
                return 0
            fi ;;
    esac

    # Didn't match any criteria
    return 1
}

instance_filter_elasticsearch()
{
    local instance=$1

    case $show_elasticsearch in
        "all")
            return 0 ;;
        "enabled")
            if is_elasticsearch_enabled $instance; then
                return 0
            fi ;;
        "disabled")
            if ! is_elasticsearch_enabled $instance; 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
        die "Error: --enabled and --disabled are mutually exclusive."
    fi
}

set_show_elasticsearch()
{
    local elasticsearch_param=$1

    if [ "$show_elasticsearch" = "all" ]; then
        show_elasticsearch=$elasticsearch_param
    else
        die "Error: --elasticsearch and --noelasticsearch are mutually exclusive."
    fi
}

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
}

set_show_z3950()
{
    local z3950_param=$1

    if [ "$show_z3950" = "all" ]; then
        show_z3950=$z3950_param
    else
        die "Error: --z3950 and --noz3950 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: $scriptname [--enabled|--disabled] [--email|--noemail] [--sip|--nosip] [-h]
Options:
    --enabled         Show enabled instances
    --disabled        Show disabled instances
    --elasticsearch   Show instances with Elasticsearch enabled
    --noelasticsearch Show instances with Elasticsearch disabled
    --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
    --z3950           Show instances with Z39.50/SRU enabled
    --noz3950         Show instances with Z39.50/SRU disabled
    --help | -h       Show this help

The filtering options can be combined, and you probably want to do this
(except --email and --noemail, or --enabled and --disabled, that's just silly.)
EOH
}

show="all"
show_elasticsearch="all"
show_email="all"
show_sip="all"
show_plack="all"
show_letsencrypt="all"
show_z3950="all"

args=$(getopt -l help,enabled,disabled,elasticsearch,noelasticsearch,email,noemail,sip,nosip,plack,noplack,letsencrypt,noletsencrypt,z3950,noz3950 -o h -n $0 -- "$@")
set -- $args

while [ ! -z "$1" ]
do
    case "$1" in
        -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" ;;
          --z3950) set_show_z3950 "enabled" ;;
        --noz3950) set_show_z3950 "disabled" ;;
  --elasticsearch) set_show_elasticsearch "enabled" ;;
--noelasticsearch) set_show_elasticsearch "disabled" ;;
        --enabled) set_show "enabled" ;;
       --disabled) set_show "disabled" ;;
                *) break;;
    esac
    shift
done

show_instances

exit 0
