#!/bin/sh
set -eu
DEFAULT_VERSION="v4.24.2"
REPO_BASE="https://atomgit.com/atomgit_atomcode/atomcode/releases/download"
REPO_LATEST_API="https://api.atomgit.com/api/v5/repos/atomgit_atomcode/atomcode/releases/latest"
uname_s=$(uname -s)
uname_m=$(uname -m)
case "$uname_s" in
Darwin) os="darwin" ;;
Linux) os="linux" ;;
HarmonyOS) os="ohos" ;;
*) echo "Unsupported OS: $uname_s (Windows users: download the zip from the release page)"; exit 1 ;;
esac
case "$uname_m" in
arm64|aarch64) arch="arm64" ;;
x86_64|amd64) arch="x64" ;;
*) echo "Unsupported arch: $uname_m"; exit 1 ;;
esac
if [ -n "${ATOMCODE_PREFIX:-}" ]; then
PREFIX="$ATOMCODE_PREFIX"
elif [ "$os" = "ohos" ]; then
PREFIX="$HOME/.local/bin"
elif [ -w /usr/local/bin ] 2>/dev/null; then
PREFIX="/usr/local/bin"
elif [ "$(id -u)" -eq 0 ]; then
PREFIX="/usr/local/bin"
else
PREFIX="$HOME/.local/bin"
fi
mkdir -p "$PREFIX"
INVITE="${ATOMCODE_INVITE:-}"
while [ $# -gt 0 ]; do
case "$1" in
--invite=*) INVITE="${1#*=}"; shift ;;
--invite) shift; INVITE="$1"; shift ;;
*) shift ;;
esac
done
if [ -n "$INVITE" ]; then
ATOMCODE_DIR="${ATOMCODE_HOME:-$HOME/.atomcode}"
mkdir -p "$ATOMCODE_DIR"
INSTALL_UUID=""
if command -v uuidgen >/dev/null 2>&1; then
INSTALL_UUID="$(uuidgen)"
elif [ -f /proc/sys/kernel/random/uuid ]; then
INSTALL_UUID="$(cat /proc/sys/kernel/random/uuid)"
else
INSTALL_HEX="$(od -An -N16 -tx1 /dev/urandom 2>/dev/null | tr -d ' \n')"
INSTALL_UUID="$(printf '%s' "$INSTALL_HEX" | sed -E 's/^(.{8})(.{4})(.{4})(.{4})(.{12}).*/\1-\2-\3-\4-\5/')"
fi
if echo "$INVITE" | grep -qE '^[A-Za-z0-9]{8}$'; then
cat > "$ATOMCODE_DIR/pending_invite" <<EOF
invite_code=${INVITE}
install_uuid=${INSTALL_UUID}
attempted_at=$(date +%s)
EOF
else
echo "Warning: invalid invite code format, skipping referral" >&2
fi
fi
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
DEST="$TMP/atomcode"
if command -v curl >/dev/null 2>&1; then
_fetch="curl -sL --connect-timeout 5 --max-time 10"
_down="curl -fL --progress-bar -o"
elif command -v wget >/dev/null 2>&1; then
_fetch="wget -qO- --timeout=10 --tries=1"
_down="wget --show-progress -O"
else
echo "Error: need curl or wget." >&2
exit 1
fi
if [ -n "${ATOMCODE_VERSION:-}" ]; then
VERSION="$ATOMCODE_VERSION"
else
echo "==> Detecting latest version"
VERSION=$($_fetch "$REPO_LATEST_API" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')
[ -n "$VERSION" ] || VERSION="$DEFAULT_VERSION"
fi
BIN_NAME="atomcode-${VERSION}-${os}-${arch}"
URL="${REPO_BASE}/${VERSION}/${BIN_NAME}"
echo "==> Downloading $BIN_NAME"
echo " from $URL"
$_down "$DEST" "$URL"
if head -c 4 "$DEST" | grep -q "<" 2>/dev/null; then
echo "Error: download looks like an HTML page, not a binary."
echo " The release may not exist for your platform, or the URL is wrong."
echo " URL: $URL"
exit 1
fi
chmod +x "$DEST"
TARGET="$PREFIX/atomcode"
if [ -e "$TARGET" ] && [ ! -w "$TARGET" ]; then
echo "==> Installing to $TARGET (sudo required)"
sudo mv "$DEST" "$TARGET"
elif [ ! -w "$PREFIX" ]; then
echo "==> Installing to $TARGET (sudo required)"
sudo mv "$DEST" "$TARGET"
else
echo "==> Installing to $TARGET"
mv "$DEST" "$TARGET"
fi
echo ""
echo "Installed: $TARGET"
"$TARGET" --version 2>/dev/null || true
case ":$PATH:" in
*":$PREFIX:"*) ;;
*)
LINE="export PATH=\"$PREFIX:\$PATH\""
RC=""
if [ -n "${ZSH_VERSION:-}" ] || [ "$(basename "${SHELL:-}")" = "zsh" ]; then
RC="$HOME/.zshrc"
elif [ -n "${BASH_VERSION:-}" ] || [ "$(basename "${SHELL:-}")" = "bash" ]; then
RC="$HOME/.bashrc"
fi
if [ -n "$RC" ]; then
if [ -f "$RC" ] && grep -qF "$PREFIX" "$RC" 2>/dev/null; then
:
else
echo "" >> "$RC"
echo "# Added by AtomCode installer" >> "$RC"
echo "$LINE" >> "$RC"
echo ""
echo "Added $PREFIX to PATH in $RC"
fi
echo ""
echo "To start using atomcode right now, run:"
echo ""
echo " source $RC"
echo ""
else
echo ""
echo "Note: $PREFIX is not in your PATH. Add this line to your shell rc:"
echo " $LINE"
fi
;;
esac