#!/bin/bash

# ================================
# Dotfiles installation script
# ================================

DOTFILES_DIR="$HOME/git/dotfiles"

echo "Setting up dotfiles..."

# Function to create symlink
create_symlink() {
    local source="$1"
    local target="$2"
    
    if [ -L "$target" ]; then
        echo "Removing existing symlink: $target"
        rm "$target"
    elif [ -f "$target" ]; then
        echo "Backing up existing file: $target -> $target.backup"
        mv "$target" "$target.backup"
    fi
    
    echo "Creating symlink: $source -> $target"
    ln -s "$source" "$target"
}

# Zsh configuration
if [ -f "$DOTFILES_DIR/.zshrc" ]; then
    create_symlink "$DOTFILES_DIR/.zshrc" "$HOME/.zshrc"
fi

# Tmux configuration
if [ -f "$DOTFILES_DIR/.tmux.conf" ]; then
    create_symlink "$DOTFILES_DIR/.tmux.conf" "$HOME/.tmux.conf"
fi

# Neovim configuration
if [ -d "$DOTFILES_DIR/nvim" ]; then
    mkdir -p "$HOME/.config"
    if [ -L "$HOME/.config/nvim" ]; then
        echo "Removing existing symlink: $HOME/.config/nvim"
        rm "$HOME/.config/nvim"
    elif [ -d "$HOME/.config/nvim" ]; then
        echo "Backing up existing directory: $HOME/.config/nvim -> $HOME/.config/nvim.backup"
        mv "$HOME/.config/nvim" "$HOME/.config/nvim.backup"
    fi
    create_symlink "$DOTFILES_DIR/nvim" "$HOME/.config/nvim"
fi

echo "Dotfiles setup complete!"
echo ""
echo "To apply changes:"
echo "  - For zsh: source ~/.zshrc or restart your terminal"
echo "  - For tmux: tmux source-file ~/.tmux.conf (if running) or restart tmux"
echo "  - For nvim: restart nvim"