#!/usr/bin/env bash # # Cleanup home for XDG compliance # if [[ -z "$DOTFILES" ]]; then cd -- "$(dirname "$0")/.." || exit 1 DOTFILES="$PWD" fi . "${DOTFILES}/lib/helpers.sh" . "${DOTFILES}/lib/pretty.bash" # ============================================================================ # ENV validation # ============================================================================ if [[ -z "$BDOTDIR" ]] || [[ -z "$XDG_CONFIG_HOME" ]] || [[ -z "$XDG_DATA_HOME" ]]; then __dko_err "DOTFILES are not set up, run this after symlinking and restarting" __dko_err_ "shell" exit 1 fi # ============================================================================ # Cleanup functions # ============================================================================ # Move entire dir or file somewhere else (create dir if needed) # $1 full path to move (can be file or dir) # $2 full destination path __move() { [[ ! -e "$1" ]] && __dko_ok "${1} not found" && return 0 [[ -e "$2" ]] && __dko_err "${2} exists, cannot move ${1}" && return 1 __dko_status "Move ${1} to ${2}" dko-maybemkdir "$(dirname "${2}")" || return 1 mv -- "$1" "$2" && __dko_ok "Moved ${1} to ${2}" } # Move contents of one dir into another # $1 source directory, removed on completion # $2 destination directory, created if needed __merge_dir() { [[ ! -d "$1" ]] && __dko_ok "${1} not found" && return 0 __dko_status "Merge ${1} into ${2}" dko-maybemkdir "$2" || return 1 mv -- "${1}/*" "$2" && __dko_ok "${1}/* moved into ${2}" mv -- "${1}/.*" "$2" && __dko_ok "${1}/.* moved into ${2}" [[ -d "$1" ]] && rmdir -- "$1" && __dko_ok "${1} removed" } # Remove file or dir completely with confirmation # $1 path to completely remove __remove() { if [[ -f "$1" ]] || [[ -L "$1" ]]; then rm -i -- "$1" && __dko_ok "${1} removed" elif [[ -d "$1" ]]; then rm -I -r -- "$1" && __dko_ok "${1} removed" fi [[ ! -e "$1" ]] && __dko_ok "${1} not found" && return 0 } # Logic for NVM __clean_nvm() { [ -d "$FNM_DIR" ] && return 0 [ -z "$NVM_DIR" ] && return 0 [[ "${NVM_DIR}" != "${XDG_CONFIG_HOME}/nvm" ]] && { __dko_err "NVM_DIR not set properly. Should be ${XDG_CONFIG_HOME}/nvm" return 1 } dko-maybemkdir "${NVM_DIR}" || return 1 __move "${XDG_CONFIG_HOME}/.nvm" "$NVM_DIR" __move "${HOME}/.nvm" "$NVM_DIR" } # ============================================================================ # main # ============================================================================ __move "${HOME}/.ctags.d" "${XDG_CONFIG_HOME}/ctags" __move "${HOME}/.gimp-2.8" "${XDG_CONFIG_HOME}/GIMP/2.8" __move "${HOME}/.grip" "${XDG_CONFIG_HOME}/grip" __move "${HOME}/.fonts.conf" "${XDG_DATA_HOME}/fontconfig/fonts.conf" __move "${HOME}/.inputrc" "${XDG_CONFIG_HOME}/readline/inputrc" __move "${HOME}/.parallel" "${XDG_CONFIG_HOME}/parallel" __move "${HOME}/.pulse-cookie" "${XDG_CONFIG_HOME}/pulse/cookie" __move "${HOME}/.solargraph" "${XDG_CACHE_HOME}/solargraph" __move "${HOME}/.vagrant.d" "${XDG_DATA_HOME}/vagrant" __merge_dir "${HOME}/.fonts" "${XDG_DATA_HOME}/fonts" [[ -n "$ASDF_DATA_DIR" ]] && __move "${HOME}/.asdf" "${ASDF_DATA_DIR}" [[ -n "$CARGO_HOME" ]] && __move "${HOME}/.cargo" "$CARGO_HOME" [[ -n "$COMPOSER_HOME" ]] && __move "${HOME}/.composer" "$COMPOSER_HOME" [[ -n "$GNUPGHOME" ]] && [[ -d "${HOME}/.gnupg" ]] && { rmdir "${HOME}/.gnupg" 2>/dev/null || __move "${HOME}/.gnupg" "$GNUPGHOME" } [[ -n "$PYLINTHOME" ]] && __move "${HOME}/.pylint.d" "$PYLINTHOME" [[ -n "$YARN_CACHE_FOLDER" ]] && __move "${HOME}/.yarn-cache" "$YARN_CACHE_FOLDER" # ========================================================================== # Removals # ========================================================================== # delete shell history not in XDG_STATE_HOME case $HISTFILE in "${HOME}/.bash_history" | \ "${HOME}/.zhistory" | \ "${HOME}/.zsh_history" | \ "${HOME}/.local/bash_history" | \ "${HOME}/.local/zsh_history") dko_warn "Symlink and restart shell to set HISTFILE location properly." dko_warn "Then run cleanup again to remove old histfile paths." ;; esac __remove "${HOME}/.bash_history" __remove "${HOME}/.zhistory" __remove "${HOME}/.zsh_history" __remove "${HOME}/.local/bash_history" __remove "${HOME}/.local/zsh_history" ! dko-same "${HOME}/.dotfiles/bash/dot.bash_profile" "${HOME}/.bash_profile" && __remove "${HOME}/.bash_profile" ! dko-same "${HOME}/.dotfiles/bash/dot.bashrc" "${HOME}/.bashrc" \ __remove "${HOME}/.bashrc" ! dko-same "${HOME}/.dotfiles/zsh/dot.zshenv" "${HOME}/.zshenv" \ __remove "${HOME}/.zshenv" # support dropped in 2.2.0 __remove "${HOME}/.agignore" # should be ~/.config/ctags/*.ctags __remove "${HOME}/.ctags" # should be in XDG_CONFIG_HOME/fzf/fzf.bash and fzf.zsh or /usr/share/fzf __remove "${HOME}/.fzf.bash" __remove "${HOME}/.fzf.zsh" __remove "${HOME}/.ideavimrc" # alias points to dotfile __remove "${HOME}/.mdlrc" # should be in XDG as of 0.9.0 __remove "${HOME}/.proselint" # should be "${XDG_DATA_HOME}/forgit" __remove "${HOME}/.forgit" # should be symlinked in XDG __remove "${HOME}/.gitconfig" # should be symlinked in XDG __remove "${HOME}/.gitignore" # garbage, old xdg-utils made it, maybe has .desktop entries __remove "${HOME}/.gnome" # i will never use hyper terminal __remove "${HOME}/.hyper.js" __remove "${XDG_CONFIG_HOME}/hyper" # i will never use kitty terminal __remove "${XDG_CONFIG_HOME}/kitty" __remove "${XDG_CONFIG_HOME}/markdownlint.json" # mackup doesn't work any more __remove "${HOME}/.mackup.cfg" # should be in .local; probably created on ZSH startup __remove "${HOME}/.wget-hsts" # stopped using tern __remove "${HOME}/.tern-config" __remove "${HOME}/.tern-project" # should be ~/.config/tldr __remove "${HOME}/.tldr" # Useless __remove "${HOME}/.zlogin" __remove "${HOME}/.zprofile" # Created by /etc/zshrc_Apple_Terminal -- apple's way of sharing session data # for a shell. __remove "${HOME}/.zsh_sessions" # should be ~/.dotfiles/zsh/.zshrc __remove "${HOME}/.zshrc" # should be XDG_DATA_HOME/zinit __remove "${DOTFILES}/zsh/.zinit" __remove "${DOTFILES}/zsh/.zplugin" # obsolete __remove "${XDG_DATA_HOME}/nvim/mason" __remove "${XDG_DATA_HOME}/zplug" "${DOTFILES}/bin/dko-fix-git-completion" # NVM __clean_nvm