-
Notifications
You must be signed in to change notification settings - Fork 0
install_script
Ihsan Tonuzi edited this page Mar 14, 2026
·
1 revision
#!/usr/bin/env bash
# Exit immediately if a command fails, treat unset variables as an error, and catch pipeline failures
set -euo pipefail
# --- Configuration Variables ---
DOT_DIR="$HOME/.dotfiles"
REPO_URL="ssh://git@codeberg.org/iton0/dotfiles.git"
SSH_KEY="$HOME/.ssh/id_ed25519"
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
INSTALL_SCRIPT="$HOME/.local/scripts/new_machine_setup/main_script"
# --- 1. Pre-flight Checks ---
echo "Starting dotfiles setup..."
# Check for Git, install dynamically if missing
if ! command -v git &> /dev/null; then
echo "Git is missing. Attempting to install..."
if command -v dnf &> /dev/null; then
sudo dnf install -y git
elif command -v apt-get &> /dev/null; then
sudo apt-get update && sudo apt-get install -y git
else
echo "CRITICAL ERROR: Neither dnf nor apt-get found. Please install Git manually." >&2
exit 1
fi
fi
# Fail fast if SSH key is missing
if [[ ! -f "$SSH_KEY" ]]; then
echo "CRITICAL ERROR: SSH key not found at $SSH_KEY" >&2
echo "Please copy your SSH keys to ~/.ssh/ before running this script." >&2
exit 1
fi
# --- 2. SSH & Network Setup ---
# (This MUST happen before the clone to prevent SSH connection hangs)
echo "Configuring SSH and verifying host..."
# Fix permissions
chmod 700 "$HOME/.ssh"
chmod 600 "$SSH_KEY"
[[ -f "${SSH_KEY}.pub" ]] && chmod 644 "${SSH_KEY}.pub"
# Start ssh-agent if not running, and ensure it closes when the script exits
if [[ -z "${SSH_AUTH_SOCK:-}" ]]; then
eval "$(ssh-agent -s)" > /dev/null
trap 'kill $SSH_AGENT_PID &>/dev/null' EXIT
fi
# Add key to agent if not already added
if ! ssh-add -l | grep -q "id_ed25519"; then
ssh-add "$SSH_KEY"
fi
# Auto-accept Codeberg host key safely
KNOWN_HOSTS="$HOME/.ssh/known_hosts"
touch "$KNOWN_HOSTS"
chmod 600 "$KNOWN_HOSTS"
if ! grep -q "codeberg.org" "$KNOWN_HOSTS" 2>/dev/null; then
echo "Adding codeberg.org to known_hosts..."
ssh-keyscan -t ed25519 codeberg.org >> "$KNOWN_HOSTS" 2>/dev/null
fi
# --- 3. Clone & Configure ---
echo "Setting up dotfiles repository..."
# Define the dot wrapper function (dynamically finding the git binary)
function dot() {
git --git-dir="$DOT_DIR" --work-tree="$HOME" "$@"
}
if [[ ! -d "$DOT_DIR" ]]; then
echo "Cloning bare repository..."
# --branch main prevents detached HEAD issues on fresh bare clones
git clone --bare --depth 1 --branch main --filter=blob:none \
-c user.name="iton0" \
-c user.email="iton224@protonmail.com" \
"$REPO_URL" "$DOT_DIR"
dot config remote.origin.fetch "+refs/heads/main:refs/remotes/origin/main"
else
echo "Dotfiles repo exists. Ensuring config is correct..."
dot config user.name "iton0"
dot config user.email "iton224@protonmail.com"
fi
# Apply specific dotfiles settings
dot config --local rerere.enabled true
dot config --local pull.rebase true
dot config --local core.excludesFile "$XDG_CONFIG_HOME/git/dotfiles_excludes"
# --- 4. Checkout & Sync ---
echo "Syncing and checking out files..."
dot fetch origin main
# Force checkout treats the repo as the single source of truth.
# WARN: This WILL overwrite any existing files in $HOME that match repo tracked files.
dot checkout -f main
# Link local main to upstream main securely
dot branch --set-upstream-to="origin/main" main
# --- 5. Post-Install ---
if [[ -x "$INSTALL_SCRIPT" ]]; then
echo "Executing post-install script..."
"$INSTALL_SCRIPT"
else
echo "Notice: Post-install script ($INSTALL_SCRIPT) not found or not executable. Skipping."
fi