-
Notifications
You must be signed in to change notification settings - Fork 2
/
add_rune.spell
executable file
·100 lines (86 loc) · 1.95 KB
/
add_rune.spell
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
#!/bin/bash
# Add a new [rune](#runes-static-configuration-files) to the dotfiles, supports
# some customization options
set -e
runes="$SPELLS/runes/.db"
if [ $# -lt 1 ]
then
echo Usage: "$0" dotfile
exit 1
fi
# Opts
# -a Alternate name
# -o Options
while [ "$#" -gt 0 ]
do
case "$1" in
-a|--alternate)
alt="$2"
shift
shift
;;
-o|--options)
opt="$2"
shift
shift
;;
-h|--help)
cat <<EOF
Usage add_rune rune [-a alternate] [-o options]
-a | --alternate
Choose the name to save under in the runes directory
-o | --options
Add options. Currently only 'check' is suported. To prevent
creation of the symlink if the dir doesn't exist
EOF
exit 42
;;
*)
dotfile="$1"
shift
;;
esac
done
if ! [ -e "$dotfile" ]
then
echo No such file or directory: "$dotfile"
exit 1
fi
dotfile_exp="$(realpath "$dotfile")"
dotfile="${dotfile_exp/#$HOME/\~}"
[ -n "$opt" ] && opt=",$opt"
[ -n "$alt" ] || alt="$(basename "$dotfile" | sed 's/^\.//')"
echo "$dotfile,$alt${opt:-""}"
echo -n "Confirm [Y/n] "
read -r response
if [ "$response" = "n" ] || [ "$response" = "N" ]
then
exit 0
fi
echo "$dotfile,$alt${opt:-""}" | tee -a "$runes"
awk '!seen_lines[$0]++' "$runes" > /tmp/.add_rune_t
mv /tmp/.add_rune_t "$runes"
move_dotfile() {
if [ "$opt" = ",sudo" ]
then
cp "${args[@]}"
echo "sudo for '$1'"
sudo rm "${args[0]}" "${args[1]}"
else
mv "${args[@]}"
fi
}
mkdir -vp "$SPELLS/runes/$(dirname "$alt")"
if [ -d "$dotfile_exp" ]
then
mkdir -vp "$SPELLS/runes/$alt" || exit 1
for f in "$dotfile_exp"/*
do
args=(-v "$f" "$SPELLS/runes/$alt/$(basename "$f")")
move_dotfile "$f"
done
else
args=(-v "$dotfile_exp" "$SPELLS/runes/$alt")
move_dotfile "$dotfile_exp"
fi
"$SPELLS"/castRunes.sh