-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquran
executable file
·461 lines (391 loc) · 12.4 KB
/
quran
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/env bash
########################################################################################################
# .. . #
# x .d88" @88> #
# x. . .u . u. u. 5888R %8P #
# .u@u .@88k z88u .d88B :@8c u x@88k u@88c. . '888R . #
# .zWF8888bx ~"8888 ^8888 ="8888f8888r us888u. ^"8888""8888" .udR88N 888R .@88u #
# .888 9888 8888 888R 4888>'88" .@88 "8888" 8888 888R <888'888k 888R ''888E`#
# I888 9888 8888 888R 4888> ' 9888 9888 8888 888R 9888 'Y" 888R 888E #
# I888 9888 8888 888R 4888> 9888 9888 8888 888R 9888 888R 888E #
# I888 9888 8888 ,888B . .d888L .+ 9888 9888 8888 888R 88888888 9888 888R 888E #
# `888Nx?888 "8888Y 8888" ^"8888*" 9888 9888 "*88*" 8888" 88888888 ?8888u../ .888B . 888& #
# "88" '888 `Y" 'YP "Y" "888*""888" "" 'Y" "8888P' ^*888% R888"#
# 88E ^Y" ^Y' "P' "% "" #
# 98> #
# '8 #
# ` #
# #
# Get the English translation of the verbatim word of Allah, the only God worthy of worship, #
# Right in your terminal. #
########################################################################################################
# Github: https://github.com/SaminYaser-work/quran-cli
#
#
# Licensed under GNU GENERAL PUBLIC LICENSE Version 3 (GPLv3)
#
# Copyright (C) 2022 Samin Yaser
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
###########################################
# Variables
###########################################
data_path="$HOME/.local/share/quran-cli/data"
file="$data_path/saheeh_english_fn.csv"
metadata="$data_path/metadata.csv"
bookmark_file="$data_path/bookmark.csv"
# Styling
if [ -x /usr/bin/tput ] && tput setaf 1 &>/dev/null; then
BOLD=$(tput bold)
RESET=$(tput sgr0)
UNDERLINE=$(tput smul)
NOUNDERLINE=$(tput rmul)
RED=$(tput setaf 1)
NC=$(tput sgr0)
else
BOLD="\033[1m"
UNDERLINE="\033[4m"
NOUNDERLINE="\033[0m"
RESET="\033[0m"
RED='\033[0;31m' # Red
NC='\033[0m'
fi
# Sed replacements
remove_verse_number='sed "s/([0-9]\+) //g"'
remove_footnote_number='sed "s/\[[0-9]\+\]//g"'
verse_number_in_bold='sed "s/\(([0-9]\+)\)/${BOLD}\1${RESET}/g"'
footnote_number_in_italics='sed "s/\([[0-9]\+]\)/${UNDERLINE}\1${NOUNDERLINE}/g"'
insert_new_line='sed "s/;;/\n\n/g"'
replace_blank_notes='sed "s/::blank::/No notes available/g"'
footnote_del_blank_line='sed "/::blank::/{N;d;}"'
# Arguments
surah=$1
ayah=$2
ayahLast=$3
# flags
all=0
no_footnote=0
range_given=0
output_verse=0
if [[ -n $ayahLast ]]; then
range_given=1
fi
###########################################
# Error handling
###########################################
# Check if the surah is non-empty
if [ -z "$surah" ]; then
echo -e "${RED}Surah number is required.${NC}"
print_help
exit 1
fi
# Check if surah is valid
if [[ $surah -gt 115 ]]; then
echo -e "${RED}Error:${NC} There are only 114 surahs in the Noble Quran."
exit 1
fi
###########################################
# Functions
###########################################
# Error checking functions
# --------------------------
check_ayah_exists_in_a_surah() {
num_of_ayahs=$(awk -F, -v surah="$1" '$1 == surah {print $2}' $metadata)
if [[ $2 -gt $num_of_ayahs || $2 -lt 1 ]]; then
echo -e "${RED}Error:${NC} Surah $(get_surah_name_transliterated "$1") only has $num_of_ayahs ayahs."
exit 1
fi
}
check_surah_exists() {
if [[ $1 -gt 114 || $1 -lt 1 ]]; then
echo -e "${RED}Error:${NC} There are only 114 surahs in the Noble Quran."
exit 1
fi
}
check_verse_exists() {
if [[ $1 -gt 6236 || $1 -lt 1 ]]; then
echo -e "${RED}Error:${NC} There are only 6236 verses in the Noble Quran."
exit 1
fi
}
check_bookmark_file() {
if [[ ! -f $bookmark_file ]]; then
echo -e "${RED}Bookmark file doesn't exist!${NC}"
exit 1
fi
total_bookmarks=$(($(wc -l <$bookmark_file) - 1))
if [[ $total_bookmarks -lt 1 ]]; then
echo "No bookmarks found."
exit 1
fi
}
check_valid_bookmark() {
if [[ $1 -gt $(($(wc -l <$bookmark_file) - 1)) || $1 -lt 1 ]]; then
echo -e "${RED}Error:${NC} Invalid bookmark number."
exit 1
fi
}
# Get functions
# ----------------
display_center() {
columns="$(tput cols)"
while IFS= read -r line; do
printf "%*s\n" $(((${#line} + columns) / 2)) "$line"
done <"$1"
}
# Metadata : _index,_ayas,_start,_name,_tname,_ename,_type,_order,_rukus
# Column No: 1 2 3 4 5 6 7 8 9
# Get the title (in English, transliteration & Arabic) of the surah
get_surah_title() {
local res
res=$(awk -F, -v surah="$1" '$1 == surah {print $4 " | " $5 " ("$6")"}' "$metadata")
# echo -e "${BOLD}$res${RESET}"
display_center <(echo -e "${UNDERLINE}${BOLD}$res${RESET}${NOUNDERLINE}")
}
# Get the transliterated name of the surah
get_surah_name_transliterated() {
local res
res=$(awk -F, -v surah="$1" '$1 == surah {print $5}' "$metadata")
echo -e "${BOLD}$res${RESET}"
}
get_surah_title_by_verse() {
local res
res=$(awk -F, -v verse="$1" '$3+1 > verse {print f;exit;} {f=$4 " | " $5 " ("$6")"}' "$metadata") # Prints the line before the match is found
display_center <(echo -e "${UNDERLINE}${BOLD}$res${RESET}${NOUNDERLINE}")
}
save_bookmark() {
if [[ ! -f $bookmark_file ]]; then
echo "No bookmark file found. Creating a new one in $bookmark_file..."
if touch $bookmark_file; then
echo "Created new bookmark file at $bookmark_file"
echo "Surah|Ayah|Date" >$bookmark_file
else
echo "Failed to create bookmark file at $bookmark_file."
exit 1
fi
fi
echo "$1|$2|$(date)" >>$bookmark_file &&
echo "Bookmark saved." && exit 0
echo "Failed to write to bookmark file at $bookmark_file."
exit 1
}
show_bookmarks() {
check_bookmark_file
column -t -s '|' $bookmark_file
}
select_bookmark() {
check_bookmark_file
check_valid_bookmark "$1"
s=$(awk -F '|' -v b="$1" 'NR==(b+1) {print $1}' $bookmark_file)
a=$(awk -F '|' -v b="$1" 'NR==(b+1) {print $2}' $bookmark_file)
get_surah_title "$s"
echo
get_ayah_of_a_surah "$s" "$a"
print_footnotes "$s" "$a"
}
delete_bookmark() {
check_bookmark_file
check_valid_bookmark "$1"
x=$(($1 + 1))
sed -i "${x}d" $bookmark_file
echo "Bookmark deleted."
}
# Prints full information about the surah
get_surah_info() {
local res
res=$(awk -F, -v surah="$1" '$1 == surah {print "Index: " $1 "\nNumber of Ayahs: " $2 "\nStarting Verse Number: " $3+1 "\nName: " $4 "\nName (Transliterated): " $5 "\nName (Translation): " $6 "\nType: " $7 "\nOrder: " $8 "\nNumber of Rukūʿ: " $9}' "$metadata")
echo "$res"
}
get_all_ayahs() {
local res
res=$(awk -F "|" '{print $4""}' $file)
echo "$res"
}
get_all_notes() {
local res
res=$(awk -F '|' '{print $5"\n"}' "$file" | eval "$insert_new_line" | eval "$replace_blank_notes")
echo "$res"
}
# TODO: Format output nicely
get_full_quran() {
local res
res=$(awk -F '|' '{print $4"|"$5}' "$file" | sed "s/;;/ /g" | eval "$replace_blank_notes")
echo "$res"
}
get_full_surah() {
local res
res=$(awk -F "|" -v s="$1" '$2 == s {print $4"\n"}' $file)
echo "$res"
}
get_full_notes() {
local res
res=$(awk -F '|' -v s="$1" '$2 == s {print $5"\n"}' "$file" | eval "$insert_new_line" | eval "$footnote_del_blank_line")
echo "$res"
}
get_ayahs_in_range() {
local res
res=$(awk -F '|' -v s="$1" -v a1="$2" -v a2="$3" '$2 == s && $3 >= a1 && $3 <= a2 {print $4"\n"}' "$file")
echo "$res"
}
get_footnotes_in_range() {
local res
res=$(awk -F '|' -v s="$1" -v a1="$2" -v a2="$3" '$2 == s && $3 >= a1 && $3 <= a2 {print $5"\n"}' "$file" | eval "$insert_new_line" | eval "$footnote_del_blank_line")
echo "$res"
}
# Get a single ayah without verse number and references
get_ayah_of_a_surah_simple() {
local res
res=$(awk -F "|" -v s="$1" -v a="$2" '$2 == s && $3 == a {print $4}' $file | eval "$remove_verse_number" | eval "$remove_footnote_number")
echo "$res"
}
get_ayah_of_a_surah() {
local res
res=$(awk -F "|" -v s="$1" -v a="$2" '$2 == s && $3 == a {print $4 "\n"}' $file | eval "$verse_number_in_bold" | eval "$footnote_number_in_italics")
echo -e "$res"
}
get_verse() {
local res
res=$(awk -F "|" -v a="$1" '$1 == a {print $4 "\n"}' $file | eval "$verse_number_in_bold" | eval "$footnote_number_in_italics")
echo "$res"
}
get_verse_note() {
local res
res=$(awk -F "|" -v a="$1" '$1 == a {print $5 "\n"}' $file | eval "$insert_new_line" | eval "$footnote_del_blank_line")
echo "$res"
}
get_footnotes_of_ayah_in_a_surah() {
local res
res=$(awk -F '|' -v s="$1" -v a="$2" '$2 == s && $3 == a {print $5 "\n"}' $file | eval "$insert_new_line" | eval "$footnote_del_blank_line")
echo "$res"
}
get_footnote_header() {
echo
echo -e "${UNDERLINE}Footnotes:${NOUNDERLINE}"
echo
}
print_footnotes() {
if [[ "$#" -eq 1 ]]; then
footnotes=$(get_verse_note "$1")
else
footnotes=$(get_footnotes_of_ayah_in_a_surah "$1" "$2")
fi
if [[ -n $footnotes ]]; then
get_footnote_header
echo "$footnotes"
fi
exit 0
}
print_footnotes_in_range() {
footnotes=$(get_footnotes_in_range "$1" "$2" "$3")
if [[ -n $footnotes ]]; then
get_footnote_header
echo "$footnotes"
fi
exit 0
}
# TODO: Print a help message
print_help() {
printf "Usage: Under construction\nFor now, please refer to the README.md file on GitHub.\nhttps://github.com/SaminYaser-work/quran-cli"
}
###########################################
# Parsing arguments
###########################################
while getopts 'ahslv:i:B:b:d:r:' opt; do
case "$opt" in
h)
print_help
exit 0
;;
a) # TODO: Needs more work
all=1
echo "This feature is not yet implemented."
exit 1
;;
s)
check_surah_exists "$2"
check_ayah_exists_in_a_surah "$2" "$3"
get_ayah_of_a_surah_simple "$2" "$3"
exit 0
;;
v)
check_verse_exists "$OPTARG"
get_surah_title_by_verse "$OPTARG"
echo
get_verse "$OPTARG"
print_footnotes "$OPTARG"
exit 0
;;
i)
check_surah_exists "$OPTARG"
get_surah_info "$OPTARG"
exit 0
;;
r)
check_surah_exists "$2"
check_ayah_exists_in_a_surah "$2" "$3"
check_ayah_exists_in_a_surah "$2" "$4"
get_surah_title "$2"
echo
get_ayahs_in_range "$2" "$3" "$4"
print_footnotes_in_range "$2" "$3" "$4"
exit 0
;;
B)
save_bookmark "$OPTARG" "$3"
exit 0
;;
b)
select_bookmark "$OPTARG"
exit 0
;;
d)
delete_bookmark "$OPTARG"
exit 0
;;
l)
show_bookmarks
exit 0
;;
:)
echo -e "option requires an argument."
print_help
exit 1
;;
?)
print_help
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
###########################################
# Printing results
###########################################
# Printing full Qur'an
# if [[ $all -eq 1 ]]; then
# fi
# The default output when no options are passed
if [[ -z $2 ]]; then
# Prints a full surah
check_surah_exists "$1"
get_surah_title "$1"
echo
get_full_surah "$1"
get_footnote_header
get_full_notes "$1"
else
# Print a specific ayah of a surah
check_ayah_exists_in_a_surah "$1" "$2"
get_surah_title "$1"
echo
get_ayah_of_a_surah "$1" "$2"
print_footnotes "$1" "$2"
fi