#!/bin/sh # Customer installer (POSIX shell, runs on the customer's computer; downloads and installs prebuilt files, builds nothing). # # sh install.sh install the latest version from the download site # sh install.sh --version 0.1.0 install one version # sh install.sh --from FILE.tar.gz install from a package file you already have (no network) # sh install.sh --accept-licence accept the licence for an unattended install # sh install.sh --system install into /usr/local/bin (uses sudo when needed) # sh install.sh --no-modify-path do not add ~/.local/bin to your shell startup file # sh install.sh --uninstall remove what this script installed # # Environment: CRUMBTRAIL_DOWNLOAD_BASE (default https://crumbtrail.ai/downloads), # CRUMBTRAIL_INSTALL_DIR (overrides the install folder), CRUMBTRAIL_SHA256 (expected checksum for --from), # CRUMBTRAIL_ACCEPT_LICENCE=1 (accept the licence without a prompt), # CRUMBTRAIL_DOWNLOAD_TOKEN (your personal download token; the command on your account page sets it), # CRUMBTRAIL_NO_MODIFY_PATH=1 (never edit a shell startup file), # CRUMBTRAIL_DOWNLOAD_STALL_SECONDS (default 30: a download slower than 1 KB/s for this long is retried). # Download layout: $BASE/latest, $BASE/// and # $BASE///SHA256SUMS and its signature SHA256SUMS.sig. With --from, a SHA256SUMS file beside the # tarball and its SHA256SUMS.sig are required, and the signature is checked. # --allow-unsigned (local test builds only, not in --help): install from --from without a signed SHA256SUMS list. # Local internal qualification archives (crumbtrail-internal-*) require --from and --allow-internal; paid packages never do. set -eu # Everything runs from main, called on the last line, so a download cut off part way runs nothing. main() { # Public key for release signatures (P-256, base64 DER). scripts/sign-release-sums signs with the matching private key. RELEASE_KEY=MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEuCpr0ojIQuZGqK06mteZ5u0PU7M/heCZeh3f6EN6dpXUdjFaHj2DFwPboDxHJKqY1AwWOLRtdyLCGZVFfORnrg== BASE=${CRUMBTRAIL_DOWNLOAD_BASE:-https://crumbtrail.ai/downloads} edition=${CRUMBTRAIL_EDITION:-free} version= from= system=0 uninstall=0 allow_internal=0 allow_unsigned=0 default_dir=0 accept_licence=${CRUMBTRAIL_ACCEPT_LICENCE:-0} modify_path=1 [ -z "${CRUMBTRAIL_NO_MODIFY_PATH:-}" ] || modify_path=0 MARK_BEGIN='# >>> crumbtrail >>>' MARK_END='# <<< crumbtrail <<<' say() { printf '%s\n' "$*"; } die() { printf 'Crumbtrail install failed: %s\n' "$*" >&2; exit 1; } while [ $# -gt 0 ]; do case $1 in --version) [ $# -ge 2 ] || die "--version needs a number"; version=$2; shift 2 ;; --from) [ $# -ge 2 ] || die "--from needs a file"; from=$2; shift 2 ;; --allow-internal) allow_internal=1; shift ;; --allow-unsigned) allow_unsigned=1; shift ;; --accept-licence) accept_licence=1; shift ;; --system) system=1; shift ;; --no-modify-path) modify_path=0; shift ;; --uninstall) uninstall=1; shift ;; -h|--help) if [ -f "$0" ]; then awk '/^# Local internal/ {exit} NR > 1 {sub(/^# ?/, ""); print}' "$0" else echo "Usage: sh install.sh [--version X.Y.Z] [--from FILE] [--system] [--uninstall]"; echo "Run with no options to install the latest release into ~/.local/bin." fi; exit 0 ;; *) die "unknown option $1 (try --help)" ;; esac done [ "$allow_internal" = 0 ] || [ -n "$from" ] || die "--allow-internal requires a local --from archive" if [ -n "${CRUMBTRAIL_INSTALL_DIR:-}" ]; then bindir=$CRUMBTRAIL_INSTALL_DIR relaydir=$bindir/relay elif [ "$system" = 1 ]; then bindir=/usr/local/bin relaydir=$bindir/relay else [ -n "${HOME:-}" ] || die "HOME is not set" bindir=$HOME/.local/bin relaydir=$HOME/.crumbtrail/relay default_dir=1 fi # Licence and notice files live in a share folder beside the programs, never among them: /share/crumbtrail when the # program folder is /bin, otherwise a crumbtrail-docs folder next to the program folder. The updater uses the same rule. case $(basename "$bindir") in bin) docsdir=$(dirname "$bindir")/share/crumbtrail ;; *) docsdir=$(dirname "$bindir")/crumbtrail-docs ;; esac notices="LICENSE LICENSE.txt LICENSE.md LICENCE LICENCE.txt LICENCE.md THIRD_PARTY_NOTICES THIRD_PARTY_NOTICES.txt THIRD_PARTY_NOTICES.md" # The one file this script may edit to put the install folder on PATH: a marked block, or a file only we own (fish). profile_file() { case ${SHELL:-} in */fish) printf '%s\n' "${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/crumbtrail.fish" ;; */zsh) printf '%s\n' "${ZDOTDIR:-$HOME}/.zshrc" ;; */bash) # macOS terminals start login shells, which read only the first of these three that exists if [ "$(uname -s)" != Darwin ]; then printf '%s\n' "$HOME/.bashrc" elif [ -f "$HOME/.bash_profile" ]; then printf '%s\n' "$HOME/.bash_profile" elif [ -f "$HOME/.bash_login" ]; then printf '%s\n' "$HOME/.bash_login" else printf '%s\n' "$HOME/.profile"; fi ;; *) printf '%s\n' "$HOME/.profile" ;; esac } add_to_path() { # idempotent: a second run finds the marker and changes nothing file=$1 if [ -f "$file" ] && grep -F "$MARK_BEGIN" "$file" >/dev/null 2>&1; then return 0; fi mkdir -p "$(dirname "$file")" 2>/dev/null || return 1 case $file in *.fish) body='if not contains -- "$HOME/.local/bin" $PATH; set -gx PATH "$HOME/.local/bin" $PATH; end' ;; *) body='case ":$PATH:" in *":$HOME/.local/bin:"*) ;; *) export PATH="$HOME/.local/bin:$PATH" ;; esac' ;; esac { [ ! -s "$file" ] || printf '\n'; printf '%s\n%s\n%s\n' "$MARK_BEGIN" "$body" "$MARK_END"; } >> "$file" 2>/dev/null || return 1 } remove_from_path() { for file in "$HOME/.profile" "$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.bash_login" "${ZDOTDIR:-$HOME}/.zshrc"; do [ -f "$file" ] && grep -F "$MARK_BEGIN" "$file" >/dev/null 2>&1 || continue # Drops the block and the one blank line add_to_path put before it, so the file reads as it did before. awk -v b="$MARK_BEGIN" -v e="$MARK_END" ' $0==b{skip=1;held=0;next} $0==e{skip=0;next} skip{next} held{print "";held=0} $0==""{held=1;next} {print} END{if(held)print ""}' "$file" > "$file.crumbtrail-tmp" \ && cat "$file.crumbtrail-tmp" > "$file" && say "Removed the crumbtrail PATH entry from $file" || say "Could not edit $file; delete the lines between \"$MARK_BEGIN\" and \"$MARK_END\" yourself." rm -f "$file.crumbtrail-tmp" done fish=${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/crumbtrail.fish if [ -f "$fish" ] && grep -F "$MARK_BEGIN" "$fish" >/dev/null 2>&1; then rm -f "$fish"; say "Removed $fish"; fi } # Run a command, with sudo when the folder is not writable and this is a system install. as_owner() { if [ "$system" = 1 ] && [ "$(id -u)" != 0 ] && [ -z "${CRUMBTRAIL_INSTALL_DIR:-}" ]; then sudo "$@"; else "$@"; fi } if [ "$uninstall" = 1 ]; then for f in crumbtrail crumbtrail-cloud $notices; do if [ -f "$bindir/$f" ]; then as_owner rm -f "$bindir/$f"; say "Removed $bindir/$f"; fi done for f in $notices; do if [ -f "$docsdir/$f" ]; then as_owner rm -f "$docsdir/$f"; say "Removed $docsdir/$f"; fi done if [ -d "$docsdir" ]; then as_owner rmdir "$docsdir" 2>/dev/null || :; fi if [ -d "$relaydir" ]; then as_owner rm -rf "$relaydir"; say "Removed $relaydir"; fi [ "$default_dir" = 1 ] && remove_from_path say "Your recordings and saved setups stay in each project's .crumbtrail folder and in ~/.crumbtrail. Delete them yourself if you want them gone." exit 0 fi # Which licence is shown: the paid one for the paid edition (a paid archive named on --from, or the full editions on the # download site), the Free one for everything else. The package's own manifest is checked against this below. licence=free if [ -n "$from" ]; then case $(basename "$from") in crumbtrail-paid-*) licence=paid ;; esac else case $edition in full|full-no-reports) licence=paid ;; esac fi if [ "$licence" = paid ]; then say "Crumbtrail Paid Edition Software Licence" say "https://crumbtrail.ai/legal/paid-edition-licence" say "Where you have a signed agreement with Crumbtrail, that agreement governs your use." say "Use is subject to this licence and the Crumbtrail Privacy Policy: https://crumbtrail.ai/legal/privacy-policy" else say "Crumbtrail Free Edition Software Licence Agreement" say "https://crumbtrail.ai/legal/free-edition-licence" say "Use is subject to this licence and the Crumbtrail Privacy Policy: https://crumbtrail.ai/legal/privacy-policy" fi # The yes/no. For a paid archive on this computer it comes after the archive is checked, so the licence text can be read out of # that same checked archive and shown by its path (docs/specs/customer-onboarding.md section 4). ask_licence() { if [ "$accept_licence" != 1 ]; then [ -t 0 ] || die "licence acceptance required; use --accept-licence or CRUMBTRAIL_ACCEPT_LICENCE=1 for an unattended install" printf 'Accept the licence? [y/N] ' >&2 answer= IFS= read -r answer || answer= case $answer in y|Y|yes|Yes|YES) ;; *) die "licence not accepted; nothing was installed" ;; esac fi } if [ "$licence" = paid ] && [ -n "$from" ]; then :; else ask_licence; fi os=$(uname -s) case $os in Linux) os=linux ;; Darwin) os=macos ;; *) die "this installer supports Linux and macOS; on Windows use the PowerShell command from your account page at https://crumbtrail.ai/download" ;; esac arch=$(uname -m) case $arch in x86_64|amd64) arch=x86_64 ;; aarch64|arm64) arch=aarch64 ;; *) die "no build for the $arch processor" ;; esac if command -v sha256sum >/dev/null 2>&1; then sha() { sha256sum "$1" | cut -d' ' -f1; } elif command -v shasum >/dev/null 2>&1; then sha() { shasum -a 256 "$1" | cut -d' ' -f1; } else die "need sha256sum or shasum to check the download"; fi stall=30 mb() { awk -v b="$1" 'BEGIN { printf "%.1f", b / 1048576 }'; } draw() { # draw DEST: one progress line, redrawn in place, once some bytes have arrived [ -f "$1.part" ] || return 0 have=$(wc -c 2>/dev/null < "$1.part" | tr -d ' ') || return 0 [ -n "$have" ] && [ "$have" -gt 0 ] || return 0 total=$(tr -d '\r' 2>/dev/null < "$1.head" | awk '{ k = tolower($1) } k == "content-length:" { l = $2 } k == "content-range:" { n = split($3, a, "/"); r = a[n] } END { if (r ~ /^[0-9]+$/) print r; else if (l ~ /^[0-9]+$/) print l }') || total= if [ -n "$total" ] && [ "$total" -ge "$have" ] && [ "$total" -gt 0 ]; then printf '\r %s of %s MB (%s%%) ' "$(mb "$have")" "$(mb "$total")" $((have * 100 / total)) >&2 else printf '\r %s MB ' "$(mb "$have")" >&2; fi drew=1 } getter= get() { # get URL DEST: one try, always run as (get ...) so exec replaces only that subshell. Reads the token header from stdin. exec curl --silent --fail --max-redirs 0 --connect-timeout 15 --speed-limit 1024 --speed-time "$stall" ${resume:+--continue-at} ${resume:+-} \ -K - -D "$2.head" -w '%{http_code}' -o "$2.part" "$1" } # fetch URL DEST WHAT [progress]. Credentialed downloads never follow a redirect and the token never appears in a process argument list. # Each try gives up on a connection that does not open in 15 s or a transfer below 1 KB/s for $stall s. A dropped or stalled download is # tried up to 4 times and resumes from the bytes already received when the server allows it; the checksum below still checks every byte. # A progress line is shown for the big file only when the output is a terminal and not CI. fetch() { case $BASE in https://*) ;; http://*) printf '%s\n' "$BASE" | awk '/^http:\/\/(localhost|127\.0\.0\.1|\[::1\])(:[0-9]+)?(\/|$)/ {ok=1} END {exit !ok}' || die "downloads require HTTPS except on this computer" ;; *) die "downloads require HTTPS" ;; esac [ -n "${CRUMBTRAIL_DOWNLOAD_TOKEN:-}" ] || die "sign in at https://crumbtrail.ai/download and generate an install command" case $CRUMBTRAIL_DOWNLOAD_TOKEN in *[!0-9a-f]*) die "the download token is not valid; generate a new install command at https://crumbtrail.ai/download" ;; esac [ ${#CRUMBTRAIL_DOWNLOAD_TOKEN} = 64 ] || die "the download token is not valid; generate a new install command at https://crumbtrail.ai/download" command -v curl >/dev/null 2>&1 || die "need curl to download" stall=${CRUMBTRAIL_DOWNLOAD_STALL_SECONDS:-30} case $stall in ''|*[!0-9]*|0) die "CRUMBTRAIL_DOWNLOAD_STALL_SECONDS must be a whole number of seconds above 0" ;; esac rm -f "$2.part" show=0 if [ "${4:-}" = progress ] && [ -t 2 ] && [ -z "${CI:-}" ]; then show=1; fi tries=4; try=1; pause=1 while :; do resume= [ ! -s "$2.part" ] || resume=- rm -f "$2.code" "$2.head" rc=0 if [ "$show" = 1 ]; then # in the background so the line can be redrawn; $! is the download itself, which cleanup stops on an interrupt printf 'header = "Authorization: Bearer %s"\n' "$CRUMBTRAIL_DOWNLOAD_TOKEN" | (get "$1" "$2") > "$2.code" 2>/dev/null & getter=$! drew=0 while kill -0 "$getter" 2>/dev/null; do draw "$2"; sleep 1; done wait "$getter" || rc=$? getter= draw "$2"; [ "$drew" = 0 ] || printf '\n' >&2 else printf 'header = "Authorization: Bearer %s"\n' "$CRUMBTRAIL_DOWNLOAD_TOKEN" | (get "$1" "$2") > "$2.code" 2>/dev/null || rc=$?; fi status=$(cat "$2.code" 2>/dev/null) || status=000 rm -f "$2.code" "$2.head" why= case $rc:$status in 0:200|0:206) mv "$2.part" "$2"; return 0 ;; 0:416) # asked to continue past the end: the earlier try already received the whole file; the checksum below still checks it if [ -s "$2.part" ]; then mv "$2.part" "$2"; return 0; fi why="the download site could not continue the earlier download" ;; *:401|*:403) rm -f "$2.part"; die "download refused; sign in at https://crumbtrail.ai/download and generate a new install command" ;; *:404) rm -f "$2.part"; die "$3 is not published yet. Nothing was installed. Your command still works; run it again after the next release" ;; *:3??) rm -f "$2.part"; die "the download site redirected the request, which is refused for your safety. Nothing was installed" ;; *:416|33:*) rm -f "$2.part"; why="the download site could not continue the earlier download" ;; 22:408|22:429|22:5??) why="the download site is busy" ;; 0:*|22:*) rm -f "$2.part"; die "the download site answered $status. Nothing was installed; try again later" ;; 6:*) why="the download site could not be found" ;; 7:*|28:000) why="the download site could not be reached" ;; 28:*) why="the download stopped or slowed below 1 KB/s for $stall seconds" ;; 18:*|52:*|55:*|56:*|92:*) why="the connection dropped" ;; *) rm -f "$2.part"; die "the download failed. Nothing was installed; check your network and run the command again" ;; esac if [ "$try" -ge "$tries" ]; then rm -f "$2.part" case $rc:$status in 22:*) die "the download site is busy and answered $status. Nothing was installed; try again later" ;; 6:*|7:*|28:000) die "could not reach the download site. Nothing was installed; check your network and run the command again" ;; *) die "$why. It did not finish after $tries tries. Nothing was installed; check your network and run the command again" ;; esac fi try=$((try + 1)) if [ -s "$2.part" ]; then next="Continuing where it stopped"; else next="Starting again"; fi say "The download was interrupted: $why. $next in $pause s (try $try of $tries)." >&2 sleep "$pause"; pause=$((pause * 2)) done } work=$(mktemp -d "${TMPDIR:-/tmp}/crumbtrail-install.XXXXXX") check_signed() { # check_signed LIST SIGNATURE WHAT: the checksum list must carry a valid Crumbtrail release signature. command -v openssl >/dev/null 2>&1 || die "need openssl to check that the download comes from Crumbtrail" printf '%s\n' '-----BEGIN PUBLIC KEY-----' "$(printf '%s' "$RELEASE_KEY" | fold -w 64)" '-----END PUBLIC KEY-----' > "$work/release-key.pem" openssl dgst -sha256 -verify "$work/release-key.pem" -signature "$2" "$1" >/dev/null 2>&1 || die "$3 is not signed by Crumbtrail. Nothing was installed." say "Signature OK" } mutating=0; reason=; interrupted=0; ct_token= cleanup() { # runs on every exit, including a signal: roll back a half-finished update BEFORE the scratch folder (with the plan) is deleted rc=$? trap '' INT HUP TERM [ -z "${getter:-}" ] || kill "$getter" 2>/dev/null || : if [ "$mutating" = 1 ]; then if undo; then if [ "$interrupted" = 1 ]; then say "Interrupted. Your earlier installation was put back exactly as it was." >&2 else say "Crumbtrail install failed: ${reason:-the install was interrupted}. Your earlier installation was put back exactly as it was." >&2; fi else say "Crumbtrail install failed and the earlier files could not all be put back. Files ending in .ct-old next to the programs hold your earlier versions." >&2; fi [ "$rc" != 0 ] || rc=1 elif [ "$mutating" = 2 ]; then finish fi rm -rf "$work" exit "$rc" } trap cleanup EXIT trap 'interrupted=1; exit 130' INT trap 'interrupted=1; exit 129' HUP trap 'interrupted=1; exit 143' TERM if [ -n "$from" ]; then [ -f "$from" ] || die "no such file: $from" tarball=$from file=$(basename "$from") want=${CRUMBTRAIL_SHA256:-} sums=$(dirname "$from")/SHA256SUMS if [ "$allow_unsigned" = 1 ]; then unsigned_warning="Warning: this package has no signature, so nothing proves it came from Crumbtrail. Go on only if Crumbtrail sent it to you directly or you built it yourself. Otherwise put SHA256SUMS.sig beside it and run again without --allow-unsigned." if [ -z "$want" ]; then [ -f "$sums" ] || die "no SHA256SUMS next to $from. Put the file beside it, or set CRUMBTRAIL_SHA256." if [ -f "$sums.sig" ]; then check_signed "$sums" "$sums.sig" "The SHA256SUMS next to $from" else say "$unsigned_warning" >&2 fi else say "$unsigned_warning" >&2 fi else [ -f "$sums" ] || die "no SHA256SUMS next to $from. Put the signed list beside it." [ -f "$sums.sig" ] || die "SHA256SUMS has no signature. Put SHA256SUMS.sig beside it, as sent by Crumbtrail. Nothing was installed." check_signed "$sums" "$sums.sig" "The SHA256SUMS next to $from" fi if [ -z "$want" ] || [ "$allow_unsigned" = 0 ]; then listed=$(awk -v f="$file" '{n=$2; sub(/^\*/,"",n)} n==f {print $1}' "$sums") [ -n "$listed" ] || die "SHA256SUMS does not list $file" [ -z "$want" ] || [ "$want" = "$listed" ] || die "CRUMBTRAIL_SHA256 differs from the signed list. Nothing was installed." want=$listed fi else case $edition in free|full|full-no-reports) ;; *) die "unknown edition" ;; esac if [ -z "$version" ]; then fetch "$BASE/latest" "$work/latest" "a release" version=$(tr -d ' \r\n' < "$work/latest") fi case $version in ''|*[!0-9A-Za-z._-]*) die "unexpected version \"$version\"" ;; esac case $edition in free) file=crumbtrail-free-$version-$os-$arch.tar.gz ;; *) file=crumbtrail-paid-$version-$os-$arch.tar.gz ;; esac say "Downloading $file" fetch "$BASE/$version/$edition/$file" "$work/$file" "A $os $arch build of version $version" progress fetch "$BASE/$version/$edition/SHA256SUMS" "$work/SHA256SUMS" "The checksum list for version $version" fetch "$BASE/$version/$edition/SHA256SUMS.sig" "$work/SHA256SUMS.sig" "The signature for version $version" ct_token=$CRUMBTRAIL_DOWNLOAD_TOKEN # a plain variable, never exported: only the private file below keeps it, for updates unset CRUMBTRAIL_DOWNLOAD_TOKEN # last fetch done: nothing extracted or run below ever sees the token check_signed "$work/SHA256SUMS" "$work/SHA256SUMS.sig" "The checksum list for version $version" want=$(awk -v f="$file" '{n=$2; sub(/^\*/,"",n)} n==f {print $1}' "$work/SHA256SUMS") [ -n "$want" ] || die "SHA256SUMS does not list $file" tarball=$work/$file fi case $want in ''|*[!0-9a-fA-F]*) die "invalid SHA256SUMS entry" ;; esac [ ${#want} = 64 ] || die "invalid SHA256SUMS entry" got=$(sha "$tarball") [ "$got" = "$want" ] || die "checksum mismatch for $file (expected $want, got $got). Nothing was installed." say "Checksum OK" # Refuse anything that would unpack outside its own folder. tar -tzf "$tarball" > "$work/list" || die "$file is not a readable archive" top=$(head -n 1 "$work/list" | cut -d/ -f1) case $top in crumbtrail-*[!0-9A-Za-z._-]*|'') die "unexpected archive layout" ;; crumbtrail-*) ;; *) die "unexpected archive layout" ;; esac awk -v top="$top" '($0 != top && index($0,top "/") != 1) || /(^|\/)\.\.($|\/)/ || /\\/ {bad=1} END {exit bad}' "$work/list" || die "unexpected archive layout" tar -tvzf "$tarball" > "$work/types" awk 'substr($0,1,1) != "-" && substr($0,1,1) != "d" {bad=1} END {exit bad}' "$work/types" || die "archive links and special files are not supported" if [ "$licence" = paid ] && [ -n "$from" ]; then if grep -Fx "$top/LICENCE.md" "$work/list" >/dev/null 2>&1 && tar -xzOf "$tarball" "$top/LICENCE.md" > "$work/LICENCE.md" 2>/dev/null; then say "Licence text: $work/LICENCE.md (it is kept as $docsdir/LICENCE.md once you accept)" else die "this package has no licence file. Nothing was installed." fi ask_licence fi mkdir "$work/x" tar -xzf "$tarball" -C "$work/x" pkg=$work/x/$top if [ "$licence" = paid ]; then # The paid licence was shown, so the package must be a paid one and nothing else. case $top in crumbtrail-paid-*) ;; *) die "this is not a Crumbtrail paid package" ;; esac grep -E '"edition"[[:space:]]*:[[:space:]]*"paid"' "$pkg/manifest.json" >/dev/null || die "this is not a Crumbtrail paid package" [ -f "$pkg/LICENCE.md" ] || die "this package has no licence file. Nothing was installed." elif [ "$allow_internal" = 0 ] && { [ -n "$from" ] || [ "$edition" = free ]; }; then case $top in crumbtrail-internal-*) die "internal full packages require --allow-internal with --from; this is not a Free download" ;; esac grep -E '"edition"[[:space:]]*:[[:space:]]*"free"' "$pkg/manifest.json" >/dev/null || die "a public install requires a Free package manifest" [ ! -e "$pkg/crumbtrail-cloud" ] || die "a Free package cannot contain the team service" fi if [ -f "$pkg/platform.txt" ]; then [ "$(cat "$pkg/platform.txt")" = "$os $arch" ] || die "the archive is for a different operating system or processor" else case "$os $arch" in "linux x86_64") target=x86_64-unknown-linux-musl ;; "linux aarch64") target=aarch64-unknown-linux-musl ;; "macos x86_64") target=x86_64-apple-darwin ;; "macos aarch64") target=aarch64-apple-darwin ;; *) die "unsupported operating system or processor" ;; esac grep -E '"target"[[:space:]]*:[[:space:]]*"'"$target"'"' "$pkg/manifest.json" >/dev/null || die "the archive is for a different operating system or processor" fi [ -f "$pkg/manifest.json" ] && [ -f "$pkg/sbom.cdx.json" ] && [ -f "$pkg/SHA256SUMS" ] || die "archive is missing its manifest, bill of materials or payload checksums" while read -r digest path; do path=${path#\*} case $path in ''|/*|../*|*/../*|*/..|*\\*) die "invalid payload checksum path" ;; esac [ -f "$pkg/$path" ] || die "missing archive file: $path" [ "$(sha "$pkg/$path")" = "$digest" ] || die "payload checksum mismatch: $path" done < "$pkg/SHA256SUMS" [ -x "$pkg/crumbtrail" ] || die "the archive has no crumbtrail program" reported=$("$pkg/crumbtrail" --version 2>/dev/null) || die "the program in the archive does not run on this computer" if [ -z "$from" ]; then # a network install must be the version that was asked for case " $reported " in *" $version "*) ;; *) die "the package reports \"$reported\" but version $version was requested. Nothing was installed" ;; esac mver=$(sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$pkg/manifest.json" | head -n 1) [ -z "$mver" ] || [ "$mver" = "$version" ] || die "the package manifest says version $mver but version $version was requested. Nothing was installed" fi # Replace every owned file together or not at all. The whole plan (source, destination, mode, and whether the destination already exists) # is written down before anything changes. Then every file is staged beside its destination as .ct-new, each old file is moved aside as # .ct-old, and the new file is moved in. Rollback reads the plan and the files on disk, so an interruption between any two steps is safe: # an old file is only restored when its .ct-old exists, a destination is only removed when the plan said it did not exist before, and a # destination the plan said already existed is never deleted. This also runs on a signal, before the scratch folder is deleted. for f in p_src p_dst p_mode p_had; do : > "$work/$f"; done nl=' ' plan_one() { # plan_one SOURCE DEST MODE (MODE keep copies the source's own permissions) case $1$2 in *"$nl"*) die "a file name contains a line break; nothing was installed" ;; esac if [ -e "$2.ct-old" ] || [ -L "$2.ct-old" ]; then die "$2.ct-old is left over from an earlier interrupted update and holds your earlier version. Move it back over $2 (or delete it) and run the command again. Nothing was changed"; fi printf '%s\n' "$1" >> "$work/p_src"; printf '%s\n' "$2" >> "$work/p_dst"; printf '%s\n' "$3" >> "$work/p_mode" if [ -e "$2" ] || [ -L "$2" ]; then printf 'H\n' >> "$work/p_had"; else printf 'N\n' >> "$work/p_had"; fi } plan_all() { for f in crumbtrail crumbtrail-cloud; do [ -f "$pkg/$f" ] || continue plan_one "$pkg/$f" "$bindir/$f" 0755 done for notice in $notices; do [ -f "$pkg/$notice" ] || continue plan_one "$pkg/$notice" "$docsdir/$notice" 0644 done if [ -d "$pkg/relay" ]; then for r in "$pkg"/relay/*; do [ -f "$r" ] || continue plan_one "$r" "$relaydir/$(basename "$r")" keep done fi } stage_all() { while IFS= read -r src <&5 && IFS= read -r dst <&6 && IFS= read -r mode <&7; do as_owner mkdir -p "$(dirname "$dst")" || return 1 if [ "$mode" = keep ]; then as_owner cp -p "$src" "$dst.ct-new" || return 1; else as_owner install -m "$mode" "$src" "$dst.ct-new" || return 1; fi done 5< "$work/p_src" 6< "$work/p_dst" 7< "$work/p_mode" } swap_all() { while IFS= read -r dst <&6 && IFS= read -r had <&8; do if [ "$had" = H ]; then as_owner mv -f "$dst" "$dst.ct-old" || return 1; fi as_owner mv -f "$dst.ct-new" "$dst" || return 1 done 6< "$work/p_dst" 8< "$work/p_had" } undo() { # put everything back from the plan and the disk; fails (return 1) if any file could not be restored failed=0 while IFS= read -r dst <&6 && IFS= read -r had <&8; do if [ "$had" = H ]; then if [ -e "$dst.ct-old" ] || [ -L "$dst.ct-old" ]; then as_owner mv -f "$dst.ct-old" "$dst" || failed=1; fi else as_owner rm -f "$dst" || failed=1; fi as_owner rm -f "$dst.ct-new" || failed=1 done 6< "$work/p_dst" 8< "$work/p_had" return $failed } finish() { # the install is complete: drop the moved-aside old files while IFS= read -r dst <&6 && IFS= read -r had <&8; do [ "$had" = H ] && as_owner rm -f "$dst.ct-old" : done 6< "$work/p_dst" 8< "$work/p_had" } as_owner mkdir -p "$bindir" || die "could not create $bindir. Nothing was installed" # The version already installed, if any, so the end of the install can say it was replaced. A program that will not run says nothing. was= if [ -x "$bindir/crumbtrail" ] && [ ! -L "$bindir/crumbtrail" ]; then was=$("$bindir/crumbtrail" --version 2>/dev/null | head -n 1) || was=; fi plan_all mutating=1 reason="could not replace the installed files" if ! stage_all; then reason="could not write to $bindir (or the relay folder)"; exit 1; fi if ! swap_all; then exit 1; fi mutating=2 finish mutating=0 now=$("$bindir/crumbtrail" --version) say "Installed $now into $bindir" if [ -n "$was" ] && [ "$was" != "$now" ]; then say "Replaced crumbtrail $(printf '%s' "$was" | awk '{print $NF}') with $(printf '%s' "$now" | awk '{print $NF}')."; fi # Keep the token where the installed program can find it to check for and fetch updates: one private file in your own # home folder. A root install (sudo) never writes into a home folder. if [ -n "$ct_token" ] && [ "$(id -u)" != 0 ] && [ -d "${HOME:-}" ]; then tokdir=$HOME/.crumbtrail/updates if (umask 077; mkdir -p "$tokdir" && printf '%s' "$ct_token" > "$tokdir/download-token.new" && mv -f "$tokdir/download-token.new" "$tokdir/download-token") 2>/dev/null; then :; else say "Could not save your download token, so crumbtrail update will ask you to run this install command again." >&2 fi fi ct_token= [ ! -f "$docsdir/THIRD_PARTY_NOTICES.md" ] || say "Open-source notices: $docsdir/THIRD_PARTY_NOTICES.md" if [ "$licence" = paid ]; then nextcmd="crumbtrail cloud sign-in --server https://app.crumbtrail.ai"; elif [ "$edition" = free ]; then nextcmd="crumbtrail desk serve"; else nextcmd="crumbtrail init"; fi case ":$PATH:" in *":$bindir:"*) say "Next: $nextcmd" ;; *) if [ "$default_dir" = 1 ] && [ "$modify_path" = 1 ]; then file=$(profile_file) if add_to_path "$file"; then say "Added $bindir to your PATH in $file, so new terminals find crumbtrail." else say "Could not edit $file. Add $bindir to your PATH yourself."; fi fi esc=$(printf '%s' "$bindir" | sed 's/[\\"$`]/\\&/g') case ${SHELL:-} in */fish) say "To use it in this terminal, paste: set -gx PATH \"$esc\" \$PATH; and $nextcmd" ;; *) say "To use it in this terminal, paste: export PATH=\"$esc:\$PATH\"; $nextcmd" ;; esac ;; esac } main "$@"