-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbooks_functions
171 lines (133 loc) · 3.15 KB
/
books_functions
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# shellcheck shell=bash disable=SC2154
# UTITLITIES
# find tool, returns the first|one|found, exit with error message if none found
find_tool () {
IFS='|' read -ra tools <<< "$*"
found=0
for tool in "${tools[@]}"; do
if [[ -n $(which "$tool") ]]; then
found=1
break
fi
done
if [[ "$found" -eq 0 ]]; then
if [[ ${#tools[@]} -gt 1 ]]; then
exit_with_error "missing programs: $*; install at least one of these: ${tools[*]} and try again"
else
exit_with_error "missing program: $1; please install and try again"
fi
fi
echo "$tool"
}
url_available () {
url="$1"
dl_tool=$(find_tool "curl|wget")
case "$dl_tool" in
curl)
${torsocks:-} curl --output /dev/null --silent --fail -r 0-0 "$url"
;;
wget)
${torsocks:-} wget -q --spider "$url"
;;
*)
exit_with_error "unknown download tool ${dl_tool}"
;;
esac
}
add_cron_job () {
job="$*"
(crontab -l ; echo "*/1 * * * * $job") 2>/dev/null | sort | uniq | crontab -
}
# leave <br> and <pre> to enable some simple formatting tasks
strip_html () {
#echo "$*"|sed -e 's/<br>/\n/g;s/<[^>]*>//g;s/\n/<br>/g'
echo "$*"
}
is_true () {
val="${1,,}"
if [[ "${val:0:1}" == "y" || "$val" -gt 0 ]]; then
true
else
false
fi
}
# dummmy cleanup function
cleanup () {
true
}
# echo error message to stderr and terminate main
exit_with_error () {
echo -e "$(basename "$0"): $*" >&2
kill -s TERM "$TOP_PID"
}
trap_error () {
cleanup
exit 1
}
trap_clean () {
cleanup
exit
}
_log () {
msg="$*"
logdir="${XDG_STATE_HOME:-$HOME/.state}/books"
logfile=$(basename "$0").log
mkdir -p "$logdir"
echo "$(date -Iseconds): $msg" >> "$logdir/$logfile"
}
log_err () {
_log "E: $*"
}
log_warn () {
_log "W: $*"
}
log_info () {
_log "I: $*"
}
log_debug () {
_log "D: $*"
}
# DATABASE
dbx () {
db="$1"
shift
mysql=$(find_tool "mysql")
if [ $# -gt 0 ]; then
"$mysql" -N -Bsss -h "$dbhost" -P "$dbport" -u "$dbuser" "$db" -e "$*"
else
"$mysql" -N -Bsss -h "$dbhost" -P "$dbport" -u "$dbuser" "$db"
fi
}
# LOCKING
exlock () {
cmd="$1"
lockfile="/var/lock/$(basename "$0")"
lockfd=99
flock=$(find_tool "flock")
case "$cmd" in
prepare)
eval "exec $lockfd<>\"$lockfile\""
trap 'exlock nolock' EXIT
;;
now)
$flock -xn $lockfd
;;
lock)
$flock -x $lockfd
;;
shlock)
$flock -s $lockfd
;;
unlock)
$flock -u $lockfd
;;
nolock)
$flock -u $lockfd
$flock -xn $lockfd && rm -f "$lockfile"
trap_clean
;;
*)
exit_with_error "unknown lock command: $cmd"
;;
esac
}