device-setup/dotfiles.sh

72 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
cd ~/.dotfiles
git pull
echo "Creating symbolic links for dotfiles..."
dirs_to_sym=(".config/alacritty/themes" ".zsh_functions")
sub_modules=(".config/alacritty/themes")
ignore_files=("README.md" ".git" ".gitmodules")
# Function to create symlinks recursively
create_symlinks() {
local src_dir="$1"
local target_dir="$2"
# Create the target directory if it doesn't exist
mkdir -p "$target_dir"
# Process all files, including hidden ones
local files=("$src_dir"/* "$src_dir"/.[!.]*)
for file in "${files[@]}"; do
# Skip if the file doesn't exist (can happen if no hidden files)
[ -e "$file" ] || continue
echo "Processing: $file"
# Get the file name
local file_name=$(basename "$file")
# Check if file should be ignored
if [[ " ${ignore_files[@]} " =~ " $file_name " ]]; then
echo "Ignoring: $file_name"
continue
fi
# If the file is a directory
if [ -d "$file" ]; then
# Check if the directory is in the list of directories to symlink
if [[ " ${dirs_to_sym[@]} " =~ " $file_name " ]]; then
# For these special directories, create the directory and symlink contents
mkdir -p "$target_dir/$file_name"
# If this directory is also a submodule, initialize it
if [[ " ${sub_modules[@]} " =~ " $file_name " ]]; then
echo "Initializing submodule: $file_name"
(cd "$file" && git submodule update --init --recursive)
fi
# Recursively create symlinks for the contents
create_symlinks "$file" "$target_dir/$file_name"
else
# For regular directories, create them and recurse
mkdir -p "$target_dir/$file_name"
create_symlinks "$file" "$target_dir/$file_name"
fi
else
# If the file is not a symlink already
if [ ! -L "$target_dir/$file_name" ]; then
# If the file exists
if [ -e "$target_dir/$file_name" ]; then
# Remove the file
echo "Removing existing: $target_dir/$file_name"
rm -rf "$target_dir/$file_name"
fi
# Create a symlink
echo "Linking: $file -> $target_dir/$file_name"
ln -s "$file" "$target_dir/$file_name"
fi
fi
done
}
# Call the function to create symlinks from dotfiles to home
create_symlinks "$HOME/.dotfiles" "$HOME"
echo "Dotfiles setup complete!"