#!/usr/bin/env bash
# sanguis: the sanguis command.  sanguis bleed prog.sanguis [out.bc]   compile
#                                sanguis drip prog.bc [cell]           run (a .sanguis file is compiled first)
#                                sanguis smoke                         check the whole kit on this machine
#                                sanguis version                       which toolchain this is
set -e
ROOT="$(cd "$(dirname "$(readlink -f "$0")")/.." && pwd)"
case "$1" in
  bleed)
    [ -n "$2" ] || { echo "sanguis bleed: name a .sanguis file" >&2; exit 2; }
    OUT="${3:-${2%.sanguis}.bc}"
    bash "$ROOT/tools/sg.sh" "$2" "$OUT"
    echo "bled $(basename "$2") into $OUT" ;;
  drip)
    [ -n "$2" ] || { echo "sanguis drip: name a .bc or .sanguis file" >&2; exit 2; }
    PROG="$2"
    if [ "${2##*.}" = "sanguis" ]; then PROG="$(mktemp --suffix=.bc)"; bash "$ROOT/tools/sg.sh" "$2" "$PROG"; fi
    exec "$ROOT/tools/bin/bch" "$PROG" "${3:-main}" ;;
  smoke) exec bash "$ROOT/_smoke.sh" ;;
  version) cat "$ROOT/VERSION" ;;
  *) echo "sanguis: bleed prog.sanguis [out.bc]   compile Sanguis to BloodCode"
     echo "         drip prog.bc [cell]           run a program (cell: main unless named; a .sanguis file is compiled first)"
     echo "         smoke                         check the whole kit on this machine"
     echo "         version                       which toolchain this is"
     [ -z "$1" ] || exit 2 ;;
esac
