-
Notifications
You must be signed in to change notification settings - Fork 2
/
learnSpells.sh
executable file
·88 lines (75 loc) · 1.89 KB
/
learnSpells.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
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
#!/bin/bash
set -e
allows="$(dirname -- "$0")/.install-profile/allows.sh learnSpells"
link_target() {
realpath "$PWD/$1"
}
spell_name() {
local v
case "$1" in
spells/*.spell)
v="${1#spells/}"
v="${v%.spell}"
;;
spells/*.crs)
v="${1#spells/}"
v="${v%.crs}"
;;
cantrips/*.sh)
v="${1%.sh}"
;;
*)
echo "Error: Invalid spell name: $1" >/dev/stderr
exit 1
;;
esac
echo "$v"
}
link_path() {
local spell_name
spell_name="$(spell_name "$1")"
realpath ~/.local/bin/"$spell_name" 2>/dev/null
}
should_learn_spell() {
{
local link_path
! link_path=$(link_path "$1") ||
[[ "$link_path" != $(link_target "$1") ]]
} && {
grep -v -i termux "$1" >/dev/null || command -V termux-fix-shebang &>/dev/null
} && {
local spell_name
spell_name="$(spell_name "$1")"
$allows "$spell_name"
}
}
cleanSpells() {
for spell in ~/.local/bin/* ~/.local/bin/cantrips/*; do
if [ -h "$spell" ] && ! [ -e "$spell" ]; then
echo -e "\033[31mRemoving dead spell: $(basename "$spell")\033[0m"
rm "$spell"
fi
done
return 0
}
newSpells() {
for spell in spells/*.spell spells/*.crs cantrips/*.sh; do
if should_learn_spell "$spell" ; then
echo "$spell"
fi
done
}
cd "$(dirname "$(realpath "$0")")" || return 0
cleanSpells
mapfile -t newSpells < <(newSpells)
if [[ "${#newSpells[@]}" -eq 0 ]]; then
exit 0
fi
echo -e "\033[33mLearning Spells...\033[0m"
mkdir -p ~/.local/bin/cantrips
for spell in "${newSpells[@]}"; do
echo -e "\033[35m\t$(spell_name "$spell")\033[0m"
ln -s "$(link_target "$spell")" "$(link_path "$spell")"
done
chmod +x spells/*.spell spells/*.crs
echo -e "\033[33mDone!\033[0m"