#!/bin/bash

# PetCareScript Linux/Mac Installer

VERSION="1.0.0"
INSTALL_DIR="/usr/local/lib/petcarescript"
BIN_DIR="/usr/local/bin"
REPO_URL="https://github.com/petcarescript/petcarescript/archive/refs/tags/v${VERSION}.tar.gz"

install_petcarescript() {
    echo "Installing PetCareScript v${VERSION}..."
    
    # Check if running as root
    if [ "$EUID" -ne 0 ]; then 
        echo "Please run as root (use sudo)"
        exit 1
    fi
    
    # Check if Node.js is installed
    if ! command -v node &> /dev/null; then
        echo "Error: Node.js is required but not found."
        echo "Please install Node.js (>=14.0.0) and try again."
        echo "Visit: https://nodejs.org"
        exit 1
    fi
    
    NODE_VERSION=$(node --version)
    echo "Node.js found: $NODE_VERSION"
    
    # Create installation directory
    mkdir -p "$INSTALL_DIR"
    mkdir -p "$BIN_DIR"
    mkdir -p "$INSTALL_DIR/icons"
    
    # Copy installation files (in real scenario, would extract from package)
    echo "Setting up PetCareScript installation..."
    
    # Generate icons
    if [ -f "$INSTALL_DIR/../tools/generate-icons.js" ]; then
        echo "Generating icons..."
        cd "$INSTALL_DIR/.."
        node tools/generate-icons.js
        cp -r icons/* "$INSTALL_DIR/icons/"
    fi
    
    # Create a simple wrapper script
    cat > "$BIN_DIR/pcs" << 'EOF'
#!/bin/bash
node "$INSTALL_DIR/index.js" "$@"
EOF
    
    # Make executable
    chmod +x "$BIN_DIR/pcs"
    
    # Install file associations if on Linux
    if [ "$(uname)" = "Linux" ]; then
        echo "Installing file associations..."
        
        # Install MIME type
        if [ -d "/usr/share/mime/packages" ]; then
            cp "$INSTALL_DIR/../mime/petcarescript.xml" "/usr/share/mime/packages/"
            update-mime-database "/usr/share/mime" 2>/dev/null || true
        fi
        
        # Install desktop file
        if [ -d "/usr/share/applications" ]; then
            cp "$INSTALL_DIR/../linux/petcarescript.desktop" "/usr/share/applications/"
            update-desktop-database "/usr/share/applications" 2>/dev/null || true
        fi
        
        # Install icons
        ICONS_BASE="/usr/share/icons/hicolor"
        if [ -d "$ICONS_BASE" ]; then
            for size in 16 32 48 64 128 256; do
                SIZE_DIR="$ICONS_BASE/${size}x${size}/mimetypes"
                mkdir -p "$SIZE_DIR"
                
                if [ -f "$INSTALL_DIR/icons/petcarescript-${size}.png" ]; then
                    cp "$INSTALL_DIR/icons/petcarescript-${size}.png" "$SIZE_DIR/application-x-petcarescript.png"
                fi
            done
            
            # Install SVG icon
            SVG_DIR="$ICONS_BASE/scalable/mimetypes"
            mkdir -p "$SVG_DIR"
            if [ -f "$INSTALL_DIR/icons/petcarescript.svg" ]; then
                cp "$INSTALL_DIR/icons/petcarescript.svg" "$SVG_DIR/application-x-petcarescript.svg"
            fi
            
            # Update icon cache
            gtk-update-icon-cache "$ICONS_BASE" 2>/dev/null || true
        fi
        
        echo "✅ File associations installed!"
    fi
    
    echo "PetCareScript v${VERSION} installed successfully!"
    echo "You can now use 'pcs' command from any terminal."
    echo "Files with .pcs extension should now show the PetCareScript icon."
}

uninstall_petcarescript() {
    echo "Uninstalling PetCareScript..."
    
    # Check if running as root
    if [ "$EUID" -ne 0 ]; then 
        echo "Please run as root (use sudo)"
        exit 1
    fi
    
    # Remove file associations
    if [ "$(uname)" = "Linux" ]; then
        echo "Removing file associations..."
        
        # Remove MIME type
        rm -f "/usr/share/mime/packages/petcarescript.xml"
        update-mime-database "/usr/share/mime" 2>/dev/null || true
        
        # Remove desktop file
        rm -f "/usr/share/applications/petcarescript.desktop"
        update-desktop-database "/usr/share/applications" 2>/dev/null || true
        
        # Remove icons
        ICONS_BASE="/usr/share/icons/hicolor"
        for size in 16 32 48 64 128 256; do
            rm -f "$ICONS_BASE/${size}x${size}/mimetypes/application-x-petcarescript.png"
        done
        rm -f "$ICONS_BASE/scalable/mimetypes/application-x-petcarescript.svg"
        
        # Update icon cache
        gtk-update-icon-cache "$ICONS_BASE" 2>/dev/null || true
    fi
    
    # Remove installation directory
    rm -rf "$INSTALL_DIR"
    
    # Remove binary
    rm -f "$BIN_DIR/pcs"
    
    echo "PetCareScript uninstalled successfully!"
}

show_help() {
    echo "PetCareScript Installer v${VERSION}"
    echo ""
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  --uninstall    Uninstall PetCareScript"
    echo "  --help         Show this help"
    echo ""
    echo "Examples:"
    echo "  sudo $0                # Install PetCareScript"
    echo "  sudo $0 --uninstall   # Uninstall PetCareScript"
}

# Parse arguments
case "$1" in
    --uninstall)
        uninstall_petcarescript
        ;;
    --help)
        show_help
        ;;
    "")
        install_petcarescript
        ;;
    *)
        echo "Unknown option: $1"
        echo "Use --help for usage information"
        exit 1
        ;;
esac