#!/bin/bash

# blob:summary=Returns a list of all the available power profiles on the system.
# blob:args=[--active-state]

if [[ ${1:-} != "" && ${1:-} != "--active-state" ]]; then
  echo "Usage: blob-power-list [--active-state]" >&2
  exit 1
fi

service=org.freedesktop.UPower.PowerProfiles
object=/org/freedesktop/UPower/PowerProfiles

profiles_json=$(busctl --json=short get-property "$service" "$object" "$service" Profiles 2>/dev/null) || exit 0
active_profile=$(busctl --json=short get-property "$service" "$object" "$service" ActiveProfile 2>/dev/null | jq -r '.data // ""')

jq -r --arg active "$active_profile" --arg with_state "${1:+1}" '
  .data[].Profile.data
  | if $with_state == "1" then "\(.)\t\(if . == $active then 1 else 0 end)" else . end
' <<<"$profiles_json"
