diff --git a/dotfiles.sh b/dotfiles.sh index f9236be..3f09e28 100755 --- a/dotfiles.sh +++ b/dotfiles.sh @@ -4,6 +4,7 @@ cd ~/.dotfiles 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") @@ -12,6 +13,8 @@ ignore_files=("README.md" ".git" ".gitmodules") 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" @@ -21,9 +24,9 @@ create_symlinks() { # 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") + local rel_path=${file#$HOME/.dotfiles/} # Get relative path # Check if file should be ignored if [[ " ${ignore_files[@]} " =~ " $file_name " ]]; then @@ -33,33 +36,43 @@ create_symlinks() { # 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 + # 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" - # If this directory is also a submodule, initialize it - if [[ " ${sub_modules[@]} " =~ " $file_name " ]]; then - echo "Initializing submodule: $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 - # Recursively create symlinks for the contents - create_symlinks "$file" "$target_dir/$file_name" + + # Process the contents + create_symlinks "$file" "$target_dir/$file_name" "false" else - # For regular directories, create them and recurse - mkdir -p "$target_dir/$file_name" - create_symlinks "$file" "$target_dir/$file_name" + # 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 - # If the file is not a symlink already + # For regular files, always create symlinks 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" + echo "Linking file: $file -> $target_dir/$file_name" ln -s "$file" "$target_dir/$file_name" fi fi @@ -67,6 +80,6 @@ create_symlinks() { } # Call the function to create symlinks from dotfiles to home -create_symlinks "$HOME/.dotfiles" "$HOME" +create_symlinks "$HOME/.dotfiles" "$HOME" "true" echo "Dotfiles setup complete!" \ No newline at end of file