forked from davidosomething/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup
executable file
·125 lines (109 loc) · 4.02 KB
/
cleanup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/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() {
[[ ! -e "$1" ]] && __dko_ok "${1} not found" && return 0
if [[ -f "$1" ]] || [[ -L "$1" ]]; then
rm -i -- "$1" && __dko_ok "${1} removed"
elif [[ -d "$1" ]]; then
rm -ir -- "$1" && __dko_ok "${1} removed"
fi
}
# Logic for NVM
__clean_nvm() {
[[ -z "$NVM_DIR" ]] ||
[[ "${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}/.bash_history" "${HOME}/.local/bash_history"
__move "${HOME}/.zhistory" "${HOME}/.local/zsh_history"
__move "${HOME}/.gimp-2.8" "${XDG_CONFIG_HOME}/GIMP/2.8"
__move "${HOME}/.inputrc" "${XDG_CONFIG_HOME}/readline/inputrc"
__move "${HOME}/.vagrant.d" "${XDG_DATA_HOME}/vagrant"
__merge_dir "${HOME}/.fonts" "${XDG_DATA_HOME}/fonts"
[[ -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"
# Removes
! 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 ~/.ctags.d/universal.ctags
__remove "${HOME}/.ctags"
# alias points to dotfile
__remove "${HOME}/.mdlrc"
# should be in XDG as of 0.9.0
__remove "${HOME}/.proselint"
# should be in XDG
__remove "${HOME}/.pulse-cookie"
# should be symlinked in XDG
__remove "${HOME}/.gitconfig"
# should be symlinked in XDG
__remove "${HOME}/.gitignore"
# should be in .local; probably created on ZSH startup
__remove "${HOME}/.wget-hsts"
# should be .tern-config
__remove "${HOME}/.tern-project"
# should be ~/.config/tldr
__remove "${HOME}/.tldr"
# NVM
__clean_nvm