#!/bin/sh
# Unix/macOS · monorepo CLI (same flow as bank landing unix pane)
#
# install (once):
#   git + node (LTS) + monorepo build deps
#   git clone https://git.taler.net/taler-typescript-core.git
#   cd taler-typescript-core && ./bootstrap && ./configure && make
#
# run from taler-typescript-core after make (needs: node, wget|curl, grep)
set -eu

command -v node >/dev/null 2>&1 || { echo "install: node (LTS)" >&2; exit 1; }
[ -f packages/taler-wallet-cli/bin/taler-wallet-cli.mjs ] || {
  echo "install/build: run from taler-typescript-core after make" >&2
  exit 1
}
command -v wget >/dev/null 2>&1 || command -v curl >/dev/null 2>&1 || {
  echo "install: wget or curl" >&2
  exit 1
}

# monorepo CLI + private wallet DB (not system package / default home DB)
tw() {
  node packages/taler-wallet-cli/bin/taler-wallet-cli.mjs \
    --wallet-db="${TMPDIR:-/tmp}/taler-wallet-goa.sqlite3" \
    --no-throttle "$@"
}

# mint from bank, register exchange, withdraw GOA:10
(wget -q -O demo-withdraw.json https://bank.hacktivism.ch/intro/demo-withdraw.json || \
  curl -fsS -o demo-withdraw.json https://bank.hacktivism.ch/intro/demo-withdraw.json) && \
tw exchanges add https://exchange.hacktivism.ch/ && \
tw exchanges accept-tos https://exchange.hacktivism.ch/ && \
tw withdraw accept-uri --exchange https://exchange.hacktivism.ch/ \
  "$(grep -o 'taler://[^"]*' demo-withdraw.json)" && \
tw run-until-done && \

# pay orbit-sticker (GOA:2); balance ends at GOA:8
tw handle-uri --yes \
  taler://pay-template/taler.hacktivism.ch/instances/goa-shop/orbit-sticker && \
tw run-until-done && tw balance
