-
Notifications
You must be signed in to change notification settings - Fork 2
/
syncspellbook.spell
executable file
·156 lines (138 loc) · 4.1 KB
/
syncspellbook.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# Tries to synchronise with changes to the
# remote repository. Also runs [learnSpells](./learnSpells.sh)
# and [castRunes](./castRunes.sh)
{ command -v jq && command -v python; } &>/dev/null ||
{ echo "please make sure jq, and python are installed" && exit 1; }
hostname() {
hn=$(command uname -n)
case "$hn" in
3QWP3T3)
echo "oboro"
;;
localhost)
echo "ornithopter"
;;
*)
echo "$hn"
;;
esac
}
symlinks() {
cd "${SPELLS:-$(dirname "$(readlink -f "$0")")/..}" || return 1
mkdir -p ~/.config/
mkdir -p ~/.local/{share,cache}
bash ./learnSpells.sh
bash ./castRunes.sh
bash ./stolenMagic.sh
}
sync() {
localRemote="$(git log --format=%H -1)" # latest known commit
mapfile -t files < <(git status --porcelain |
awk '{print $2}' |
xargs --no-run-if-empty -L1 basename)
if [[ "${#files[@]}" -gt 4 ]]; then
mapfile -t files < <(git status --porcelain |
awk '{print $2}' |
xargs -L1 dirname |
xargs -L1 basename |
sort |
uniq -c |
sed -E 's/^\s+//g')
fi
if [[ "${#files[@]}" -gt 0 ]]; then
file_str=$(printf "[%s]" "${files[@]}")
fi
echo -en "\033[32m"
git add --verbose --all
echo -en "\033[0m"
[[ "$1" == --nocommit ]] && return
if ! git diff-index --quiet HEAD --; then
hasCommits="true"
git commit -m"$(hostname) => $file_str"
elif [ -n "$(git rev-list '@{u}'..)" ]; then
hasCommits="true"
else
echo -e "\033[34mNothing to backup\033[0m"
fi
echo -en "\033[32m"
echo -n "Checking remote..."
echo -en "\033[34m"
git fetch &>/dev/null
fetch="$?"
echo -en "\033[0m"
echo -en "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
if ! [ "$fetch" = "0" ]; then
echo -e "\033[31mCan't access github\033[0m"
symlinks
return 2
fi
if [ "$localRemote" != "$(git rev-parse '@{u}')" ]; then
hasPulls="true"
else
echo -e "\033[34mNothing to pull \033[0m"
fi
if [ -n "$hasCommits" ] && [ -n "$hasPulls" ]; then
while true; do
git pull --rebase
git status | grep "rebase in progress" >/dev/null || break
echo -e "\033[31mConflicts emerged, please resolve them\033[0m"
read -r
rebase=1
for file in $(git status --short | grep UU | cut -d" " -f2); do
nvim "$file" || vim "$file"
done
git add -A
git rebase --continue
done
[[ $rebase != 0 ]] && git push --quiet
elif [ -n "$hasCommits" ]; then
git push --quiet
elif [ -n "$hasPulls" ]; then
git pull --rebase
fi
symlinks
return 0
}
farrow="=======> "
barrow=" <======="
echo -e "\033[33m${farrow} Back me up inside! ${barrow}\n\033[0m"
if [[ "$SPELLS" ]]; then
cd "$SPELLS" || exit 1
else
cd "$(dirname "$(dirname "$0")")" || exit 1
fi
bash ./readBooks.sh
find library/ -maxdepth 1 -mindepth 1 -type d |
while read -r l; do
pushd "$l" >/dev/null || continue
if [ "$(git status --porcelain)" ]; then
echo "library $l is dirty, please clean it"
else
branch=$(git symbolic-ref refs/remotes/origin/HEAD | cut -d/ -f4)
git checkout "$branch"
git pull origin "$branch" --rebase
fi
popd >/dev/null || continue
done
wait
echo -e "\e[36mBacking up spell-book\e[0m"
if sync "$1"; then
echo -e "\033[33m\n${farrow}Back me up and save me${barrow}\033[0m"
else
echo -e "\033[31m\n${farrow} Can't backup ${barrow}\033[0m"
fi
if command -V termux-fix-shebang &>/dev/null; then
find ~/.local/bin/ -type f | while read -r spell; do
real_path=$(readlink "$spell")
if [[ "$real_path" ]] && [[ "$real_path" != "$spell" ]]; then
rm "$spell"
cp "$real_path" "$spell"
termux-fix-shebang "$spell"
fi &
done
fi
bash ./readBooks.sh
if [ "$1" == '--hold' ]; then
read -n 1 -s -r
fi