-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·160 lines (145 loc) · 4.39 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·160 lines (145 loc) · 4.39 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
set -euo pipefail
dryRun=false
while getopts "n" option; do
case ${option} in
n ) dryRun=true ;;
esac
done
if test -t 1; then # if terminal
ncolors=$(which tput > /dev/null && tput colors) # supports color
if test -n "$ncolors" && test $ncolors -ge 8; then
termcols=$(tput cols)
bold="$(tput bold)"
underline="$(tput smul)"
standout="$(tput smso)"
normal="$(tput sgr0)"
black="$(tput setaf 0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
magenta="$(tput setaf 5)"
cyan="$(tput setaf 6)"
white="$(tput setaf 7)"
fi
fi
bold="${bold:-}"
underline="${underline:-}"
standout="${standout:-}"
normal="${normal:-}"
black="${black:-}"
red="${red:-}"
green="${green:-}"
yellow="${yellow:-}"
blue="${blue:-}"
magenta="${magenta:-}"
cyan="${cyan:-}"
white="${white:-}"
print_bold() {
title="$1"
text="$2"
echo
echo "${blue}================================================================================${normal}"
echo "${blue}================================================================================${normal}"
echo
echo -e " ${bold}${magenta}${title}${normal}"
echo
echo -e " ${text}"
echo
echo "${blue}================================================================================${normal}"
echo "${blue}================================================================================${normal}"
echo
}
# Validate dependencies needed to setup dotfiles
validate_dependencies() {
dependencies=( "curl" "stow" )
for i in "${dependencies[@]}"
do
command -v $i >/dev/null 2>&1 || {
echo -e "\n${red}ERROR:${normal} Dependency missing: ${yellow}$i${normal}";
echo -e "Install dependency and rerun to continue\n"
exit 1;
}
done
}
# Backup old files to avoid overwriting
filesMovedOutput=""
backup_old_files() {
oldfiles=(
".bashrc"
".config/alacritty"
".config/bash"
".config/ripgreprc"
".config/vim"
".gitconfig"
".nanorc"
".tmux.conf"
".vimrc"
".zshrc"
"scripts"
)
for i in "${oldfiles[@]}"
do
if [[ -f "$HOME/$i" ]]; then
if !([[ -h "$HOME/$i" ]] && [[ $(readlink "$HOME/$i") == *dotfiles* ]]); then
if [[ $dryRun = false ]]; then
mv "$HOME/$i" "$HOME/$i-old"
fi
filesMovedOutput="${filesMovedOutput}\n\t$HOME/$i ${blue}=>${normal} $HOME/$i-old"
fi
fi
done
}
# Use stow to link dotfiles to $HOME directory
linkedFilesOutput=""
stow_dotfiles() {
IFS=$'\n'
dotfiles=($(find . -mindepth 1 -maxdepth 1 -type d -not -path '*/\.*' | sed 's|.*/||' | grep -v -E "PowerShell|pwsh"))
unset IFS
for dotfile in "${dotfiles[@]}"
do
local stowOutput
if [[ $dryRun = true ]]; then
stowOutput=$(stow -n -v --no-folding -t $HOME $dotfile 2>&1)
else
stowOutput=$(stow -v --no-folding -t $HOME $dotfile 2>&1)
fi
while IFS= read -r line; do
if [[ "$line" == *"LINK"* ]]; then
local target source
target=$(echo "$line" | cut -c 7- | awk -F ' => ' '{print $1}')
source=$(echo "$line" | cut -c 7- | awk -F ' => ' '{print $2}')
linkedFilesOutput="${linkedFilesOutput}\n\t${target} ${blue}=>${normal} ${source}"
fi
done <<< "$stowOutput"
done
}
if [[ $dryRun = true ]]; then
dryRunMessage="${yellow} (Dry Run)${normal}"
else
dryRunMessage=""
fi
print_bold \
" dotfiles setup " \
"This script will$dryRunMessage:
- Validate dependencies needed exist
- Backup any existing dotfiles
- Link dotfiles from this repo to \$HOME ($HOME)"
echo -n "Running step: Validate dependencies"
validate_dependencies
echo " (${green}Success${normal})"
echo -n "Running step: Backup existing files"
backup_old_files
echo " (${green}Success${normal})"
echo -n "Running step: Link dotfiles"
stow_dotfiles
echo " (${green}Success${normal})"
print_bold \
" dotfiles setup complete " \
"
Files backed up:
${filesMovedOutput}
Files linked:
${linkedFilesOutput}
"