#!/bin/bash

# Install dependencies
sudo apt-get update

# Python 3 and related packages
sudo apt-get install -y python3 python3-pip python3-venv curl wget stow

# Terminal
sudo apt-get install -y zsh fzf jq knot-dnsutils tre-command
# Install fx
curl https://fx.wtf/install.sh | sudo sh

# Install Rust
curl https://sh.rustup.rs -sSf | sh
. "$HOME/.cargo/env"

cargo install --locked zellij
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
curl -s https://ohmyposh.dev/install.sh | bash -s
curl -sSfL https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh
curl --proto '=https' --tlsv1.2 -LsSf https://setup.atuin.sh | sh
cargo install eza

# Install Alacritty
sudo apt install cmake g++ pkg-config libfreetype6-dev libfontconfig1-dev libxcb-xfixes0-dev libxkbcommon-dev
cargo install alacritty
# Add to menu
sudo cp ./alacritty-term.svg /usr/share/pixmaps/Alacritty.svg
sudo desktop-file-install ./Alacritty.desktop
sudo update-desktop-database


# Install dotfiles
git clone git@woodburn.au:nathanwoodburn/dotfiles.git ~/.dotfiles
cd ~/.dotfiles
# All files are in the dotfiles directory or a subdirectory

# Create symbolic links for all dotfiles
echo "Creating symbolic links for dotfiles..."

# 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"
  
  # Handle .zsh_functions directory specially - link the whole directory
  if [ -d "$src_dir/.zsh_functions" ]; then
    local target_zsh_functions="$target_dir/.zsh_functions"
    
    # Remove existing directory or symlink if it exists
    if [ -e "$target_zsh_functions" ] || [ -L "$target_zsh_functions" ]; then
      echo "Removing existing: $target_zsh_functions"
      rm -rf "$target_zsh_functions"
    fi
    
    # Create symlink for the entire directory
    echo "Linking directory: $src_dir/.zsh_functions -> $target_zsh_functions"
    ln -s "$src_dir/.zsh_functions" "$target_zsh_functions"
  fi
  
  # Find all files and directories in the source directory, excluding .git, README.md, and .zsh_functions
  find "$src_dir" \( -not -path "*/\.git*" -and -not -name "README.md" -and -not -path "*/.zsh_functions/*" -and -not -path "*/.zsh_functions" \) -and \( -type f -o -type l \) | while read -r src_file; do
    # Get the relative path from src_dir
    rel_path="${src_file#$src_dir/}"
    
    # Construct the target file path
    target_file="$target_dir/$rel_path"
    
    # Create parent directories if they don't exist
    mkdir -p "$(dirname "$target_file")"
    
    # Remove existing file if it exists
    if [ -e "$target_file" ] || [ -L "$target_file" ]; then
      echo "Removing existing file: $target_file"
      rm -f "$target_file"
    fi
    
    # Create symlink
    echo "Linking: $src_file -> $target_file"
    ln -s "$src_file" "$target_file"
  done
}

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

echo "Dotfiles setup complete!"

# Install fonts