#!/bin/sh
#
# Examples for the cbor CLI. Each block demonstrates one capability.
# Runnable from the repository root after `cargo build --release`.
#
#   ./examples/test.sh
#
# Expects the binary at ./target/release/cbor (build it first), and the
# fixtures under ./fixtures/.

set -e

CBOR=${CBOR:-./target/release/cbor}
FIX=${FIX:-./fixtures}

run() {
  printf '\n=== %s ===\n' "$1"
  shift
  "$@"
}

# --- Import: JSON/YAML/TOML files -> CBOR ----------------------------
run "json file -> cbor -> json" \
  sh -c "$CBOR import --format=json $FIX/test.json | $CBOR export --format=json"

run "yaml file -> cbor -> yaml" \
  sh -c "$CBOR import --format=yaml $FIX/test.yaml | $CBOR export --format=yaml"

run "toml file -> cbor -> toml" \
  sh -c "$CBOR import --format=toml $FIX/test.toml | $CBOR export --format=toml"

# --- Format auto-detection from file extension -----------------------
run "auto-detected json (no --format)" \
  sh -c "$CBOR import $FIX/test.json | $CBOR export --format=json"

# --- Cross-format conversion -----------------------------------------
run "json -> yaml" \
  sh -c "$CBOR import --format=json $FIX/test.json | $CBOR export --format=yaml"

run "yaml -> toml" \
  sh -c "$CBOR import --format=yaml $FIX/test.yaml | $CBOR export --format=toml"

# --- stdin -----------------------------------------------------------
run "json stdin -> cbor -> json" \
  sh -c "cat $FIX/test.json | $CBOR import --format=json | $CBOR export --format=json"

# --- Multiple files in one stream ------------------------------------
run "two json files -> one cbor stream" \
  sh -c "$CBOR import --format=json $FIX/test.json $FIX/test.json | $CBOR export --format=json"

# --- Custom delimiter ------------------------------------------------
run "comma-delimited json output" \
  sh -c "$CBOR import --format=json $FIX/test.json $FIX/test.json | $CBOR -d=',' export --format=json"

# --- Inspect ---------------------------------------------------------
run "inspect (CDN) a fixture" \
  sh -c "$CBOR import --format=json $FIX/test.json | $CBOR inspect"

run "inspect across multiple input formats" \
  sh -c "$CBOR import $FIX/test.json $FIX/test.yaml | $CBOR inspect"

printf '\nDone.\n'
