-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinks
executable file
·50 lines (39 loc) · 816 Bytes
/
links
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
#!/usr/bin/env bash
now=$(date +"%Y.%m.%d.%H.%M.%S")
BACKUP_DIR="$HOME/.dotfiles_backup/$now"
usage() { echo "Usage: links [-f <FILENAME>]" 1>&2; exit 1; }
missing() { echo "File not found"; usage; }
link() {
DEST="${2/#\~/$HOME}" # fix ~ not expanding inside if statement
FILENAME="${1##*/}"
if [[ -e "$DEST" && ! -L "$DEST" ]]; then
mkdir -p "$BACKUP_DIR"
mv "$DEST" "$BACKUP_DIR/$FILENAME"
echo "$2 backed up"
fi
unlink "$DEST" > /dev/null 2>&1
printf "linking $FILENAME... "
ln -sfn "$1" "$DEST" > /dev/null 2>&1
printf "OK\n"
}
read_file() {
[ ! -f $1 ] && missing
while IFS=\= read -r LOC DEST; do
link "$(pwd)/$LOC" "$DEST"
done < $1
exit 0
}
while getopts ":f:h:" o; do
case "${o}" in
f)
read_file ${OPTARG}
;;
h)
usage
;;
*)
usage
;;
esac
done
usage