#!/bin/bash

# koha-zebra - Manage Zebra daemons for Koha instances
#              Copyright 2016 Theke Solutions
#              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

. /lib/lsb/init-functions

# Read configuration variable file if it is present
[ -r /etc/default/koha-common ] && . /etc/default/koha-common

# 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

usage()
{
    local scriptname=$(basename $0)

    cat <<EOF
$scriptname

This script lets you manage the Zebra daemon for your Koha instances.

Usage:
$scriptname [--start|--stop|--restart] instancename1 [instancename2...]
$scriptname -h|--help

    --start               Start the Zebra daemon for the specified instance(s)
    --stop                Stop the Zebra daemon for the specified instance(s)
    --restart             Restart the Zebra daemon for the specified instance(s)
    --status              Show the status of the Zebra daemon for the specified instance(s)
    --enable              Enable the Zebra daemon for the specified instance(s)
    --disable             Disable the Zebra daemon for the specified instance(s)
    --verbose|-v          Display progress and actions messages
    --help|-h             Display this help message

EOF
}

start_zebra()
{
    local name=$1
    if [ "$(koha_init_backend)" = "systemd" ]; then
        koha_service_ctl start zebra "$name"
    else
        _sysv_start_zebra "$name"
    fi
}

stop_zebra()
{
    local name=$1
    if [ "$(koha_init_backend)" = "systemd" ]; then
        koha_service_ctl stop zebra "$name"
    else
        _sysv_stop_zebra "$name"
    fi
}

restart_zebra()
{
    local name=$1
    if [ "$(koha_init_backend)" = "systemd" ]; then
        koha_service_ctl restart zebra "$name"
    else
        _sysv_restart_zebra "$name"
    fi
}

enable_zebra()
{
    local name=$1
    if [ "$(koha_init_backend)" = "systemd" ]; then
        koha_service_ctl enable zebra "$name"
    fi
}

disable_zebra()
{
    local name=$1
    if [ "$(koha_init_backend)" = "systemd" ]; then
        koha_service_ctl disable zebra "$name"
    fi
}

zebra_status()
{
    local name=$1
    if [ "$(koha_init_backend)" = "systemd" ]; then
        koha_service_ctl status zebra "$name"
    else
        _sysv_zebra_status "$name"
    fi
}


set_action()
{
    if [ "$op" = "" ]; then
        op=$1
    else
        die "Error: only one action can be specified."
    fi
}

op=""
verbose="no"

# Backwards compatible with old koha-*-zebra scripts
# TODO: Remove once there's consensus to remove the legacy scripts
used_script_name=$(basename $0)

if [ "$used_script_name" != "koha-zebra" ]; then
    warn "Deprecated script used (${used_script_name})"

    case "$used_script_name" in
        koha-start-zebra)
            set_action "start" ;;
        koha-stop-zebra)
            set_action "stop" ;;
        koha-restart-zebra)
            set_action "restart" ;;
        koha-enable-zebra)
            set_action "enable" ;;
        koha-disable-zebra)
            set_action "disable" ;;
        *)
            break ;;
    esac
fi
# / Backwards compatible handling code

# Read command line parameters
while [ $# -gt 0 ]; do

    case "$1" in
        -h|--help)
            usage ; exit 0 ;;
        -v|--verbose)
            verbose="yes"
            shift ;;
        --start)
            set_action "start"
            shift ;;
        --stop)
            set_action "stop"
            shift ;;
        --restart)
            set_action "restart"
            shift ;;
        --enable)
            set_action "enable"
            shift ;;
        --disable)
            set_action "disable"
            shift ;;
        --status)
            set_action "status"
            shift ;;
        -*)
            die "Error: invalid option switch ($1)" ;;
        *)
            # We expect the remaining stuff are the instance names
            break ;;
    esac

done

ZEBRA_DAEMON=$(which zebrasrv)

if [ $# -gt 0 ]; then
    # We have at least one instance name
    for name in "$@"; do

        if is_instance $name; then

            case $op in
                "start")
                    start_zebra $name
                    ;;
                "stop")
                    stop_zebra $name
                    ;;
                "restart")
                    restart_zebra $name
                    ;;
                "enable")
                    enable_zebra $name
                    ;;
                "disable")
                    disable_zebra $name
                    ;;
                "status")
                    zebra_status $name
            esac

        else
            if [ "$verbose" != "no" ]; then
                log_daemon_msg "Error: Invalid instance name $name"
                log_end_msg 1
            fi
        fi

    done
else
    if [ "$verbose" != "no" ]; then
        warn "Error: you must provide at least one instance name"
    fi
fi

exit 0
