#!/bin/bash
# koha-systemd-ctl - Helper for managing Koha instances via systemd

set -e

usage() {
    cat <<EOF
Usage: $(basename $0) <command> <instance> [service]

Commands:
  enable <instance>          Enable all services for instance
  disable <instance>         Disable all services for instance
  start <instance>           Start all services for instance
  stop <instance>            Stop all services for instance
  restart <instance>         Restart all services for instance
  status <instance>          Show status of all services for instance

  enable <instance> <svc>    Enable specific service for instance
  disable <instance> <svc>   Disable specific service for instance
  start <instance> <svc>     Start specific service for instance
  stop <instance> <svc>      Stop specific service for instance
  restart <instance> <svc>   Restart specific service for instance
  status <instance> <svc>    Show status of specific service for instance

  sync-es-indexers           Enable and start the ES indexer for every
                             instance configured to use Elasticsearch

Services: plack, zebra, sip, z3950, worker, worker-long_tasks, indexer, es-indexer

Examples:
  $(basename $0) enable library
  $(basename $0) start library plack
  $(basename $0) status library
  $(basename $0) restart library worker
EOF
    exit 1
}

if [ "${1:-}" = "sync-es-indexers" ]; then
    # Which search engine an instance uses is plain koha-conf.xml state, so
    # switching an instance to Elasticsearch never runs 'systemctl enable'.
    # Re-discover ES instances here, like the SysV init script did on every
    # start.
    for instance in $(koha-list --enabled --elasticsearch 2>/dev/null); do
        systemctl enable --now "koha-es-indexer@${instance}.service" || true
    done
    exit 0
fi

[ $# -lt 2 ] && usage

CMD=$1
INSTANCE=$2
SERVICE=${3:-}

if [ -n "$SERVICE" ]; then
    UNIT="koha-${SERVICE}@${INSTANCE}.service"
else
    UNIT="koha@${INSTANCE}.target"
fi

case "$CMD" in
    status)
        systemctl --no-pager "$CMD" "$UNIT"
        ;;
    enable|disable|start|stop|restart)
        systemctl "$CMD" "$UNIT"
        ;;
    *)
        echo "Unknown command: $CMD"
        usage
        ;;
esac
