#!/bin/sh # Install the Makoto compiler and Kamotsu into the current user's home directory. # Usage: sh install.sh [--from-dir ] set -eu fail() { printf 'Makoto installer: %s\n' "$*" >&2 exit 1 } fetch_asset() { if [ -n "$dist_dir" ]; then cp "$dist_dir/$1" "$2" || fail "could not read $dist_dir/$1." else curl --proto '=https' --proto-redir '=https' --tlsv1.2 -fsSL --retry 2 \ -o "$2" "$dist_url/$1" || fail "could not download $dist_url/$1. Rerun the installer to retry." fi } # Preserve the user's profile and append the same line only once. add_path_line() { mkdir -p "$(dirname "$1")" && { [ -f "$1" ] && grep -Fqx "$2" "$1" || printf '\n%s\n' "$2" >> "$1" } } configure_path() { posix_line='case ":${PATH-}:" in *":$HOME/.makoto/bin:"*) ;; *) export PATH="$HOME/.makoto/bin${PATH:+:$PATH}" ;; esac' case "${SHELL##*/}" in zsh) add_path_line "${ZDOTDIR:-$HOME}/.zshrc" "$posix_line" ;; bash) # Interactive bash uses .bashrc. A login shell reads the first # existing file in this list; do not shadow it with a new one. profile="$HOME/.profile" for file in "$HOME/.bash_profile" "$HOME/.bash_login"; do if [ -f "$file" ]; then profile=$file; break; fi done add_path_line "$HOME/.bashrc" "$posix_line" && add_path_line "$profile" "$posix_line" ;; fish) add_path_line "${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish" \ 'contains -- "$HOME/.makoto/bin" $PATH; or set -gx PATH "$HOME/.makoto/bin" $PATH' ;; sh|dash|ksh) add_path_line "$HOME/.profile" "$posix_line" ;; *) return 1 ;; esac } install_makoto() { version=0.1.0 usage='Usage: sh install.sh [--from-dir ]' dist_dir='' if [ "$#" -gt 0 ]; then case "$1" in -h|--help) printf '%s\n' "$usage"; return ;; --from-dir) [ "$#" -eq 2 ] && [ -n "$2" ] || fail "$usage" dist_dir=$(CDPATH='' cd -- "$2" && pwd -P) || fail "could not open directory $2." ;; *) fail "$usage" ;; esac fi os=$(uname -s) arch=$(uname -m) case "$arch" in arm64|aarch64) cpu=aarch64 ;; x86_64|amd64) cpu=x86_64 ;; *) fail "no prebuilt toolchain for $os $arch. Choose an ARM64 or x86-64 macOS or Linux host." ;; esac case "$os" in Darwin) target=$cpu-apple-darwin ;; Linux) target=$cpu-unknown-linux-musl ;; *) fail "no prebuilt toolchain for $os $arch. On Windows, run the command in a WSL terminal." ;; esac case "${HOME:-}" in /*) ;; *) fail 'HOME must be an absolute directory path.' ;; esac prefix=$HOME/.makoto archive=makoto-$target.tar.gz # The project's permanent download host also serves installs while custom DNS propagates. dist_url=https://makoto-lang-org.vercel.app/dist/v$version mkdir -p "$prefix/toolchains" if [ -e "$prefix/bin" ] || [ -L "$prefix/bin" ]; then link=$(readlink "$prefix/bin") || fail "$prefix/bin already exists. Move it aside and try again." case "$link" in toolchains/*/bin) ;; *) fail "$prefix/bin points to another installation." ;; esac fi stage=$(mktemp -d "$prefix/.install.XXXXXXXX") trap 'rm -rf "$stage"' 0 trap 'exit 1' HUP INT TERM printf 'Installing Makoto %s for %s…\n' "$version" "$target" fetch_asset "$archive.sha256" "$stage/checksum" read -r expected filename extra < "$stage/checksum" || fail 'could not read the download checksum.' case "$expected" in ''|*[!0-9a-f]*) fail 'invalid SHA-256 checksum.' ;; esac [ "${#expected}" -eq 64 ] && [ "$filename" = "$archive" ] && [ -z "$extra" ] || fail 'unexpected checksum format.' old_digest=$(cat "$prefix/bin/../.archive-sha256" 2>/dev/null || :) if [ "$old_digest" != "$expected" ] || ! "$prefix/bin/kamotsu" --version >/dev/null 2>&1 \ || ! "$prefix/bin/makotoc" --help >/dev/null 2>&1; then fetch_asset "$archive" "$stage/archive" if command -v sha256sum >/dev/null 2>&1; then actual=$(sha256sum < "$stage/archive") else actual=$(shasum -a 256 < "$stage/archive") fi [ "${actual%% *}" = "$expected" ] || fail 'archive checksum mismatch. Rerun the installer with an intact archive.' mkdir "$stage/package" tar -xzf "$stage/archive" -C "$stage/package" || fail 'could not unpack the toolchain.' "$stage/package/bin/kamotsu" --version >/dev/null && "$stage/package/bin/makotoc" --help >/dev/null \ || fail 'the downloaded toolchain could not run on this host.' printf '%s\n' "$expected" > "$stage/package/.archive-sha256" # Use a unique directory and rename the link in one step. Both tools # switch together, even when another installer runs at the same time. slot=toolchains/$target-${stage##*.} mv "$stage/package" "$prefix/$slot" ln -s "$slot/bin" "$stage/bin" case "$os" in Darwin) mv -fh "$stage/bin" "$prefix/bin" ;; Linux) mv -Tf "$stage/bin" "$prefix/bin" ;; esac fi "$prefix/bin/kamotsu" --version SHELL=${SHELL:-sh} if configure_path; then case "${SHELL##*/}" in fish) activate='set -gx PATH "$HOME/.makoto/bin" $PATH' ;; *) activate='export PATH="$HOME/.makoto/bin:$PATH"' ;; esac printf '\nOpen a new terminal, or activate Makoto here with:\n %s\n\nTry your first project:\n' "$activate" else printf '\nAdd %s/bin to your shell PATH, then run:\n' "$prefix" fi printf ' kamotsu new hello\n cd hello\n kamotsu run\n' } # A shell reading a piped download begins only after the whole function arrives. install_makoto "$@"