#!/bin/bash
# ╔══════════════════════════════════════════════════════════════════════════════╗
# ║  install.sh — Legacy manual dotfiles installer                             ║
# ║                                                                              ║
# ║  PURPOSE:                                                                    ║
# ║    The original, non-TUI entry point for setting up the dotfiles on an      ║
# ║    already-running Arch Linux system. Runs modules sequentially with        ║
# ║    minimal user interaction (a single DE selection prompt).                 ║
# ║                                                                              ║
# ║  USAGE:  bash ~/Dotfiles/setup/install.sh                                  ║
# ║                                                                              ║
# ║  SUPERSEDED BY: tui-install.sh / simple-install.sh (preferred)             ║
# ║  This file is kept for quick headless runs or scripted CI deployments.     ║
# ╚══════════════════════════════════════════════════════════════════════════════╝

set -uo pipefail
# -u: treat unset variables as errors (catches typos in variable names)
# -o pipefail: if any command in a pipeline fails, the whole pipeline fails
# (Note: -e is intentionally omitted so module failures don't abort the whole run)

# ── Logging setup ─────────────────────────────────────────────────────────────
# All output is tee'd to a log file so the run can be reviewed after the fact.

LOG="$HOME/dotfiles-install.log"

# Truncate the log file at the start of each run (> with no input empties it)
> "$LOG"

# Stamp the log with a timestamp for traceability
printf "Dotfiles install: %s\n" "$(date)" >> "$LOG"

# Redirect both stdout and stderr through tee so they appear on screen AND
# get appended to the log file simultaneously.
# exec > >(tee -a ...) 2>&1 is a permanent redirect for the rest of the script.
exec > >(tee -a "$LOG") 2>&1

# ── Core installation modules ──────────────────────────────────────────────────
# These three modules form the mandatory base layer and must run in this order:
#   1. package-managers  — installs yay (AUR helper), nvm (Node.js), rustup (Rust)
#   2. core-packages     — installs 100+ system packages via pacman/yay
#   3. core             — enables systemd services (NetworkManager, cronie, greetd, etc.)

echo "Running Core installation Scripts"
bash ~/Dotfiles/setup/modules/package-managers.sh
bash ~/Dotfiles/setup/modules/core-packages.sh
bash ~/Dotfiles/setup/modules/core.sh

# ── Shell configuration deployment ────────────────────────────────────────────
# Deploys shell dotfiles (zsh, neovim, yazi, starship, etc.) and installs
# oh-my-zsh with its plugins. Run after core because it needs packages from core.

echo "Running Shell config deployment Script"
bash ~/Dotfiles/setup/modules/shell-setup.sh

# ── Desktop environment selection ─────────────────────────────────────────────
# Prompts for a single keypress to choose which DE installer to run.
# -n1: read exactly one character without needing Enter
# -p: display the prompt inline

read -n1 -p "what DE to install? [hyprland,sway,none]" doit
case $doit in
  # HyprLua is the primary/recommended DE — Hyprland configured via Lua scripts
  hyprland) bash ~/Dotfiles/setup/modules/Desktop-Environments/hyprland.sh ;;
  # Sway — Wayland tiling compositor, i3-compatible
  sway)     bash ~/Dotfiles/setup/modules/Desktop-Environments/sway.sh ;;
  # Skip DE entirely (for headless/server setups)
  none)     echo "Skipping DE installation" ;;
  # Catch-all for invalid input
  *)        echo "please choose a desktop environment to install" ;;
esac

# ── Optional application modules ──────────────────────────────────────────────
# These are intentionally commented out — uncomment the ones you want.
# Each is a self-contained idempotent module that installs a specific app.
# In the TUI installer (tui-install.sh), these are presented as a checklist.

# Optional apps — uncomment what you want:
# bash ~/Dotfiles/setup/modules/optional-Modules/apps/steam.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/apps/vesktop.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/apps/spotify.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/apps/prismlauncher.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/apps/vintagestory.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/apps/localsend.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/apps/croc.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/apps/onlyoffice.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/apps/wireshark.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/apps/k8s.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/python.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/zfs.sh
# bash ~/Dotfiles/setup/modules/optional-Modules/wprs.sh

# ── Done ──────────────────────────────────────────────────────────────────────
# Print a final message pointing the user to the log file for review.
printf "\nDone. Log: %s\n" "$LOG"
