| 1 | #!/bin/sh |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | set -eu |
| 27 | |
| 28 | REPO="sylvinus/agent-vm" |
| 29 | |
| 30 | say() { printf 'agent-vm: %s\n' "$*"; } |
| 31 | die() { printf 'agent-vm: error: %s\n' "$*" >&2; exit 1; } |
| 32 | |
| 33 | need() { |
| 34 | command -v "$1" >/dev/null 2>&1 || die "$1 is required" |
| 35 | } |
| 36 | |
| 37 | usage() { |
| 38 | echo "usage: install.sh [--version X.Y.Z] [--git] [--dir DIR]" >&2 |
| 39 | exit 2 |
| 40 | } |
| 41 | |
| 42 | |
| 43 | fetch() { |
| 44 | curl --proto '=https' --proto-redir '=https' --tlsv1.2 -fsSL --retry 3 -o "$2" "$1" |
| 45 | } |
| 46 | |
| 47 | sha256_of() { |
| 48 | if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 |
| 49 | elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1 |
| 50 | else die "neither sha256sum nor shasum is installed" |
| 51 | fi |
| 52 | } |
| 53 | |
| 54 | |
| 55 | platform() { |
| 56 | os="$(uname -s)"; arch="$(uname -m)" |
| 57 | case "$os" in |
| 58 | Darwin) os=darwin ;; |
| 59 | Linux) os=linux ;; |
| 60 | MINGW*|MSYS*|CYGWIN*) os=windows ;; |
| 61 | esac |
| 62 | case "$arch" in |
| 63 | x86_64|amd64) arch=amd64 ;; |
| 64 | aarch64|arm64) arch=arm64 ;; |
| 65 | esac |
| 66 | echo "$os-$arch" |
| 67 | } |
| 68 | |
| 69 | cleanup() { |
| 70 | [ -z "$TMP" ] || rm -rf "$TMP" |
| 71 | [ -z "$LEFTOVER" ] || rm -rf "$LEFTOVER" |
| 72 | |
| 73 | if [ -n "$ROLLBACK" ] && [ -e "$ROLLBACK" ] && [ ! -e "$DIR" ]; then |
| 74 | mv "$ROLLBACK" "$DIR" && say "interrupted: the previous version is back in $DIR" |
| 75 | fi |
| 76 | } |
| 77 | |
| 78 | install_release() { |
| 79 | need curl |
| 80 | need tar |
| 81 | if [ -e "$DIR/.git" ]; then |
| 82 | die "$DIR is a git clone: update it with 'git -C \"$DIR\" pull', or run this with --git" |
| 83 | fi |
| 84 | if [ -e "$DIR" ] && [ ! -f "$DIR/agent-vm.sh" ]; then |
| 85 | die "$DIR exists and is not an agent-vm install: move it, or pick another --dir" |
| 86 | fi |
| 87 | |
| 88 | if [ -n "$WANT_VERSION" ]; then |
| 89 | base="https://github.com/$REPO/releases/download/v$WANT_VERSION" |
| 90 | else |
| 91 | base="https://github.com/$REPO/releases/latest/download" |
| 92 | fi |
| 93 | fetch "$base/SHA256SUMS" "$TMP/SHA256SUMS" || die "could not download $base/SHA256SUMS" |
| 94 | |
| 95 | |
| 96 | |
| 97 | platform="$(platform)" |
| 98 | tarball="$(awk -v p="$platform" '$2 ~ "^agent-vm-[0-9]+\\.[0-9]+\\.[0-9]+-" p "\\.tar\\.gz$" { print $2; exit }' "$TMP/SHA256SUMS")" |
| 99 | if [ -z "$tarball" ]; then |
| 100 | tarball="$(awk '$2 ~ /^agent-vm-[0-9]+\.[0-9]+\.[0-9]+\.tar\.gz$/ { print $2; exit }' "$TMP/SHA256SUMS")" |
| 101 | fi |
| 102 | [ -n "$tarball" ] || die "this release has no build for $platform" |
| 103 | expected="$(awk -v f="$tarball" '$2 == f { print $1; exit }' "$TMP/SHA256SUMS")" |
| 104 | version="${tarball#agent-vm-}" |
| 105 | version="${version%.tar.gz}" |
| 106 | version="${version%-"$platform"}" |
| 107 | if [ -n "$WANT_VERSION" ] && [ "$version" != "$WANT_VERSION" ]; then |
| 108 | die "asked for $WANT_VERSION, the release has $tarball" |
| 109 | fi |
| 110 | |
| 111 | |
| 112 | |
| 113 | say "downloading agent-vm $version" |
| 114 | fetch "https://github.com/$REPO/releases/download/v$version/$tarball" "$TMP/$tarball" \ |
| 115 | || die "could not download $tarball" |
| 116 | actual="$(sha256_of "$TMP/$tarball")" |
| 117 | [ "$actual" = "$expected" ] || die "checksum mismatch for $tarball (expected $expected, got $actual)" |
| 118 | |
| 119 | mkdir "$TMP/x" |
| 120 | tar -xzf "$TMP/$tarball" -C "$TMP/x" |
| 121 | src="$TMP/x/agent-vm-$version" |
| 122 | [ -f "$src/agent-vm.sh" ] || die "$tarball has no agent-vm-$version/agent-vm.sh" |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | mkdir -p "$(dirname "$DIR")" |
| 128 | LEFTOVER="$DIR.new.$$" |
| 129 | mv "$src" "$LEFTOVER" |
| 130 | if [ -e "$DIR" ]; then |
| 131 | ROLLBACK="$DIR.old.$$" |
| 132 | mv "$DIR" "$ROLLBACK" |
| 133 | mv "$LEFTOVER" "$DIR" |
| 134 | LEFTOVER="$ROLLBACK" |
| 135 | ROLLBACK="" |
| 136 | else |
| 137 | mv "$LEFTOVER" "$DIR" |
| 138 | LEFTOVER="" |
| 139 | fi |
| 140 | say "agent-vm $version is in $DIR" |
| 141 | } |
| 142 | |
| 143 | install_git() { |
| 144 | need git |
| 145 | if [ -e "$DIR/.git" ]; then |
| 146 | say "updating the clone in $DIR" |
| 147 | git -C "$DIR" pull --ff-only |
| 148 | elif [ -e "$DIR" ]; then |
| 149 | die "$DIR exists and is not a git clone: move it, or pick another --dir" |
| 150 | else |
| 151 | mkdir -p "$(dirname "$DIR")" |
| 152 | git clone "https://github.com/$REPO.git" "$DIR" |
| 153 | fi |
| 154 | } |
| 155 | |
| 156 | main() { |
| 157 | WANT_VERSION=""; USE_GIT=""; DIR="" |
| 158 | while [ $# -gt 0 ]; do |
| 159 | case "$1" in |
| 160 | --version) |
| 161 | [ $# -ge 2 ] || usage |
| 162 | WANT_VERSION="$2"; shift ;; |
| 163 | --version=*) WANT_VERSION="${1#*=}" ;; |
| 164 | --git) USE_GIT=1 ;; |
| 165 | --dir) |
| 166 | [ $# -ge 2 ] || usage |
| 167 | DIR="$2"; shift ;; |
| 168 | --dir=*) DIR="${1#*=}" ;; |
| 169 | -h|--help) usage ;; |
| 170 | *) usage ;; |
| 171 | esac |
| 172 | shift |
| 173 | done |
| 174 | WANT_VERSION="${WANT_VERSION#v}" |
| 175 | if [ -n "$WANT_VERSION" ]; then |
| 176 | printf '%s\n' "$WANT_VERSION" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$' \ |
| 177 | || die "not a version: '$WANT_VERSION' (expected X.Y.Z)" |
| 178 | [ -z "$USE_GIT" ] || die "--version installs a release: it does not go with --git" |
| 179 | fi |
| 180 | |
| 181 | case "$(uname -s)" in |
| 182 | Darwin|Linux|MINGW*|MSYS*|CYGWIN*) ;; |
| 183 | *) die "agent-vm runs on macOS, Linux and Windows (Git Bash) only" ;; |
| 184 | esac |
| 185 | need bash |
| 186 | |
| 187 | [ -n "${HOME:-}" ] || die "HOME is not set" |
| 188 | [ -n "$DIR" ] || DIR="${XDG_DATA_HOME:-$HOME/.local/share}/agent-vm" |
| 189 | |
| 190 | case "$DIR" in |
| 191 | /*|[A-Za-z]:/*|[A-Za-z]:\\*) ;; |
| 192 | *) DIR="$(pwd)/$DIR" ;; |
| 193 | esac |
| 194 | DIR="${DIR%/}" |
| 195 | |
| 196 | TMP=""; LEFTOVER=""; ROLLBACK="" |
| 197 | trap cleanup EXIT |
| 198 | trap 'exit 1' HUP INT TERM |
| 199 | TMP="$(mktemp -d 2>/dev/null || mktemp -d -t agent-vm)" |
| 200 | |
| 201 | if [ -n "$USE_GIT" ]; then install_git; else install_release; fi |
| 202 | |
| 203 | |
| 204 | |
| 205 | if (exec </dev/tty) 2>/dev/null; then |
| 206 | bash "$DIR/agent-vm.sh" install </dev/tty |
| 207 | else |
| 208 | bash "$DIR/agent-vm.sh" install </dev/null |
| 209 | fi |
| 210 | } |
| 211 | |
| 212 | main "$@" |