#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash -p systemdMinimal

usage() {
  echo "swaync-service-dispatcher <action> <service>"
  echo ""
  echo "actions:"
  echo "- print <service>"
  echo "  prints 'true' or 'false' based on if the service is wanted-up (i.e. started or starting)"
  echo "- up <service>"
  echo "- down <service>"
  echo "- toggle <service>"
  exit 1
}

action="$1"
service="$2"

log() {
  if [ -n "$SWAYNC_DEBUG" ]; then
    printf "%s\n" "$1"
  fi
}

checkActive() {
  systemctl is-active "$service" > /dev/null && echo true || echo false
}
startService() {
  log "startService: $service"
  systemctl start "$service"
}
stopService() {
  log "stopService: $service"
  systemctl stop "$service"
}

case "$action" in
  (print)
    checkActive
    ;;
  (toggle)
    case "$(checkActive)" in
      false)
        startService
        ;;
      true)
        stopService
        ;;
    esac
    ;;
  # these aren't needed by swaync; just handy for testing/debugging
  (up)
    startService
    ;;
  (down)
    stopService
    ;;
  (*)
    usage
    ;;
esac
