#!/bin/bash # tmrw installer — run it with: curl -fsSL cli.tmrw.nz | bash # Shows the logo, asks for the studio password and runs the setup it unlocks set -e MAX_ATTEMPTS=3 BOX_WIDTH=44 LABEL="Please enter your password:" LABEL_INDENT=3 CLI_URL="${TMRW_CLI_URL:-https://cli.tmrw.nz}" logo() { printf '\n\033[1m%s\033[0m\n\n' "$(cat <<'ART' ⢸⡇ ⠛⢻⡟⠛⠛⠃⣠⡾⠛⠛⠛⢶⡄ ⣿⣴⠞⠛⠻⣦⡴⠛⠛⠻⣦ ⢠⡶⠛⠛⠛⢷⣄ ⢸⣧⠾⠛⠃⢸⣧⠶⠛⠛⢠⡶⠛⠛⠛⢳⣆⠈⣿⡀ ⣼⣷ ⣾⠃ ⢸⡇ ⢰⣿ ⠘⣿ ⣿⡇ ⣿⠃ ⣿ ⣿⡇ ⣿⡆⢸⡏ ⢸⡟ ⣿⡇ ⣿⡆⠸⣧ ⢰⡏⢸⣇ ⣸⡏ ⢸⡇ ⠈⣿⡀ ⢰⡿ ⣿ ⣿ ⣿ ⢿⡇ ⢀⣿⠃⢸⡇ ⢸⡇ ⢿⣇ ⢀⣿⠃ ⢻⡆⣿⠁ ⢿⣤⡿⢀⣤⣄ ⠘⠷⠶⠶⠆⠈⠻⠶⠶⠶⠛⠁ ⠿ ⠿ ⠿ ⠈⠛⠶⠶⠶⠟⠁ ⠸⠇ ⠸⠇ ⠈⠛⠶⠶⠶⠟⠃ ⠈⠿⠇ ⠘⠿⠃⠘⠯⠟ ART )" } # Draw the box, then read the password inside it as dots; curl owns stdin, so read the terminal ask_password() { local border char dots=0 local after_label=$((BOX_WIDTH - LABEL_INDENT - ${#LABEL})) local max_dots=$((after_label - 2)) border="$(printf '─%.0s' $(seq 1 "$BOX_WIDTH"))" printf ' ╭%s╮\n │%*s│\n │%*s\033[1m%s\033[0m%*s│\n │%*s│\n ╰%s╯\n' \ "$border" "$BOX_WIDTH" "" "$LABEL_INDENT" "" "$LABEL" "$after_label" "" "$BOX_WIDTH" "" "$border" > /dev/tty printf '\033[3A\r\033[%dC' $((3 + LABEL_INDENT + ${#LABEL} + 1)) > /dev/tty password="" while IFS= read -rsn1 char < /dev/tty; do case "$char" in "") break ;; $'\177'|$'\b') [ -z "$password" ] && continue password="${password%?}" if [ "$dots" -gt 0 ]; then printf '\b \b' > /dev/tty dots=$((dots - 1)) fi ;; *) password="$password$char" if [ "$dots" -lt "$max_dots" ]; then printf '•' > /dev/tty dots=$((dots + 1)) fi ;; esac done printf '\033[3B\r' > /dev/tty } # Fetch the setup script with the password; prints the HTTP status fetch_setup() { curl -sS -u "tmrw:$password" -o "$1" -w '%{http_code}' "$CLI_URL/install" || echo "000" } main() { local setup_file attempt status setup_file="$(mktemp -t tmrw-setup)" trap 'rm -f "$setup_file"' EXIT logo for attempt in $(seq 1 "$MAX_ATTEMPTS"); do ask_password status="$(fetch_setup "$setup_file")" case "$status" in 200) printf '\n' bash "$setup_file" < /dev/tty return ;; 401) printf ' \033[31mWrong password\033[0m%s\n\n' "$([ "$attempt" -lt "$MAX_ATTEMPTS" ] && echo ' — try again')" ;; 429) printf ' \033[31mToo many attempts — wait a minute and run it again\033[0m\n'; exit 1 ;; *) printf ' \033[31mCouldn'"'"'t reach %s (HTTP %s) — check your connection\033[0m\n' "$CLI_URL" "$status"; exit 1 ;; esac done exit 1 } main "$@"