dotfiles/modules/02-symlinks.sh

129 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# =====================
# Dotfiles Symlinking Script
# =====================
# - Symlinks all dotfiles and app configs from the repo to $HOME and $HOME/.config
# - Handles device-specific fragments for Hyprland, Waybar, etc. using $DOTFILES_DEVICE and $HOSTNAME
# - Idempotent and safe to re-run
# - To extend, add new fragment types to the fragment_types array below
# =====================
mkdir -p "$HOME/.config"
echo "Symlinking dotfiles..."
# Get the dotfiles directory (parent of modules directory)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOTFILES_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
# Symlink regular dotfiles (excluding .config directory)
for file in $(find "$DOTFILES_DIR" -maxdepth 1 -name ".*"); do
[ "$file" = "$DOTFILES_DIR/.git" ] && continue
[ "$file" = "$DOTFILES_DIR/.github" ] && continue
[ "$file" = "$DOTFILES_DIR/.vscode" ] && continue
[ "$file" = "$DOTFILES_DIR/.config" ] && continue # Handle .config separately
basefile="$(basename "$file")"
if [ -f "$file" ] || [ -d "$file" ]; then
ln -sf "$file" "$HOME/$basefile"
echo "Linked $basefile"
fi
done
# Symlink .local/bin and .local/share files recursively
for SUBDIR in bin share; do
LOCAL_REPO="$DOTFILES_DIR/.local/$SUBDIR"
LOCAL_HOME="$HOME/.local/$SUBDIR"
if [ -d "$LOCAL_REPO" ]; then
mkdir -p "$LOCAL_HOME"
echo "Symlinking .local/$SUBDIR files (recursive)..."
find "$LOCAL_REPO" -type f | while read -r srcfile; do
relpath="${srcfile#$LOCAL_REPO/}"
targetfile="$LOCAL_HOME/$relpath"
targetdir="$(dirname "$targetfile")"
mkdir -p "$targetdir"
[ -L "$targetfile" ] && rm "$targetfile"
[ -f "$targetfile" ] && rm "$targetfile"
ln -sf "$srcfile" "$targetfile"
echo "Linked $srcfile -> $targetfile"
done
fi
done
# Symlink .config directory contents
CONFIG_DIR="$DOTFILES_DIR/.config"
if [ -d "$CONFIG_DIR" ]; then
echo "Symlinking .config contents (files only, recursive)..."
find "$CONFIG_DIR" -type f | while read -r srcfile; do
# Get relative path from $CONFIG_DIR
relpath="${srcfile#$CONFIG_DIR/}"
targetfile="$HOME/.config/$relpath"
targetdir="$(dirname "$targetfile")"
# If targetdir is a symlink, remove it first
[ -L "$targetdir" ] && rm "$targetdir"
mkdir -p "$targetdir"
# Remove existing file/symlink at target
[ -L "$targetfile" ] && rm "$targetfile"
[ -f "$targetfile" ] && rm "$targetfile"
ln -sf "$srcfile" "$targetfile"
echo "Linked $srcfile -> $targetfile"
done
# =====================
# Device-specific symlinks for Hyprland and Waybar
# =====================
# This section handles device-specific config fragments for Hyprland, Waybar, etc.
# Uses $DOTFILES_DEVICE (set by setup.sh) and $HOSTNAME for profile-specific fragments.
# To add new device types (e.g., tablet, server), update host-profiles.conf and add fragments.
# To add new fragment types, extend the fragment_types array below.
# Format: "source_fragment_path:target_symlink_path"
# Example: fragment_types=(
# "hypr/includes/monitors-$HOSTNAME.conf:hypr/includes/monitors.conf"
# "hypr/includes/hypridle-$DOTFILES_DEVICE.conf:hypr/hypridle.conf"
# "waybar/config-$DOTFILES_DEVICE:waybar/config"
# )
if [ -z "$DOTFILES_DEVICE" ]; then
echo "ERROR: DOTFILES_DEVICE environment variable not set. Run setup.sh to detect device profile."
exit 1
fi
HOSTNAME=$(hostname)
# Device-specific fragments to symlink (source:target)
fragment_types=(
"hypr/includes/monitors-$HOSTNAME.conf:hypr/includes/monitors.conf" # Monitor layout per host
"hypr/includes/hypridle-$DOTFILES_DEVICE.conf:hypr/hypridle.conf" # Idle config per device type
"waybar/config-$DOTFILES_DEVICE:waybar/config" # Waybar config per device type
)
# Symlink each device-specific fragment
for fragment in "${fragment_types[@]}"; do
src="${CONFIG_DIR}/${fragment%%:*}"
tgt_rel="${fragment##*:}"
tgt="$HOME/.config/$tgt_rel"
tgt_dir="$(dirname "$tgt")"
mkdir -p "$tgt_dir"
if [ -f "$src" ]; then
# Remove existing file/symlink at target
[ -L "$tgt" ] && rm "$tgt"
[ -f "$tgt" ] && rm "$tgt"
ln -sf "$src" "$tgt"
echo "Linked $src -> $tgt"
else
echo "Warning: Device-specific fragment missing: $src"
# To troubleshoot, ensure the fragment exists for your device/host
fi
done
# =====================
# End device-specific symlinking
# =====================
fi
echo "====================="
echo "Dotfiles symlinking complete!"
echo "====================="