#!/bin/sh
# still.fail's command line, from a clone or from an installed release (~/.stillfail/app, see cloud/src/install.ts).
# `ember`, its name before the rename, is a link to it (bin/ember, ~/.local/bin/ember).
#   stillfail start [--port N]                          runs the station (the release's service does this):
#                                                       stillfail-station, whose loopback port (4760, or a free one when
#                                                       that is taken; --port insists on one) sends links to the page it
#                                                       once had on to still.fail cloud
#   stillfail status                                    where the station is: its workspace and cloud, whether it is
#                                                       online, and why it does no work when it does none
#   stillfail update [--beta|--stable]                  updates this station to its cloud's latest release, on its
#                                                       channel (stable, or the test channel's); --beta/--stable puts
#                                                       it on that channel first (back to the stable release even when
#                                                       that is older than the beta it runs)
#   stillfail station enroll <cloud-origin> <token>     joins a workspace in still.fail cloud
#   stillfail station id
# The data is in $STILLFAIL_DATA (else $EMBER_DATA), default ~/.stillfail; ~/.ember is the old default, which the
# station moves there on its first start (mesh/app/src/former.rs).
set -eu
# Called through a link (~/.local/bin/stillfail): the app is where the link points.
self=$(readlink -f "$0" 2>/dev/null || echo "$0")
here=$(cd "$(dirname "$self")/.." && pwd)
data=${STILLFAIL_DATA:-${EMBER_DATA:-$HOME/.stillfail}}
[ "$data" = "$HOME/.ember" ] && data="$HOME/.stillfail"
station=${STILLFAIL_STATION_BIN:-${EMBER_STATION_BIN:-$here/mesh/target/release/stillfail-station}}
case "${1:-} ${2:-}" in
  "start "*) shift; exec "$station" run --app "$here" --data "$data" "$@" ;;
  "update "*)
    # From the cloud the station is in (its installer, without a token, updates). Before the station first ran,
    # its data may still be at the old place.
    cloud="$data/mesh/cloud.json"
    [ -f "$cloud" ] || cloud="$HOME/.ember/mesh/cloud.json"
    origin=$(sed -n 's/.*"origin": *"\([^"]*\)".*/\1/p' "$cloud" 2>/dev/null | head -1)
    [ -n "$origin" ] || { echo "这台 station 还没有加入 workspace，无从更新" >&2; exit 1; }
    # Its channel, set first when named (the station's last line says it); a station that cannot say (a clone not
    # built) takes the stable one.
    case "${2:-}" in
      --beta) set -- beta ;;
      --stable) set -- stable ;;
      "") set -- ;;
      *) echo "usage: stillfail update [--beta|--stable]" >&2; exit 2 ;;
    esac
    channel=$("$station" channel "$@" --app "$here" --data "$data" 2>/dev/null | tail -n 1) || true
    case "$channel" in
      stable|beta) ;;
      *) [ $# -eq 0 ] || { echo "没能把 station 设成 $1 渠道" >&2; exit 1; }; channel=stable ;;
    esac
    curl -fsSL "$origin/install.sh" | STILLFAIL_CHANNEL="$channel" sh ;;
  "station enroll") shift 2; exec "$station" enroll "$@" --data "$data" ;;
  "status "*|"station status") exec "$station" status --data "$data" ;;
  "station id") exec "$station" id --data "$data" ;;
  *) echo "usage: stillfail start [--port N] | stillfail status | stillfail update [--beta|--stable] | stillfail station enroll <cloud-origin> <token> | stillfail station id" >&2; exit 2 ;;
esac
