#!/bin/bash set -e # Exit on any error # Configuration CLI_NAME="dochia-cli" CLI_NAME_LOWER="dochia" GITHUB_REPO="dochia-dev/dochia-cli" INSTALL_DIR="/usr/local/bin" TMP_DIR="/tmp/${CLI_NAME}-install" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Utility functions log() { echo -e "${GREEN}[INFO]${NC} $1" } warn() { echo -e "${YELLOW}[WARN]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" exit 1 } # Check if running as root for system-wide install check_permissions() { if [[ $EUID -ne 0 && "$INSTALL_DIR" == "/usr/local/bin" ]]; then warn "Installing to $INSTALL_DIR requires sudo privileges" if command -v sudo >/dev/null 2>&1; then SUDO="sudo" else error "sudo not found. Please run as root or install to a user directory." fi fi } # Detect OS and architecture detect_platform() { OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) case $ARCH in x86_64|amd64) ARCH="amd64" ;; arm64|aarch64) ARCH="arm64" ;; armv7l) ARCH="arm" ;; *) error "Unsupported architecture: $ARCH" ;; esac case $OS in linux) PLATFORM="linux" ;; darwin) # Use different naming for macOS based on architecture if [ "$ARCH" = "arm64" ]; then PLATFORM="mac" else PLATFORM="macos" fi ;; mingw*|cygwin*|msys*) PLATFORM="windows" ;; *) error "Unsupported operating system: $OS" ;; esac log "Detected platform: $PLATFORM-$ARCH" } # Get the latest release version from GitHub get_latest_version() { log "Fetching latest release version..." if command -v curl >/dev/null 2>&1; then LATEST_VERSION=$(curl -s "https://api.github.com/repos/$GITHUB_REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v?([^"]+)".*/\1/') elif command -v wget >/dev/null 2>&1; then LATEST_VERSION=$(wget -qO- "https://api.github.com/repos/$GITHUB_REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v?([^"]+)".*/\1/') else error "Neither curl nor wget found. Please install one of them." fi if [ -z "$LATEST_VERSION" ]; then error "Failed to fetch latest version" fi log "Latest version: $LATEST_VERSION" } # Download and extract the binary download_binary() { # Construct download URL based on your release naming convention # Format: dochia_linux_amd64_1.0.3.tar.gz # Note: LATEST_VERSION contains the full tag (e.g., dochia-cli-1.0.3) # but archive name only needs the version number (e.g., 1.0.3) VERSION_NUMBER=$(echo "$LATEST_VERSION" | sed 's/^dochia-cli-//') ARCHIVE_NAME="${CLI_NAME}_${PLATFORM}_${ARCH}_${VERSION_NUMBER}.tar.gz" DOWNLOAD_URL="https://github.com/$GITHUB_REPO/releases/download/${LATEST_VERSION}/$ARCHIVE_NAME" log "Downloading $CLI_NAME from $DOWNLOAD_URL" # Create temporary directory mkdir -p "$TMP_DIR" cd "$TMP_DIR" # Download archive if command -v curl >/dev/null 2>&1; then curl -sL "$DOWNLOAD_URL" -o "$ARCHIVE_NAME" elif command -v wget >/dev/null 2>&1; then wget -q "$DOWNLOAD_URL" -O "$ARCHIVE_NAME" fi if [ ! -f "$ARCHIVE_NAME" ]; then error "Failed to download archive from $DOWNLOAD_URL" fi log "Extracting archive..." # Extract the archive if command -v tar >/dev/null 2>&1; then tar -xzf "$ARCHIVE_NAME" else error "tar command not found. Cannot extract archive." fi # Find the binary (it might be in a subdirectory or directly in the archive) BINARY_PATH="" if [ "$PLATFORM" = "windows" ]; then BINARY_NAME="dochia.exe" else BINARY_NAME="dochia" fi # Look for the binary in current directory and subdirectories BINARY_PATH=$(find . -name "$BINARY_NAME" -type f | head -1) if [ -z "$BINARY_PATH" ] || [ ! -f "$BINARY_PATH" ]; then error "Binary '$BINARY_NAME' not found in extracted archive" fi # Move binary to a predictable location and make it executable # Only copy if the binary is not already in the expected location if [ "$BINARY_PATH" != "./$CLI_NAME" ]; then cp "$BINARY_PATH" "./$CLI_NAME" fi chmod +x "./$CLI_NAME" log "Binary extracted successfully" } # Install the binary install_binary() { log "Installing $CLI_NAME to $INSTALL_DIR" # Create install directory if it doesn't exist $SUDO mkdir -p "$INSTALL_DIR" # Copy binary to install directory $SUDO cp "$CLI_NAME" "$INSTALL_DIR/$CLI_NAME" # Verify installation if [ -x "$INSTALL_DIR/$CLI_NAME" ]; then log "✅ $CLI_NAME_LOWER installed successfully!" log "Run '$CLI_NAME_LOWER --help' to get started" else error "Installation failed" fi } # Cleanup temporary files cleanup() { if [ -d "$TMP_DIR" ]; then rm -rf "$TMP_DIR" fi } # Main installation process main() { log "Installing $CLI_NAME..." # Set up cleanup trap trap cleanup EXIT check_permissions detect_platform get_latest_version download_binary install_binary log "🎉 Installation completed successfully!" log "" log "To use $CLI_NAME_LOWER, run: $CLI_NAME_LOWER --help" # Check if install directory is in PATH if ! echo "$PATH" | grep -q "$INSTALL_DIR"; then warn "$INSTALL_DIR is not in your PATH. Add it to your shell profile:" warn "echo 'export PATH="$INSTALL_DIR:\$PATH"' >> ~/.bashrc" warn "source ~/.bashrc" fi } # Run main function main "$@"