#!/bin/sh
set -e

# gitsw installer
# Usage: curl -fsSL https://raw.githubusercontent.com/yigiterdev/gitsw/main/install.sh | sh

REPO="yigiterdev/gitsw"
BINARY_NAME="gitsw"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

info() {
    printf "${GREEN}info:${NC} %s\n" "$1"
}

warn() {
    printf "${YELLOW}warn:${NC} %s\n" "$1"
}

error() {
    printf "${RED}error:${NC} %s\n" "$1"
    exit 1
}

# Detect OS
detect_os() {
    case "$(uname -s)" in
        Linux*)  echo "linux" ;;
        Darwin*) echo "darwin" ;;
        MINGW*|MSYS*|CYGWIN*) echo "windows" ;;
        *) error "Unsupported operating system: $(uname -s)" ;;
    esac
}

# Detect architecture
detect_arch() {
    case "$(uname -m)" in
        x86_64|amd64) echo "x86_64" ;;
        arm64|aarch64) echo "aarch64" ;;
        *) error "Unsupported architecture: $(uname -m)" ;;
    esac
}

# Get download URL for latest release
get_download_url() {
    local os=$1
    local arch=$2
    local target=""
    local ext="tar.gz"

    case "$os" in
        linux)
            target="${arch}-unknown-linux-gnu"
            ;;
        darwin)
            target="${arch}-apple-darwin"
            ;;
        windows)
            target="${arch}-pc-windows-msvc"
            ext="zip"
            ;;
    esac

    echo "https://github.com/${REPO}/releases/latest/download/${BINARY_NAME}-${target}.${ext}"
}

# Main installation
main() {
    echo ""
    echo "  ┌─────────────────────────────────────┐"
    echo "  │       gitsw installer               │"
    echo "  │   Smart Git Branch Switcher         │"
    echo "  └─────────────────────────────────────┘"
    echo ""

    # Detect platform
    OS=$(detect_os)
    ARCH=$(detect_arch)

    info "Detected platform: ${OS}-${ARCH}"

    # Get download URL
    DOWNLOAD_URL=$(get_download_url "$OS" "$ARCH")
    info "Downloading from: ${DOWNLOAD_URL}"

    # Create temp directory
    TMP_DIR=$(mktemp -d)
    trap "rm -rf $TMP_DIR" EXIT

    # Download
    if command -v curl > /dev/null 2>&1; then
        curl -fsSL "$DOWNLOAD_URL" -o "$TMP_DIR/gitsw.tar.gz"
    elif command -v wget > /dev/null 2>&1; then
        wget -q "$DOWNLOAD_URL" -O "$TMP_DIR/gitsw.tar.gz"
    else
        error "Neither curl nor wget found. Please install one of them."
    fi

    # Extract
    info "Extracting..."
    cd "$TMP_DIR"
    if [ "$OS" = "windows" ]; then
        unzip -q gitsw.tar.gz
    else
        tar xzf gitsw.tar.gz
    fi

    # Install
    info "Installing to ${INSTALL_DIR}..."

    # Check if we need sudo
    if [ -w "$INSTALL_DIR" ]; then
        mv "$BINARY_NAME" "$INSTALL_DIR/"
    else
        warn "Need sudo to install to ${INSTALL_DIR}"
        sudo mv "$BINARY_NAME" "$INSTALL_DIR/"
    fi

    chmod +x "$INSTALL_DIR/$BINARY_NAME"

    # Verify installation
    if command -v gitsw > /dev/null 2>&1; then
        echo ""
        info "Successfully installed gitsw!"
        echo ""
        gitsw --version
        echo ""
        echo "  Run 'gitsw --help' to get started"
        echo ""
    else
        warn "Installed to ${INSTALL_DIR}/${BINARY_NAME}"
        warn "Make sure ${INSTALL_DIR} is in your PATH"
    fi
}

main "$@"
