#!/bin/bash
#
# Requires python3. Reads JSON on stdin; outputs YAML on stdout.

set -e

PYTHON=python3

function fail() {
  echo > /dev/stderr
  echo "$*" > /dev/stderr
  exit 1
}

$PYTHON -c '' || fail "Could not find Python 3 at \"$PYTHON\". Do you have it installed?"
$PYTHON -c 'import yaml' || fail "Could not import yaml in Python; try running 'pip3 install pyyaml'"

if [ -t 0 ]; then
  echo "Please input the JSON on standard input." > /dev/stderr
  echo > /dev/stderr
fi

python3 -c "import sys, yaml, json; print(yaml.dump(json.loads(sys.stdin.read())))"
