#!/usr/bin/env bash
# Inspired on official babashka install script https://github.com/babashka/babashka/blob/master/install
set -euo pipefail
version=""
checksum=""
default_install_dir="/usr/local/bin"
install_dir="$default_install_dir"
download_dir=""
print_help() {
echo "Installs latest (or specific) version of eca. Installation directory defaults to $default_install_dir."
echo -e
echo "Usage:"
echo "install [--dir
] [--download-dir ] [--version or \"nightly\"] [--checksum ]"
echo -e
echo "Defaults:"
echo " * Installation directory: ${default_install_dir}"
echo " * Download directory: temporary"
if [[ -z "$checksum" ]]; then
echo " * Checksum: no"
else
echo " * Checksum: ${checksum}"
fi
echo " * Version: "
echo -e
echo "Requires curl and unzip (plus sha256sum or shasum when using --checksum)."
exit 1
}
# Prints an error (and an optional hint) to stderr and exits.
error() {
>&2 echo "Error: $1"
if [[ -n "${2:-}" ]]; then
>&2 echo "$2"
fi
exit 1
}
has() {
command -v "$1" >/dev/null 2>&1
}
file_sha256() {
if has shasum; then
shasum -a 256 < "$1" | cut -d' ' -f1
else
sha256sum < "$1" | cut -d' ' -f1
fi
}
while [[ $# -gt 0 ]]
do
key="$1"
case "$key" in
--dir)
install_dir="$2"
shift
shift
;;
--download-dir)
download_dir="$2"
shift
shift
;;
--version)
version="$2"
shift
shift
;;
--checksum)
checksum="$2"
shift
shift
;;
--static)
# Retro compatible, always static if available for that arch.
shift
;;
*) # unknown option
print_help
;;
esac
done
if [[ -z "$download_dir" ]]; then
download_dir="$(mktemp -d)"
trap 'rm -rf "$download_dir"' EXIT
fi
if [[ "$checksum" != "" ]] && [[ "$version" == "" ]]; then
error "options --checksum and --version should be provided together!"
fi
# Check required commands upfront, otherwise `set -e` would make the script
# exit halfway through without saying why when one of them is missing.
missing=""
for cmd in curl unzip; do
has "$cmd" || missing="$missing $cmd"
done
if [[ -n "$checksum" ]] && ! has shasum && ! has sha256sum; then
missing="$missing sha256sum"
fi
if [[ -n "$missing" ]]; then
error "required command(s) not found:$missing" \
"Please install the missing command(s) using your system package manager and run this script again."
fi
case "$(uname -s)" in
Linux*) platform=linux;;
Darwin*) platform=macos;;
*) error "unsupported OS: $(uname -s)" \
"Please download eca manually from https://github.com/editor-code-assistant/eca/releases";;
esac
case "$(uname -m)" in
aarch64) arch=aarch64;;
arm64) arch=aarch64;;
*) arch=amd64;;
esac
if [[ "$version" == "" ]]; then
version_url="https://raw.githubusercontent.com/editor-code-assistant/eca/master/resources/ECA_VERSION"
if ! version="$(curl -fsSL "$version_url")" || [[ -z "$version" ]]; then
error "failed to fetch the latest eca version from $version_url" \
"Check your internet connection or pass a specific version with --version."
fi
fi
ext="zip"
case "$platform-$arch" in
linux-amd64) filename="eca-native-static-$platform-$arch."$ext
;;
*) filename="eca-native-$platform-$arch."$ext
;;
esac
if [[ "$version" == "nightly" ]]; then
download_url="https://github.com/editor-code-assistant/eca-dev-builds/releases/latest/download/$filename"
else
download_url="https://github.com/editor-code-assistant/eca/releases/download/$version/$filename"
fi
# No subshell here on purpose: bash 3.2 (macOS default) doesn't stop on a failing
# subshell even with `set -e`, which would let the script continue after an error.
mkdir -p "$download_dir"
archive="$download_dir/$filename"
echo -e "Downloading $download_url to $download_dir"
if ! curl -fsSL -o "$archive" "$download_url"; then
rm -f "$archive"
error "failed to download $download_url" \
"Check your internet connection and that eca version '$version' is available for $platform-$arch."
fi
if [[ -n "$checksum" ]]; then
actual_checksum="$(file_sha256 "$archive")"
expected_checksum="$(echo "$checksum" | tr '[:upper:]' '[:lower:]')"
if [[ "$actual_checksum" != "$expected_checksum" ]]; then
>&2 echo "Error: failed checksum on $filename"
>&2 echo "Got: $actual_checksum"
>&2 echo "Expected: $expected_checksum"
exit 1
fi
fi
unzip -qqo "$archive" -d "$download_dir"
rm -f "$archive"
if [[ "$download_dir" != "$install_dir" ]]
then
mkdir -p "$install_dir"
if [ -f "$install_dir/eca" ]; then
echo "Moving $install_dir/eca to $install_dir/eca.old"
mv -f "$install_dir/eca" "$install_dir/eca.old"
fi
mv -f "$download_dir/eca" "$install_dir/eca"
fi
if [[ "$version" == "nightly" ]]; then
chmod +x "$install_dir/eca"
fi
echo "Successfully installed eca in $install_dir"