diff --git a/dotfiles.sh b/dotfiles.sh index 3f09e28..c61cdc8 100755 --- a/dotfiles.sh +++ b/dotfiles.sh @@ -1,85 +1,52 @@ #!/bin/bash -cd ~/.dotfiles +# Check if ~/.dotfiles exists +if [ ! -d "$HOME/.dotfiles" ]; then + echo "Error: ~/.dotfiles directory does not exist." + echo "Please clone your dotfiles repository to ~/.dotfiles first." + exit 1 +fi + +cd ~/.dotfiles || exit 1 git pull echo "Creating symbolic links for dotfiles..." -# Convert paths to array of directory names to special handle -dirs_to_sym=(".config/alacritty/themes" ".zsh_functions") -sub_modules=(".config/alacritty/themes") -ignore_files=("README.md" ".git" ".gitmodules") +ignore_files=("dotfiles.sh" "README.md" ".git" ".gitignore") +backup_dir="$HOME/.dotfiles_backup/$(date +%Y%m%d_%H%M%S)" -# Function to create symlinks recursively -create_symlinks() { - local src_dir="$1" - local target_dir="$2" - local is_root="$3" # Flag to indicate if this is the root call - - # 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 +# For each file/dir in the dotfiles directory +for file in $(ls -A); do + # If the file is not in the ignore list + if [[ ! " ${ignore_files[@]} " =~ " ${file} " ]]; then + target="$HOME/$file" + source="$PWD/$file" - # Get the file name - local file_name=$(basename "$file") - local rel_path=${file#$HOME/.dotfiles/} # Get relative path - - # Check if file should be ignored - if [[ " ${ignore_files[@]} " =~ " $file_name " ]]; then - echo "Ignoring: $file_name" + # Skip if it's already a correct symlink + if [ -L "$target" ] && [ "$(readlink "$target")" = "$source" ]; then + echo "✓ Symlink for $file already exists and is correct." continue fi - # If the file is a directory - if [ -d "$file" ]; then - # Handle directory symlinking based on whether it's in the special directories list - if [[ "$is_root" == "true" && "$file_name" == ".config" ]]; then - # For .config directory, we want to create it and process its contents differently - mkdir -p "$target_dir/.config" - create_symlinks "$file" "$target_dir/.config" "false" - elif [[ " ${dirs_to_sym[@]} " =~ " $rel_path " ]]; then - # Special directory that needs special handling - echo "Special directory: $rel_path" - mkdir -p "$target_dir/$file_name" - - # Handle submodule initialization if needed - if [[ " ${sub_modules[@]} " =~ " $rel_path " ]]; then - echo "Initializing submodule: $rel_path" - (cd "$file" && git submodule update --init --recursive) - fi - - # Process the contents - create_symlinks "$file" "$target_dir/$file_name" "false" - else - # For regular directories in our dotfiles, we want to symlink them directly - if [ ! -L "$target_dir/$file_name" ]; then - if [ -e "$target_dir/$file_name" ]; then - echo "Removing existing: $target_dir/$file_name" - rm -rf "$target_dir/$file_name" - fi - echo "Linking directory: $file -> $target_dir/$file_name" - ln -s "$file" "$target_dir/$file_name" - fi - fi - else - # For regular files, always create symlinks - if [ ! -L "$target_dir/$file_name" ]; then - if [ -e "$target_dir/$file_name" ]; then - echo "Removing existing: $target_dir/$file_name" - rm -rf "$target_dir/$file_name" - fi - echo "Linking file: $file -> $target_dir/$file_name" - ln -s "$file" "$target_dir/$file_name" - fi + # Backup existing file/directory if it exists + if [ -e "$target" ]; then + mkdir -p "$backup_dir" + echo "⚠ Backing up existing $file to $backup_dir/" + mv "$target" "$backup_dir/$file" fi - done -} + + # Create a symbolic link + if [ -d "$source" ]; then + echo "Creating symbolic link for directory: $file" + else + echo "Creating symbolic link for file: $file" + fi + + if ln -s "$source" "$target"; then + echo "✓ Successfully linked $file" + else + echo "✗ Failed to create symlink for $file" + fi + fi +done -# Call the function to create symlinks from dotfiles to home -create_symlinks "$HOME/.dotfiles" "$HOME" "true" - -echo "Dotfiles setup complete!" \ No newline at end of file +echo "Dotfiles setup complete." \ No newline at end of file