#!/usr/bin/env sh

if command -v system-config-printer >/dev/null 2>&1; then
  exec system-config-printer
fi

if command -v alacritty >/dev/null 2>&1; then
  exec alacritty -e sh -lc '
    while :; do
      clear
      printf "Print Queue\n"
      printf "===========\n\n"

      if ! lpstat -r >/dev/null 2>&1; then
        printf "CUPS scheduler is not running.\n"
      else
        if ! lpstat -o 2>/dev/null | sed "/^[[:space:]]*$/d" | grep . >/dev/null 2>&1; then
          printf "No print jobs queued.\n"
        else
          lpstat -o
        fi
      fi

      printf "\nRefreshing every 3 seconds. Press Ctrl+C to close."
      sleep 3
    done
  '
fi

exec xdg-open http://localhost:631/jobs/
