-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·113 lines (92 loc) · 2.5 KB
/
setup.sh
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
#!/usr/bin/env bash
# make sure we have pulled in and updated any submodules
git submodule init
git submodule update
# what directories should be installable by all users including the root user
base=(
bash
)
# folders that should, or only need to be installed for a local user
useronly=(
git
bin
wezterm
scripts
)
# .config folder items
configonly=(
nvim
)
# Explicitly remove known conflicting files
remove_known_conflicts() {
local usr=$1
# Define a list of known conflicting files for each app
declare -A known_conflicts
known_conflicts[bash]=".bash_profile .bashrc"
known_conflicts[git]=".gitconfig"
for app in "${!known_conflicts[@]}"; do
for file in ${known_conflicts[$app]}; do
if [[ -f "${usr}/${file}" || -L "${usr}/${file}" ]]; then
echo "Removing known conflicting file: ${usr}/${file}"
rm -f "${usr}/${file}"
fi
done
done
}
# resolve stow conflicts by removing or backing up conflicting files
resolve_conflicts() {
usr=$1
app=$2
target=${usr}/$3 # target path, if it's for config, it'll be passed as an additional argument
echo "Resolving conflicts for ${app} in ${target}"
# Perform a dry run to find conflicts
conflicts=$(stow -n -v -t "${target}" "${app}" 2>&1 | grep "existing target is neither a link nor a directory" | awk '{print $NF}')
# If there are conflicts, remove the conflicting files
for conflict in $conflicts; do
echo "Removing conflicting file: ${target}/${conflict}"
rm -rf "${target}/${conflict}"
done
}
# run the stow command for the passed in directory ($2) in location $1
stowit() {
usr=$1
app=$2
# -v verbose
# -R recursive
# -t target
stow -v -R -t ${usr} ${app}
}
# run the stow command for configuration files in .config
stowConfig() {
usr=$1
app=$2
target="${usr}/.config/${app}"
mkdir -p "${target}/${app}"
echo "Stowing ${app} into ${target} for user ${usr}"
resolve_conflicts "${usr}" "${app}" ".config/${app}"
stow -v -R -t "${target}" "${app}"
}
echo ""
echo "Stowing apps for user: ${whoami}"
# Remove known conflicting files before starting
remove_known_conflicts "${HOME}"
# install apps available to local users and root
for app in ${base[@]}; do
stowit "${HOME}" $app
done
# install only user space folders
for app in ${useronly[@]}; do
if [[ ! "$(whoami)" = *"root"* ]]; then
stowit "${HOME}" $app
fi
done
# install .config configurations
echo ""
echo "Stowing .config specific files"
for app in ${configonly[@]}; do
if [[ ! "$(whoami)" = *"root"* ]]; then
stowConfig "${HOME}" $app
fi
done
echo ""
echo "##### ALL DONE"