-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.sh
More file actions
executable file
·62 lines (53 loc) · 1.29 KB
/
lib.sh
File metadata and controls
executable file
·62 lines (53 loc) · 1.29 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
#!/bin/bash
# Shared helpers
DOTFILES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
info() { printf "${CYAN}[*]${NC} %s\n" "$1"; }
ok() { printf "${GREEN}[+]${NC} %s\n" "$1"; }
warn() { printf "${YELLOW}[!]${NC} %s\n" "$1"; }
err() { printf "${RED}[-]${NC} %s\n" "$1"; }
ask() {
local question="$1"
read -rp "$question (y/N) " reply
local yn
yn=$(echo "$reply" | tr "A-Z" "a-z")
[[ "$yn" == "y" || "$yn" == "yes" ]]
}
symlink_to() {
local src="$DOTFILES_DIR/$1"
local dst="$2"
if [[ ! -e "$src" ]]; then
err "Source does not exist: $src"
return 1
fi
mkdir -p "$(dirname "$dst")"
if [[ -L "$dst" ]]; then
local current
current=$(readlink "$dst")
if [[ "$current" == "$src" ]]; then
ok "Link exists: $dst -> $1"
return 0
else
warn "Symlink exists but points elsewhere: $dst -> $current"
if ask " Overwrite?"; then
rm -f "$dst"
else
return 0
fi
fi
elif [[ -e "$dst" ]]; then
warn "File exists: $dst"
if ask " Back up to ${dst}.bak and replace?"; then
mv "$dst" "${dst}.bak"
ok "Backed up: ${dst}.bak"
else
return 0
fi
fi
ln -s "$src" "$dst"
ok "Linked: $dst -> $1"
}