#!/usr/bin/env bash
# restartsrv <service>...: restart ScalPanel services and wait until they are healthy.
# Services: api worker panel node postgres mariadb pdns mail mail-db webmail-api sshgw backup-store,
# or the names of cPanel's scripts: httpd (reloads the edge), mysql, postgresql, named, exim, dovecot, cpsrvd.
. "$(dirname "$(readlink -f "$0")")/_common"
[ $# -ge 1 ] || usage "<service>..."
list=() edge=""
for s in "$@"; do
  case "$s" in
    httpd|apache|nginx) edge=1 ;;
    mysql|mysqld|mariadb) list+=(mariadb) ;;
    postgresql|postgres) list+=(postgres) ;;
    named|pdns|dns) list+=(pdns) ;;
    exim|dovecot|mail|imap|smtp) list+=(mail) ;;
    cpsrvd|panel) list+=(panel) ;;
    *) list+=("$s") ;;
  esac
done
[ -z "$edge" ] || "$SCALPANELCTL" reload-edge
[ ${#list[@]} -eq 0 ] || exec "$SCALPANELCTL" restart "${list[@]}"
