-
Notifications
You must be signed in to change notification settings - Fork 0
/
locateref
executable file
·57 lines (47 loc) · 1.39 KB
/
locateref
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
#!/bin/bash
# The directory where this scrip resides
TDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Some configuration, which should define REFDIR
# shellcheck source=./tconfig
source "$TDIR/tconfig"
KEYWORDS="$*"
TIME=$(date +%Y.%m.%d-%H.%M.%S)
GOTREF="/tmp/gotref-$TIME"
READER=evince
KEYWORDS="$(echo "$KEYWORDS" | sed 's/{//g' | sed 's/}//g' | sed 's/(//g' | sed 's/)//g' | sed 's/\[//g' \
| sed 's/\]//g' | sed "s/'//" | sed 's/"//g' | sed 's/`//g' | sed 's/^//g' | sed 's/~//g' \
| sed 's/\\\\L/L/g' | sed 's/\\\\l/l/g' | sed 's/\\//g' | sed 's/^\s*//' | sed 's/\s*$//' \
| sed 's/[^0-9a-zA-Z]/\n/g'\
| tr '[:upper:]' '[:lower:]'\
)"
if [[ -z "$KEYWORDS" ]]; then
printf "No keyword received.\n"
exit 1
fi
find "$REFDIR" -name "*" > "$GOTREF"
NUMBER_OF_MATCH=0
while IFS= read -r LINE
do
MATCH=1
LINE_LOWER_CASE=$(echo "$LINE" | tr '[:upper:]' '[:lower:]')
for WORD in $KEYWORDS; do
if ! echo "$LINE_LOWER_CASE" | grep -qF "$WORD"; then
MATCH=0
break
fi
done
if [[ $MATCH -eq 1 ]]; then
NUMBER_OF_MATCH=$((NUMBER_OF_MATCH+1))
MATCHED_LINE="$LINE"
printf "%s\n" "$LINE"
fi
done < "$GOTREF"
if [[ $NUMBER_OF_MATCH -eq 0 ]]; then
printf "No reference found.\n"
exit 1
fi
if [[ $NUMBER_OF_MATCH -eq 1 ]]; then
"$READER" "$MATCHED_LINE"
fi
rm "$GOTREF"
exit 0