#!/usr/bin/env bash set -euo pipefail # Configuration APP_NAME="EMessage" SERVER_URL="https://updater.machinetele.com" # Detect platform OS="$(uname -s)" ARCH="$(uname -m)" case "$OS" in Linux) GOOS="linux" ;; Darwin) GOOS="darwin" ;; *) echo "Unsupported OS: $OS"; exit 1 ;; esac case "$ARCH" in x86_64|amd64) GOARCH="amd64" ;; aarch64|arm64) GOARCH="arm64" ;; i386|i686) GOARCH="386" ;; armv7l) GOARCH="arm" ;; *) echo "Unsupported architecture: $ARCH"; exit 1 ;; esac PLATFORM="${GOOS}-${GOARCH}" echo "Checking for ${APP_NAME} updates for ${PLATFORM}..." echo "Server: ${SERVER_URL}" # Function to cleanup temp files cleanup() { rm -rf "$TMP_DIR" } trap cleanup ERR EXIT if [[ "$GOOS" == "darwin" ]]; then # --- macOS: download and install .app bundle from darwin-app --- MANIFEST_URL="${SERVER_URL}/${APP_NAME}/darwin-app.json" echo "Looking for macOS bundle manifest at ${MANIFEST_URL}" JSON=$(curl -sf "${MANIFEST_URL}") || { echo "Platform macOS download not available (no install manifest at ${MANIFEST_URL})" exit 1 } # Parse version and sha256 if command -v jq &>/dev/null; then VERSION=$(echo "$JSON" | jq -r '.Version') SHA256=$(echo "$JSON" | jq -r '.Sha256') else VERSION=$(echo "$JSON" | grep -o '"Version":"[^"]*"' | cut -d'"' -f4) SHA256=$(echo "$JSON" | grep -o '"Sha256":"[^"]*"' | cut -d'"' -f4) fi if [[ -z "$VERSION" || -z "$SHA256" ]]; then echo "Failed to parse macOS install manifest." exit 1 fi echo "Latest macOS bundle version: ${VERSION}" # Download the darwin-app tarball (uncompressed tar, as built by the build script) BUNDLE_URL="${SERVER_URL}/${APP_NAME}/darwin-app" TMP_DIR=$(mktemp -d) TMP_TAR="${TMP_DIR}/${APP_NAME}.tar" echo "Downloading ${BUNDLE_URL}..." curl -sfL "${BUNDLE_URL}" -o "${TMP_TAR}" || { echo "Download failed." exit 1 } # Verify SHA256 computed=$(sha256sum "${TMP_TAR}" | cut -d' ' -f1) if [[ "$computed" != "$SHA256" ]]; then echo "Hash mismatch! Expected $SHA256, got $computed" exit 1 fi # Determine installation directory if [[ $EUID -eq 0 ]]; then APP_INSTALL_DIR="/Applications" else APP_INSTALL_DIR="${HOME}/Applications" mkdir -p "$APP_INSTALL_DIR" fi # Extract the .app bundle (tar, not compressed) echo "Extracting to ${APP_INSTALL_DIR}..." tar xf "${TMP_TAR}" -C "$APP_INSTALL_DIR" || { echo "Extraction failed." exit 1 } # The archive should contain a .app directory if [[ ! -d "${APP_INSTALL_DIR}/${APP_NAME}.app" ]]; then echo "Error: Archive did not contain ${APP_NAME}.app" ls -la "${APP_INSTALL_DIR}" exit 1 fi echo "${APP_NAME} ${VERSION} installed to ${APP_INSTALL_DIR}/${APP_NAME}.app" echo "The auto-updater will handle future updates inside the bundle." else # --- Linux: download binary and create desktop entry (unchanged) --- MANIFEST_URL="${SERVER_URL}/${APP_NAME}/${PLATFORM}.json" echo "Checking for Linux binary at ${MANIFEST_URL}" JSON=$(curl -sf "${MANIFEST_URL}") || { echo "Platform ${PLATFORM} is not supported (no manifest found at ${MANIFEST_URL})" exit 1 } # Parse version and sha256 if command -v jq &>/dev/null; then VERSION=$(echo "$JSON" | jq -r '.Version') SHA256=$(echo "$JSON" | jq -r '.Sha256') else VERSION=$(echo "$JSON" | grep -o '"Version":"[^"]*"' | cut -d'"' -f4) SHA256=$(echo "$JSON" | grep -o '"Sha256":"[^"]*"' | cut -d'"' -f4) fi if [[ -z "$VERSION" || -z "$SHA256" ]]; then echo "Failed to parse Linux binary manifest." exit 1 fi echo "Latest Linux version: ${VERSION}" # Download binary BINARY_URL="${SERVER_URL}/${APP_NAME}/${VERSION}/${PLATFORM}.gz" TMP_DIR=$(mktemp -d) TMP_GZ="${TMP_DIR}/${APP_NAME}.gz" echo "Downloading ${BINARY_URL}..." curl -sfL "${BINARY_URL}" -o "${TMP_GZ}" || { echo "Download failed." exit 1 } # Verify SHA256 echo "${SHA256} ${TMP_GZ}" | sha256sum -c --quiet 2>/dev/null || { echo "Hash mismatch! Binary may be corrupted." exit 1 } # Decompress gunzip -c "${TMP_GZ}" > "${TMP_DIR}/${APP_NAME}" || { echo "Decompression failed." exit 1 } chmod +x "${TMP_DIR}/${APP_NAME}" # Install binary if [[ $EUID -eq 0 ]]; then INSTALL_DIR="/usr/local/bin" else # Prefer ~/.local/bin, else ~/bin if [[ -d "${HOME}/.local/bin" ]]; then INSTALL_DIR="${HOME}/.local/bin" elif [[ -d "${HOME}/bin" ]]; then INSTALL_DIR="${HOME}/bin" else INSTALL_DIR="${HOME}/.local/bin" mkdir -p "$INSTALL_DIR" fi fi cp "${TMP_DIR}/${APP_NAME}" "${INSTALL_DIR}/${APP_NAME}" echo "Binary installed to ${INSTALL_DIR}/${APP_NAME}" # --- Linux desktop entry with icon --- ICON_URL="${SERVER_URL}/${APP_NAME}/appicon.png" ICON_DIR="${HOME}/.local/share/icons/hicolor/256x256/apps" mkdir -p "$ICON_DIR" if curl -sfL "${ICON_URL}" -o "${ICON_DIR}/${APP_NAME}.png"; then echo "App icon downloaded." else echo "Warning: Could not download icon (${ICON_URL})." ICON_DIR="" # will skip icon in desktop file fi DESKTOP_DIR="${HOME}/.local/share/applications" mkdir -p "$DESKTOP_DIR" DESKTOP_FILE="${DESKTOP_DIR}/${APP_NAME}.desktop" cat > "$DESKTOP_FILE" <