#!/bin/sh set -e usage() { cat <&2 } detect_platform() { OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) # Convert OS names case "$OS" in linux*) OS="linux" ;; darwin*) OS="macos" ;; cygwin*|mingw*|msys*) OS="windows" ;; *) OS="linux" ;; # fallback esac # Convert architecture case "$ARCH" in x86_64|x64) ARCH="amd64" ;; i386|i686|x86) ARCH="386" ;; armv7l) ARCH="armv7" ;; aarch64|arm64) ARCH="arm64" ;; *) ARCH="amd64" ;; # fallback esac echo "${OS}_${ARCH}" } download_file() { local url="$1" local output="$2" log_debug "Downloading: $url -> $output" if command -v curl >/dev/null 2>&1; then curl -sSL -f "$url" -o "$output" || { log_err "Failed to download $url" return 1 } elif command -v wget >/dev/null 2>&1; then wget -q -O "$output" "$url" || { log_err "Failed to download $url" return 1 } else log_err "Neither curl nor wget found" return 1 fi log_debug "Download completed: $output" } extract_tar() { local archive="$1" local target_dir="$2" log_debug "Extracting $archive to $target_dir" if [ ! -f "$archive" ]; then log_err "Archive not found: $archive" return 1 fi mkdir -p "$target_dir" case "$archive" in *.tar.gz|*.tgz) tar -xzf "$archive" -C "$target_dir" ;; *.tar) tar -xf "$archive" -C "$target_dir" ;; *.zip) if command -v unzip >/dev/null 2>&1; then unzip -q "$archive" -d "$target_dir" else log_err "unzip not found" return 1 fi ;; *) log_err "Unknown archive format: $archive" return 1 ;; esac log_debug "Extraction completed" } install_binary() { local src="$1" local dst="$2" if [ ! -f "$src" ]; then log_err "Binary not found: $src" return 1 fi log_debug "Installing $src to $dst" # Create directory if it doesn't exist mkdir -p "$(dirname "$dst")" # Copy binary cp "$src" "$dst" # Make executable (for Unix-like systems) if [ "$PLATFORM" != "windows_amd64" ]; then chmod +x "$dst" fi log_info "Installed: $dst" } find_binary_in_dir() { local dir="$1" # Look for sky binary for pattern in "sky" "sky.exe" "sky-*" "sky.*"; do local file=$(find "$dir" -name "$pattern" -type f 2>/dev/null | head -1) if [ -n "$file" ] && [ -f "$file" ]; then echo "$file" return 0 fi done # If not found, check for any executable file if [ "$PLATFORM" = "windows_amd64" ]; then find "$dir" -name "*.exe" -type f 2>/dev/null | head -1 else find "$dir" -type f -executable 2>/dev/null | head -1 fi } get_latest_version() { # Since we're using static URLs, we'll use a default version # You can modify this to fetch from an API if available echo "latest" } main() { # Default values BINDIR="/usr/local/bin" DEBUG="0" PLATFORM="" VERSION="latest" BASE_URL="https://static.app.razgon.ru/src" # Parse arguments while getopts "b:dp:v:h" opt; do case "$opt" in b) BINDIR="$OPTARG" ;; d) DEBUG="1" ;; p) PLATFORM="$OPTARG" ;; v) VERSION="$OPTARG" ;; h|*) usage ;; esac done shift $((OPTIND - 1)) # Auto-detect platform if not specified if [ -z "$PLATFORM" ]; then PLATFORM=$(detect_platform) log_info "Auto-detected platform: $PLATFORM" fi # Validate platform case "$PLATFORM" in linux_amd64|windows_amd64) # Valid platforms ;; *) log_err "Unsupported platform: $PLATFORM" log_err "Supported platforms: linux_amd64, windows_amd64" exit 1 ;; esac # Get version if not specified if [ "$VERSION" = "latest" ]; then VERSION=$(get_latest_version) fi log_info "Installing sky $VERSION for $PLATFORM to $BINDIR" # Create temporary directory TMPDIR=$(mktemp -d) trap 'rm -rf "$TMPDIR"' EXIT log_debug "Using temporary directory: $TMPDIR" # Determine file name based on platform if [ "$PLATFORM" = "windows_amd64" ]; then FILENAME="sky-dev-windows_amd64.tar.gz" BINARY_NAME="sky.exe" else FILENAME="sky-dev-linux_amd64.tar.gz" BINARY_NAME="sky" fi # Download URL DOWNLOAD_URL="$BASE_URL/$FILENAME" log_info "Downloading from: $DOWNLOAD_URL" # Download the file ARCHIVE_PATH="$TMPDIR/$FILENAME" download_file "$DOWNLOAD_URL" "$ARCHIVE_PATH" # ARCHIVE_PATH="./dist/$FILENAME" # Extract EXTRACT_DIR="$TMPDIR/extracted" extract_tar "$ARCHIVE_PATH" "$EXTRACT_DIR" # Find the binary BINARY_PATH=$(find_binary_in_dir "$EXTRACT_DIR") if [ -z "$BINARY_PATH" ]; then log_err "Could not find sky binary in the archive" exit 1 fi log_debug "Found binary: $BINARY_PATH" # Install binary INSTALL_PATH="$BINDIR/$BINARY_NAME" install_binary "$BINARY_PATH" "$INSTALL_PATH" # Verify installation if [ -f "$INSTALL_PATH" ]; then if [ "$PLATFORM" != "windows_amd64" ]; then # Check if binary is executable if [ -x "$INSTALL_PATH" ]; then log_info "Verifying installation..." "$INSTALL_PATH" version 2>/dev/null || "$INSTALL_PATH" --version 2>/dev/null || true fi fi log_info "" log_info "✅ sky successfully installed to: $INSTALL_PATH" log_info " Platform: $PLATFORM" log_info " Version: $VERSION" log_info "" log_info "You can now run: $INSTALL_PATH" else log_err "Installation failed" exit 1 fi } # Run main function main "$@"