#<!--
#!/bin/sh
#
# ccxt-qr-term.sh — print a scannable QR code for https://ccxt.com in the
# terminal, with the pixelated CCXT logo drawn from actual QR modules
# in the center.
#
# Local usage:
#   ./ccxt-qr-term.sh                # white letters on black tile (default)
#   ./ccxt-qr-term.sh -i             # inverted: black letters on white tile
#   ./ccxt-qr-term.sh "https://ccxt.com/survey"
#   ./ccxt-qr-term.sh -i "https://ccxt.com/survey"
#
# Remote usage (pipe straight to sh, no download needed):
#   curl -fsSL https://ccxt.com/qr.sh | sh
#   curl -fsSL https://ccxt.com/qr.sh | sh -s -- -i
#   curl -fsSL https://ccxt.com/qr.sh | sh -s -- -i "https://ccxt.com/survey"
#
#   Note: when piping, the script itself occupies the stdin of sh, so any
#   arguments must go through `sh -s -- <args>`. Everything internal reads
#   from its own pipes, so piped invocation behaves identically to a local run.
#
# Requirements (macOS):  brew install qrencode   (perl ships with macOS)
# POSIX sh compatible — runs under sh, dash, bash, and zsh.
#
set -eu

INVERT=0
while [ $# -gt 0 ]; do
  case "$1" in
    -i|--invert) INVERT=1; shift ;;
    *) break ;;
  esac
done
URL="${1:-https://ccxt.com}"

command -v qrencode >/dev/null 2>&1 || {
  echo "qrencode not found. Install with: brew install qrencode" >&2; exit 1;
}

# -l H = ~30% error correction. The 11x11 logo destroys 121 of the 841
# modules of the natural 29x29 symbol for this URL (~14.4%) — within budget.
# Longer payloads auto-upgrade to bigger symbols, restoring headroom.
# ASCII output: each module is 2 chars, "##" = dark, "  " = light.
# POSIX sh has no pipefail, so capture qrencode output first to catch failures.
qr_ascii=$(qrencode -l H -m 2 -t ASCII "$URL") || {
  echo "qrencode failed for payload: $URL" >&2; exit 1;
}
export INVERT
printf '%s\n' "$qr_ascii" | perl -C -e '
  # --- parse module matrix ---------------------------------------------------
  my @m;
  while (<STDIN>) {
    chomp;
    my @row;
    for (my $i = 0; $i < length($_); $i += 2) {
      push @row, (substr($_, $i, 2) eq "##" ? 1 : 0);
    }
    push @m, \@row;
  }
  my $H = scalar @m;
  my $W = scalar @{$m[0]};

  # --- pixelated CCXT logo, 11x11 modules: "#" = letters/outer border, ------
  # --- "." = surrounding frame. 7x7 letters + 1px frame + 1px border --------
  # Default:   "#" = light (white letters, black frame, white border)
  # --invert:  "#" = dark  (black letters, white frame, black border)
  my @logo = (
    "###########",
    "#.........#",
    "#.###.###.#",
    "#.#...#...#",
    "#.###.###.#",
    "#.........#",
    "#.#.#.###.#",
    "#..#...#..#",
    "#.#.#..#..#",
    "#.........#",
    "###########",
  );
  my $lh = scalar @logo;
  my $lw = length $logo[0];
  my $oy = int(($H - $lh) / 2);
  my $ox = int(($W - $lw) / 2);

  my $inv = $ENV{INVERT} ? 1 : 0;
  for my $y (0 .. $lh - 1) {
    for my $x (0 .. $lw - 1) {
      my $lit = (substr($logo[$y], $x, 1) eq "#") ? 1 : 0;   # 1 = letter pixel
      # letter pixels are light by default (module 0), dark when inverted
      $m[$oy + $y][$ox + $x] = $inv ? $lit : (1 - $lit);
    }
  }

  # --- render as half-blocks, forced white-on-black via ANSI -----------------
  # light module = white, dark module = black (scans in any terminal theme)
  my %px = ("00" => "\x{2588}",   # both light  -> full block
            "01" => "\x{2580}",   # bottom dark -> upper half
            "10" => "\x{2584}",   # top dark    -> lower half
            "11" => " ");         # both dark   -> space
  binmode(STDOUT, ":utf8");
  for (my $y = 0; $y < $H; $y += 2) {
    print "\e[40;97m";
    for my $x (0 .. $W - 1) {
      my $top = $m[$y][$x];
      my $bot = ($y + 1 < $H) ? $m[$y + 1][$x] : 1;  # below the grid: dark, merges with bg
      print $px{"$top$bot"};
    }
    print "\e[0m\n";
  }
'
echo
echo "Encodes: $URL — point your iPhone camera at the screen."

exit 0
-->
[{"data":1,"prerenderedAt":3},["ShallowReactive",2],{},1794229929735]