-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.sh
executable file
·62 lines (52 loc) · 1.6 KB
/
link.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
#!/bin/bash
# get script directory, cd into it
dir="$(dirname $(realpath $0))"
pushd "$dir" > /dev/null
single_app=$1
if [[ "$(uname)" == "Darwin" ]]; then
HOMEBREW_PREFIX="/opt/homebrew"
else
HOMEBREW_PREFIX="/home/linuxbrew/.linuxbrew"
fi
export PATH="$HOMEBREW_PREFIX/bin:$PATH"
eval "$(brew shellenv)"
# Function to safely remove symlink if it exists
remove_symlink_if_exists() {
local symlink_path="$1"
if [ -L "$symlink_path" ]; then # Check if it's a symlink
echo "Removing existing symlink: $symlink_path"
mv "$symlink_path" "$symlink_path.backup"
fi
}
safely_stow() {
app=$1
# Before stowing, check and remove any existing symlinks that would cause conflicts
while read -r line; do
if [[ "$line" == *"existing target is not owned by stow:"* ]]; then
# Extract the path of the conflicting file
conflict_file="${line#*: }"
# Check if this path is relative to the home directory
if [[ "$conflict_file" == .* ]]; then
conflict_file="$HOME/$conflict_file"
fi
remove_symlink_if_exists "$conflict_file"
fi
done < <(stow "$app" --adopt --no --ignore=bootstrap.sh --ignore=post-stow.sh 2>&1)
# Now attempt to stow again after removing any problematic symlinks
stow "$app" --adopt --ignore=bootstrap.sh --ignore=post-stow.sh
if [[ -f "$app/post-stow.sh" ]]; then
bash "$app/post-stow.sh"
fi
echo -e "\033[32m✔︎\033[0m $(basename "$app")"
}
if [ -n "$single_app" ]; then
safely_stow "$single_app"
else
echo
echo -e "\033[94mLinking dotfiles\033[0m"
for app in `ls -d1 */`
do
safely_stow "$app"
done
fi
popd > /dev/null