function zmx-remote-install --description "Install zmx on a remote host by streaming a locally downloaded release tarball" argparse 'h/help' 'upgrade-only' 'v/version=' -- $argv or return 1 if set -q _flag_help; or test (count $argv) -ne 1 echo "Usage: zmx-remote-install [--upgrade-only] [--version VERSION] [user@]host" echo "Example: zmx-remote-install hel1" echo "By default, installs the active local zmx version." echo "With --upgrade-only, only upgrades an existing remote zmx that is older than local." return 0 end set -l destination $argv[1] set -l desired_version if set -q _flag_version set desired_version $_flag_version else if type -q zmx set desired_version (zmx version | awk 'NR == 1 {print $2}') else if type -q mise set desired_version (mise exec -- zmx version | awk 'NR == 1 {print $2}') end if test -z "$desired_version" echo "zmx-remote-install: could not determine local zmx version from zmx or mise" >&2 return 1 end mkdir -p "$HOME/.ssh/controlmasters" set -l remote_version (command ssh \ -o ControlMaster=auto \ -o ControlPersist=10m \ -o ControlPath="$HOME/.ssh/controlmasters/%C" \ $destination \ "sh -c 'PATH=\"\$HOME/.local/bin:\$PATH\"; if command -v zmx >/dev/null 2>&1; then zmx version | awk \"NR == 1 {print \\\$2}\"; fi'") or return $status if test -z "$remote_version" if set -q _flag_upgrade_only return 0 end echo "Remote zmx is not installed; installing local zmx $desired_version..." >&2 else if test "$remote_version" = "$desired_version" return 0 else if not type -q semver echo "zmx-remote-install: semver is required to compare local zmx $desired_version with remote zmx $remote_version" >&2 return 1 end if not semver "$desired_version" >/dev/null 2>&1; or not semver "$remote_version" >/dev/null 2>&1 echo "zmx-remote-install: cannot compare local zmx $desired_version with remote zmx $remote_version; refusing to sidegrade" >&2 return 1 end if not semver -r ">$remote_version" "$desired_version" >/dev/null 2>&1 return 0 end echo "Remote zmx is $remote_version; upgrading to local zmx $desired_version..." >&2 end set -l remote_platform (command ssh \ -o ControlMaster=auto \ -o ControlPersist=10m \ -o ControlPath="$HOME/.ssh/controlmasters/%C" \ $destination \ "sh -c 'printf \"%s %s\\n\" \"\$(uname -s)\" \"\$(uname -m)\"'") or return $status set -l remote_os (string split ' ' -- $remote_platform)[1] set -l remote_arch (string split ' ' -- $remote_platform)[2] set -l zmx_os set -l zmx_arch switch $remote_os case Linux set zmx_os linux case Darwin set zmx_os macos case '*' echo "zmx-remote-install: unsupported remote OS '$remote_os'" >&2 return 2 end switch $remote_arch case x86_64 amd64 set zmx_arch x86_64 case aarch64 arm64 set zmx_arch aarch64 case '*' echo "zmx-remote-install: unsupported remote architecture '$remote_arch'" >&2 return 2 end set -l archive "zmx-$desired_version-$zmx_os-$zmx_arch.tar.gz" set -l base_url "https://zmx.sh/a" set -l temp_dir (mktemp -d) set -l archive_path "$temp_dir/$archive" set -l checksum_path "$archive_path.sha256" if test -z "$temp_dir" echo "zmx-remote-install: failed to create a temporary directory" >&2 return 1 end echo "Downloading $archive..." >&2 if not curl -fsSL "$base_url/$archive" -o "$archive_path" rm -rf $temp_dir return 1 end if curl -fsSL "$base_url/$archive.sha256" -o "$checksum_path" set -l expected (awk '{print $1}' "$checksum_path") set -l actual if type -q sha256sum set actual (sha256sum "$archive_path" | awk '{print $1}') else if type -q shasum set actual (shasum -a 256 "$archive_path" | awk '{print $1}') else echo "zmx-remote-install: no local sha256sum or shasum found; refusing to install unverified archive" >&2 rm -rf $temp_dir return 1 end if test "$expected" != "$actual" echo "zmx-remote-install: checksum verification failed for $archive" >&2 rm -rf $temp_dir return 1 end else echo "zmx-remote-install: could not download checksum for $archive; refusing to install unverified archive" >&2 rm -rf $temp_dir return 1 end echo "Installing zmx $desired_version on $destination..." >&2 if not command ssh \ -o ControlMaster=auto \ -o ControlPersist=10m \ -o ControlPath="$HOME/.ssh/controlmasters/%C" \ $destination \ "sh -c 'mkdir -p \"\$HOME/.local/bin\" && tmp=\$(mktemp \"\$HOME/.local/bin/.zmx.XXXXXX\") && tar -xOzf - zmx > \"\$tmp\" && chmod 755 \"\$tmp\" && mv \"\$tmp\" \"\$HOME/.local/bin/zmx\"'" <"$archive_path" rm -rf $temp_dir return 1 end rm -rf $temp_dir command ssh \ -o ControlMaster=auto \ -o ControlPersist=10m \ -o ControlPath="$HOME/.ssh/controlmasters/%C" \ $destination \ "sh -c 'PATH=\"\$HOME/.local/bin:\$PATH\"; zmx version'" end