-
Notifications
You must be signed in to change notification settings - Fork 0
/
reftoget
executable file
·83 lines (71 loc) · 2.28 KB
/
reftoget
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
#!/bin/bash
# Usage: reftoget [-b|--bib BIB_FILE_NAME] [-d|--dir REFERENCE_DIRECTORY]
# reftoget records the references in BIB_FILE_NAME but not in REFERENCE_DIRECTORY. The result is
# written in a bib file named reftoget.bib.
# If BIB_FILE_NAME is not present, then all the bib files in the current directory will be checked.
# If REFERENCE_DIRECTORY is not present, then we check the default reference directory defined below.
# The directory where this scrip resides
TDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Some configuration, which should define DEFAULT_REFDIR
# shellcheck source=./tconfig
source "$TDIR/tconfig"
REFTOGET="reftoget.bib"
rm -f "$REFTOGET"
# Parse the arguments
if [[ $# -gt 4 ]]; then
printf "Usage: reftoget [-b|--bib BIB_FILE_NAME] [-d|--dir REFERENCE_DIRECTORY]\n"
exit 1
fi
while [[ -n "$1" ]]; do
case "$1" in
-b|--bib)
if [[ $# -gt 1 ]]; then
BIBFILE="$2"
shift
fi
;;
-d|--dir)
if [[ $# -gt 1 ]]; then
REFFILE="$2"
shift
fi
;;
*)
printf "Usage: reftoget [-b|--bib BIB_FILE_NAME] [-d|--dir REFERENCE_DIRECTORY]\n"
esac
shift
done
# Decide the bib files to check
if ! [[ -z "$BIBFILE" ]]; then
BIBFILES="$BIBFILE"
else
BIBFILES=$(ls -t *.bib 2>/dev/null)
fi
# Decide the reference directory
if [[ -z "$REFDIR" ]]; then
REFDIR="$DEFAULT_REFDIR"
fi
LINEBREAKER="LINEBREAKERSTARTING$(date +%s)LINEBREAKERENDING"
TIME=$(date +%Y.%m.%d-%H.%M.%S)
TMPDIR="$(mktemp -d)"
TMPBIB="$TMPDIR"/tmpbib-"$TIME"
GOTREF=$(ls "$REFDIR" | sed 's/\..*$//' | sed 's/-.*$//' | sort -u)
touch "$REFTOGET"
for FILE in $BIBFILES; do
cat "$FILE" | awk -v ORS="$LINEBREAKER" 1 | sed "s/$LINEBREAKER\s*@/\n@/g" \
| sed '/^\s*%/d' | sed '/^\s*@string/d' | sed '/^\s*@STRING/d' | sed '/^\s*@String/d' > "$TMPBIB"
while IFS= read -r LINE
do
BIBENTRYID=$(echo "$LINE" | sed 's/^\s*@[^{]*{\s*//' | sed 's/,.*$//')
if ! [[ -z "$BIBENTRYID" ]]; then
sed -i "/^\s*@.*{$BIBENTRYID.*$/d" "$TMPBIB"
fi
done < "$REFTOGET"
cat "$TMPBIB" >> "$REFTOGET"
for REF in $GOTREF; do
sed -i "/^\s*@.*{$REF.*$/d" "$REFTOGET"
done
done
sed -i "s/$LINEBREAKER/\n/g" "$REFTOGET"
rm -f "$TMPBIB"
exit 0