#!/bin/bash

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")

# 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
        
        # 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"
            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
        fi
    done
}

# Call the function to create symlinks from dotfiles to home
create_symlinks "$HOME/.dotfiles" "$HOME" "true"

echo "Dotfiles setup complete!"