{ config, pkgs, lib, ... }:

let
  # URL of your dotfiles repository
  dotfilesRepo = "http://192.168.12.81/git/wayne/dotfiles";

  # Path where the dotfiles repository will be cloned
  dotfilesPath = "${config.home.homeDirectory}/.dotfiles";

  # Path to the directory within the dotfiles repository that contains home directory dotfiles
  homeStowPath = "${dotfilesPath}/home";
in
{
  # Ensure that stow and git are installed
  home.packages = with pkgs; [
    stow
    git
  ];


  # Clone the dotfiles repository into the specified path
  home.activation.cloneDotfiles = lib.mkAfter ''
    echo "Running cloneDotfiles activation script"
    echo "Dotfiles path: ${dotfilesPath}"
    echo "Dotfiles repository: ${dotfilesRepo}"
    export PATH=${pkgs.git}/bin:$PATH
    if [ ! -d "${dotfilesPath}" ]; then
      echo "Cloning dotfiles repository from ${dotfilesRepo} to ${dotfilesPath}"
      git clone ${dotfilesRepo} ${dotfilesPath} || { echo "Failed to clone repository"; exit 1; }
    else
      echo "Dotfiles repository already exists at ${dotfilesPath}"
    fi
  '';

  # Activation script to manage the symlinks for the home directory dotfiles using GNU Stow
  home.activation.stowHome = lib.mkAfter ''
    echo "Running stow to manage dotfiles"
    export PATH=${pkgs.stow}/bin:$PATH
    cd ${dotfilesPath}/home
    # stow --adopt -t ${config.home.homeDirectory} bash bin config profile screen ssh tmux zsh  # Specify the packages to stow
    stow --adopt -t ${config.home.homeDirectory}  bin config screen ssh tmux zsh  # Specify the packages to stow
  '';
# if [ ! -d "${dotfilesPath}" ]; then
#   git clone ${dotfilesRepo} ${dotfilesPath}
#     else
#       echo "Updating dotfiles repository at ${dotfilesPath}"
#       cd ${dotfilesPath} && git pull
# fi
}
