#!/usr/bin/env sh

jobs_output="$(lpstat -o 2>/dev/null)"
status=$?

if [ "$status" -ne 0 ]; then
  printf '%s\n' '{"text":"󰐪 n/a","class":"disabled","tooltip":"CUPS is unavailable"}'
  exit 0
fi

if [ -z "$jobs_output" ]; then
  printf '%s\n' '{"text":"","class":"idle","tooltip":"No print jobs queued"}'
  exit 0
fi

job_count="$(printf '%s\n' "$jobs_output" | sed '/^[[:space:]]*$/d' | wc -l | tr -d ' ')"

if [ "$job_count" = "1" ]; then
  tooltip="1 print job queued"
else
  tooltip="$job_count print jobs queued"
fi

printf '{"text":"󰐪 %s","class":"active","tooltip":"%s"}\n' "$job_count" "$tooltip"
