#!/bin/bash
# Santa Package Manager Installation Script
# Generated: {{ timestamp }}
# Version: {{ version }}
# Source: {{ source_name }}
# Packages: {{ package_count }}

set -euo pipefail

echo "🎅 Santa Package Installation - {{ source_name }}"
echo "Generated: {{ timestamp }}"
echo "Packages to install: {{ package_count }}"
echo ""

{% if manager == "brew" %}
# Homebrew package installation
if command -v brew &> /dev/null; then
    echo "📦 Installing Homebrew packages..."
    {% for package in packages %}
    echo "Installing {{ package }}..."
    brew install {{ package | shell_escape }}
    {% endfor %}
    echo "✅ Homebrew installation completed"
else
    echo "⚠️  Homebrew not found. Please install Homebrew first:"
    echo "   /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
    echo "   Then run this script again."
    exit 1
fi

{% elif manager == "apt" or manager == "apt-get" %}
# APT package installation (Debian/Ubuntu)
if command -v apt &> /dev/null; then
    echo "📦 Installing APT packages..."
    echo "Updating package lists..."
    sudo apt update
    
    {% for package in packages %}
    echo "Installing {{ package }}..."
    sudo apt install -y {{ package | shell_escape }}
    {% endfor %}
    echo "✅ APT installation completed"
else
    echo "⚠️  APT not found. This script is for Debian/Ubuntu systems."
    exit 1
fi

{% elif manager == "yum" %}
# YUM package installation (RedHat/CentOS)
if command -v yum &> /dev/null; then
    echo "📦 Installing YUM packages..."
    {% for package in packages %}
    echo "Installing {{ package }}..."
    sudo yum install -y {{ package | shell_escape }}
    {% endfor %}
    echo "✅ YUM installation completed"
else
    echo "⚠️  YUM not found. This script is for RedHat/CentOS systems."
    exit 1
fi

{% elif manager == "pacman" %}
# Pacman package installation (Arch Linux)
if command -v pacman &> /dev/null; then
    echo "📦 Installing Pacman packages..."
    {% for package in packages %}
    echo "Installing {{ package }}..."
    sudo pacman -S --noconfirm {{ package | shell_escape }}
    {% endfor %}
    echo "✅ Pacman installation completed"
else
    echo "⚠️  Pacman not found. This script is for Arch Linux systems."
    exit 1
fi

{% elif manager == "npm" %}
# NPM package installation
if command -v npm &> /dev/null; then
    echo "📦 Installing NPM packages globally..."
    {% for package in packages %}
    echo "Installing {{ package }}..."
    npm install -g {{ package | shell_escape }}
    {% endfor %}
    echo "✅ NPM installation completed"
else
    echo "⚠️  NPM not found. Please install Node.js and NPM first."
    exit 1
fi

{% elif manager == "pip" or manager == "pip3" %}
# Python pip package installation
if command -v {{ manager }} &> /dev/null; then
    echo "📦 Installing Python packages with {{ manager }}..."
    {% for package in packages %}
    echo "Installing {{ package }}..."
    {{ manager }} install {{ package | shell_escape }}
    {% endfor %}
    echo "✅ Python package installation completed"
else
    echo "⚠️  {{ manager }} not found. Please install Python and pip first."
    exit 1
fi

{% elif manager == "cargo" %}
# Rust cargo package installation
if command -v cargo &> /dev/null; then
    echo "📦 Installing Rust packages with cargo..."
    {% for package in packages %}
    echo "Installing {{ package }}..."
    cargo install {{ package | shell_escape }}
    {% endfor %}
    echo "✅ Cargo installation completed"
else
    echo "⚠️  Cargo not found. Please install Rust toolchain first:"
    echo "   curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
    exit 1
fi

{% elif manager == "go" %}
# Go package installation
if command -v go &> /dev/null; then
    echo "📦 Installing Go packages..."
    {% for package in packages %}
    echo "Installing {{ package }}..."
    go install {{ package | shell_escape }}
    {% endfor %}
    echo "✅ Go installation completed"
else
    echo "⚠️  Go not found. Please install Go first."
    exit 1
fi

{% else %}
# Generic package manager
if command -v {{ manager }} &> /dev/null; then
    echo "📦 Installing packages with {{ manager }}..."
    {% for package in packages %}
    echo "Installing {{ package }}..."
    {{ manager }} install {{ package | shell_escape }}
    {% endfor %}
    echo "✅ Installation completed"
else
    echo "⚠️  {{ manager }} not found. Please install {{ manager }} first."
    exit 1
fi
{% endif %}

echo ""
echo "🎉 All {{ package_count }} packages have been processed!"
echo "Generated by Santa v{{ version }} - https://github.com/tylerbutler/santa"