#!/bin/bash # List the packages that your custom environment requires. REQUIRED_PACKAGES=(neovim tmux) # Array to record packages that the script installs. installed_by_script=() # Function to ensure required packages are installed. ensure_packages() { local to_install=() for pkg in "${REQUIRED_PACKAGES[@]}"; do # Check if the package is installed. if ! dpkg -s "$pkg" >/dev/null 2>&1; then to_install+=("$pkg") fi done if [ ${#to_install[@]} -gt 0 ]; then echo "Installing missing packages: ${to_install[*]}" sudo apt update && sudo apt install -y "${to_install[@]}" # Record these packages for later removal. installed_by_script+=("${to_install[@]}") fi } # Function to remove only the packages that were installed by the script. cleanup_packages() { if [ ${#installed_by_script[@]} -gt 0 ]; then echo "Removing packages installed by the script: ${installed_by_script[*]}" sudo apt remove -y "${installed_by_script[@]}" sudo apt autoremove -y fi } # Create a temporary directory for your custom dotfiles. DOTFILES_DIR=$(mktemp -d) # Set up a trap that will run when the session ends (EXIT signal). # This trap cleans up the temporary dotfiles directory and removes the packages we installed. trap 'cleanup_packages; rm -rf "$DOTFILES_DIR"' EXIT # Clone your dotfiles repository into the temporary directory. git clone https://github.com/yourusername/dotfiles.git "$DOTFILES_DIR" # Ensure that any required packages are installed. ensure_packages # Optionally, override default commands to use your custom configurations. alias vim="vim -u $DOTFILES_DIR/.vimrc" alias tmux="tmux -f $DOTFILES_DIR/.tmux.conf" # Launch a new interactive bash session that uses your custom bashrc. exec bash --rcfile "$DOTFILES_DIR/.bashrc"