#!/usr/bin/env bash
function check_empty_cmd() {
  local result
  result=$("$@")
  if [[ -z "$result" ]]; then
    echo "  <empty>"
  else
    echo "$result" | awk '{gsub(/^[ \t]+/, ""); print "  " $0}'
  fi
}
if [[ "$1" == "l" ]]; then
  shift
  pijul log --output-format json "$@" | jq -r '.[] | "\(.timestamp | sub("\\.[0-9]{9}Z$"; "") | strptime("%Y-%m-%dT%H:%M:%S") | strftime("%Y-%m-%d %H:%M")) \(.hash[:5]) \(.message) \(.authors | map("@" + .) | join(","))"'

elif [[ "$1" == "d" ]]; then
  echo "TODO: scaffold for a differ"
  echo "  1. add a private git-stash/pop functionality"
  echo "  2. pijul stash"
  echo "  3. create a temporary channel RIGHT"
  echo "  4. switch to the channel RIGHT"
  echo "  5. move (reset?) RIGHT to a hash"
  echo "  6. repeat 3-5 for a LEFT channel"
  echo "  7. diff the files recursively"
  echo "  8. (on exit) move back to old channel, pop, delete LEFT and RIGHT"

elif [[ "$1" == "status" ]]; then
  echo "on channel $(pijul channel | \grep '^\*' | cut -d ' ' -f 2-)"
  echo "remotes:"
  check_empty_cmd pijul remote
  echo "diff --short --untracked:"
  check_empty_cmd pijul diff -s -u

else
  pijul "$@"
fi