22
33set -e
44
5- rawItemsFile=false
6- debugFiles=false
7- tagFiles=false
8- typeFiles=false
9- curlFiles=false
10- # The collection files will be created only if directly querying Zotero API.
11- collectionFiles=false
5+ function usage () {
6+ echo -e " \nUpdate Bibliography for interlisp.org from Zotero\n"
7+ echo -e " Usage: $0 [ options ] [ input_items_file ]\n"
8+ echo -e " Options:"
9+ echo -e " -h | --help \tDisplay this message and exit."
10+ echo -e " -r | --rawitems \tSave the complete downloaded JSON as '00-rawItems.json' (See 'input_items_file' below.)."
11+ echo -e " -g | --tagsfile \tGenerate 'tags.json' containing all tags on the 'cleaned-up' set of entries."
12+ echo -e " -y | --typefiles \tGenerate item type information JSON files. (See below.)"
13+ echo -e " -c | --collectionsfiles \tGenerate two JSON files containing info about each of the Zotero collections."
14+ echo -e " -u | --curlfiles \tGenerate files in the 'curl/' directory with the output of each call to curl. Very low level debugging."
15+ echo -e " -d | --debugfiles \tGenerate numbered files with the intermediate processing step output JASON."
16+ echo -e " -i N | --infolevel N \tSet the level of display of informational messages. N is 0-10 (Default = 2.). (See below.)"
17+ echo -e " \n typefiles: These 3 files contain the type and itemType information for the entries with different level of details."
18+ echo -e " \n infolevel: The infolevel controls how much detail is presented during processing."
19+ echo -e " 0: NO info messages."
20+ echo -e " 1: high level general info messages."
21+ echo -e " 2: warning-type messages."
22+ # echo -e " 3:"
23+ # echo -e " 4:"
24+ echo -e " 5: detailed general info messages."
25+ # echo -e " 6:"
26+ # echo -e " 7:"
27+ echo -e " 8: individual processing steps (most of which may generate a debug File. See --debugfiles)"
28+ echo -e " 9: very detailed general info messages."
29+ echo -e " 10: extremely detailed messages (e.g., calls to curl, etc.)"
30+ echo -e " Values include all messages designated at a lower level. The value isn't checked and is used as given.\n"
31+ echo -e " \ninput_items_file:"
32+ echo -e " Providing this is optional. This a JSON file as saved by previously using the -r option."
33+ echo -e " If provided, this bypasses getting any data from Zotero, and the -r, -c, and -u options are ignored.\n"
34+ }
35+
36+ rawItemsFile=false # --rawitems/-r
37+ debugFiles=false # --debugfiles/-d
38+ tagsFile=false # --tagsfile/-g
39+ typeFiles=false # --typefiles/-y
40+ curlFiles=false # --curlfiles/-u
41+ # The collections files will be created only if directly querying Zotero API.
42+ collectionsFiles=false # --collectionsfiles/-c
1243
1344# removeChildrenFromFinalFile=false
1445
15- showInfoLevel=2
46+ showInfoLevel=2 # --infolevel/-i
1647# 0: NO showInfo messages.
1748# 1: high level general info messages.
1849# 2: warning-type messages.
@@ -100,12 +131,90 @@ function add_items_from_collection () {
100131 fi
101132}
102133
134+
135+ # option --infolevel/-i requires 1 argument
136+ LONGOPTS=help,rawitems,debugfiles,tagsfile,typefiles,curlfiles,collectionsfiles,infolevel:
137+ OPTIONS=hrdgyuci:
138+
139+ # -temporarily store output to be able to check for errors
140+ # -activate quoting/enhanced mode (e.g. by writing out “--options”)
141+ # -pass arguments only via -- "$@" to separate them correctly
142+ # -if getopt fails, it complains itself to stderr
143+ PARSED=$( getopt --options=$OPTIONS --longoptions=$LONGOPTS --name " $0 " -- " $@ " ) || exit 2
144+ # read getopt’s output this way to handle the quoting right:
145+ eval set -- " $PARSED "
146+
147+ # now enjoy the options in order and nicely split until we see --
148+ while true ; do
149+ case " $1 " in
150+ -h|--help)
151+ usage
152+ exit
153+ ;;
154+ -r|--rawitems)
155+ rawItemsFile=true
156+ shift
157+ ;;
158+ -d|--debug)
159+ debugFiles=true
160+ shift
161+ ;;
162+ -g|--tagsfile)
163+ tagsFile=true
164+ shift
165+ ;;
166+ -y|--typefiles)
167+ typeFiles=true
168+ shift
169+ ;;
170+ -u|--curlfiles)
171+ curlFiles=true
172+ shift
173+ ;;
174+ -c|--collectionsfiles)
175+ collectionsFiles=true
176+ shift
177+ ;;
178+ -i|--infolevel)
179+ showInfoLevel=" $2 "
180+ shift 2
181+ ;;
182+ --)
183+ shift
184+ break
185+ ;;
186+ * )
187+ echo " Programming error" >&2
188+ exit 3
189+ ;;
190+ esac
191+ done
192+
193+
194+ # handle non-option arguments
195+ if [[ $# -gt 1 ]]; then
196+ echo " $0 : A single input file is required." >&2
197+ exit 4
198+ fi
199+
200+ # echo "rawItemsFile: $rawItemsFile"
201+ # echo "debugFiles: $debugFiles"
202+ # echo "tagsFile: $tagsFile"
203+ # echo "typeFiles: $typeFiles"
204+ # echo "curlFiles: $curlFiles"
205+ # echo "collectionsFiles: $collectionsFiles"
206+ # echo "showInfoLevel: $showInfoLevel"
207+ # echo 'Remaining arguments:'
208+ # for arg; do
209+ # echo "--> '$arg'"
210+ # done
211+
103212root_collection=" FSK5IX4F"
104213if [[ $# -eq 0 ]] ; then
105214 # Initialize with the root collection.
106215 collections=$( curl -s " https://api.zotero.org/groups/$GROUP_ID /collections/$root_collection " | jq -s -r ' map(.data+.meta)' )
107216 add_items_from_collection $root_collection
108- if $collectionFiles ; then
217+ if $collectionsFiles ; then
109218 echo " $collections " > collections.json
110219 fi
111220 showInfo 1 " $( jq ' . | length' <<< " $collections" ) collections"
@@ -118,7 +227,7 @@ if [[ $# -eq 0 ]] ; then
118227 showInfo 10 " items updated"
119228 done < <( jq -r ' include "./bib-fns";getNeededUrls|.[]' <<< " $items" )
120229
121- if $collectionFiles ; then
230+ if $collectionsFiles ; then
122231 grouped_collections=$( jq ' group_by(.parentCollection)' <<< " $collections" )
123232 # This is not yet fully hierarchically nested.
124233 echo " $grouped_collections " > grouped_collections.json
@@ -183,7 +292,7 @@ if $typeFiles ; then
183292 types=$( jq ' group_by(.type) | map({type:.[0].type, itemTypes:(group_by(.itemType)|map({itemType:.[0].itemType, items:.}))})' <<< " $items" )
184293 echo " $types " > fullTypeInfo.json
185294fi
186- if $tagFiles ; then
295+ if $tagsFile ; then
187296 tags=$( jq ' map(.tags) | flatten | map(.tag) | unique' <<< " $items" )
188297 echo " $tags " > tags.json
189298fi
0 commit comments