forked from sbugert/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tow
executable file
·52 lines (45 loc) · 1.32 KB
/
tow
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
#!/usr/bin/env bash
##################################
# #
# tow – like GNU stow but less #
# #
##################################
set -e
if [[ $# -lt 2 ]] || ([[ $1 != "link" ]] && [[ $1 != "unlink" ]]); then
echo 'Usage: tow link|unlink path [path ...]'
exit 0
fi
# link $1 and backup existing file if it does not point to $2
function tow_link() {
if [ ! "$1" -ef "$2" ]; then # is dest not a symlink to f?
if [ -e "$1" ]; then
mv "$1" "$1"_old # if dest exists, make backup
fi
ln -sf "$2" "$1" # link file
fi
}
# unlink $1 if it points to $2
function tow_unlink() {
echo unlink "$1"
if [ "$1" -ef "$2" ]; then # is dest still a symlink to f?
rm "$1" # unlink f
fi
}
for d in "${@:2}"; do
if [[ -d $d ]]; then # if d is directory
for f in ${d%/}/*; do
if [[ $1 == "link" ]]; then # if unlink
tow_link "$HOME/.$(basename "$f")" "$PWD/$f"
elif [[ $1 == "unlink" ]]; then
tow_unlink "$HOME/.$(basename "$f")" "$PWD/$f"
fi
done
else # if d is a file
if [[ $1 == "link" ]]; then # if unlink
tow_link "$HOME/.$(basename "$d")" "$PWD/$d"
elif [[ $1 == "unlink" ]]; then
tow_unlink "$HOME/.$(basename "$d")" "$PWD/$d"
fi
fi
done
# TODO: cleanup all dead symlinks from home to $1