#!/bin/bash
# Script to setup a new VM with essential tools and configurations including:
# - Git and GitHub CLI installation and configuration
# - Downloading bash aliases

#  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#  ┃                Colors                 ┃
#  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
YELLOW="\033[1;33m"
GREEN="\033[1;32m"
RED="\033[1;31m"
NC="\033[0m" # No Color

info() { echo -e "  ${GREEN}[INFO]${NC} $1"; }
step() { echo -e "${YELLOW}>>> $1${NC}"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }

#  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#  ┃                Aliases                ┃
#  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
setup_aliases() {
    step "Downloading bash aliases..."
    wget https://files.postrach.cc/bash_aliases -O ~/.bash_aliases -q
    source ~/.bashrc
    info "Bash aliases downloaded and sourced."
}

#  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#  ┃                Packages               ┃
#  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
PACKAGES=(
  git gh
  curl wget unzip zip
  htop net-tools
  tree
  python3.12-venv
)

install_packages() {
    step "Installing packages..."
    sudo apt update -y >/dev/null 2>&1
    # install in one shot; skip already-installed packages automatically
    sudo apt install -y "${PACKAGES[@]}" >/dev/null 2>&1
    info "Package installation finished."
}

#  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#  ┃                  Git                  ┃
#  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
setup_git() {
    step "Installing Git..."
    if gh --version >/dev/null 2>&1; then
        info "Git is already installed."
    else
        sudo apt update -y >/dev/null 2>&1
        sudo apt install -y git
        info "Git installed."
    fi
    

    step "Configuring Git..."
    git config --global user.email "kamil.postrach@gmail.com"
    git config --global user.name "Postrach"
    git config --global advice.forceDeleteBranch false
    git config --global push.autoSetupRemote true
    info "Git configured."

    step "Installing GitHub CLI (gh)..."
    if gh --version >/dev/null 2>&1; then
        info "GitHub CLI is already installed."
    else
        (type -p wget >/dev/null || (sudo apt update && sudo apt install wget -y)) \
            && sudo mkdir -p -m 755 /etc/apt/keyrings \
            && out=$(mktemp) \
            && wget -nv -O "$out" https://cli.github.com/packages/githubcli-archive-keyring.gpg \
            && sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null < "$out" \
            && sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
            && sudo mkdir -p -m 755 /etc/apt/sources.list.d \
            && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \
                | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
            && sudo apt update >/dev/null 2>&1\
            && sudo apt install -y gh \
            && info "GitHub CLI installed."
    fi
}

#  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#  ┃           Check GitHub Auth           ┃
#  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
check_github_auth() {
    step "Checking GitHub authentication..."
    if gh auth status >/dev/null 2>&1; then
        info "GitHub CLI is authenticated."
    else
        info "GitHub CLI is not authenticated. Please run 'gh auth login' to authenticate."
    fi
}

#  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#  ┃               Ansible                 ┃
#  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
configure_ansible_repo() {
    step "Configuring Ansible..."
    if gh auth status >/dev/null 2>&1; then
        if ls -la ~/ | grep ansible >/dev/null 2>&1; then
            info "Ansible already exists."
        else
            info "Creating Ansible directory..."
            mkdir ~/ansible
            info "Cloning Ansible repository..."
            git clone https://github.com/Postrach/Ansible.git ~/ansible
            info "Ansible repository cloned."
        fi
    else
        info "GitHub CLI is not authenticated. Please run 'gh auth login' to authenticate."
    fi
}

#  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#  ┃               Ansible                 ┃
#  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
configure_venv() {
    step "Setting up Python virtual environment for Ansible..."
    if python3 -m venv --help >/dev/null 2>&1; then
        if [ -d "~/ansible/venv" ]; then
            info "Virtual environment already exists."
        else
            python3 -m venv ~/ansible/venv
            source ~/ansible/venv/bin/activate
            pip install --upgrade pip >/dev/null 2>&1
            pip install ansible >/dev/null 2>&1
            chmod +x ~/ansible/venv/bin/activate
            info "Python virtual environment for Ansible set up."
        fi
    else
        info "Python venv module is not available. Please install Python 3 and the venv module."
    fi
}

#  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#  ┃               CLI Parser              ┃
#  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
usage() {
    cat <<EOF
Usage: $0 [options]

Options:
  --all             Run all setup (default when no args)
  --git             Run only Git + gh setup
  --aliases         Download and source bash aliases
  --ansible_repo    Configure Ansible
  --packages        Install predefined Ubuntu packages
  --venv            Setup Python virtual environment for Ansible
  -h, --help        Show this help
EOF
}

if [ "$#" -eq 0 ]; then
    # default: run everything
    setup_aliases
    setup_git
    configure_ansible_repo
    install_packages
    configure_venv
    check_github_auth
    exit 0
fi

while [ "$#" -gt 0 ]; do
    case "$1" in
        --all)
            setup_aliases
            setup_git
            configure_ansible_repo
            install_packages
            configure_venv
            check_github_auth
            shift
            ;;
        --git)
            setup_git
            check_github_auth
            shift
            ;;
        --aliases)
            setup_aliases
            shift
            ;;
        --ansible_repo)
            configure_ansible_repo
            shift
            ;;
        --packages)
            install_packages
            shift
            ;;
        --venv)
            configure_venv
            shift
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            error "Unknown argument: $1"
            usage
            exit 1
            ;;
    esac
done
