Debian Codegraph @

xfetch

raw
1 #!/bin/bash
2
3 # ===========================================================================
4 #
5 # PUBLIC DOMAIN NOTICE
6 # National Center for Biotechnology Information (NCBI)
7 #
8 # This software/database is a "United States Government Work" under the
9 # terms of the United States Copyright Act. It was written as part of
10 # the author's official duties as a United States Government employee and
11 # thus cannot be copyrighted. This software/database is freely available
12 # to the public for use. The National Library of Medicine and the U.S.
13 # Government do not place any restriction on its use or reproduction.
14 # We would, however, appreciate having the NCBI and the author cited in
15 # any work or product based on this material.
16 #
17 # Although all reasonable efforts have been taken to ensure the accuracy
18 # and reliability of the software and data, the NLM and the U.S.
19 # Government do not and cannot warrant the performance or results that
20 # may be obtained by using this software or data. The NLM and the U.S.
21 # Government disclaim all warranties, express or implied, including
22 # warranties of performance, merchantability or fitness for any particular
23 # purpose.
24 #
25 # ===========================================================================
26 #
27 # File Name: xfetch
28 #
29 # Author: Jonathan Kans, Aaron Ucko
30 #
31 # Version Creation Date: 01/11/2025
32 #
33 # ==========================================================================
34
35 pth=$( dirname "$0" )
36
37 case "$pth" in
38 /* )
39 ;; # already absolute
40 * )
41 pth=$(cd "$pth" && pwd)
42 ;;
43 esac
44
45 case ":$PATH:" in
46 *:"$pth":* )
47 ;;
48 * )
49 PATH="$PATH:$pth"
50 export PATH
51 ;;
52 esac
53
54 # handle common flags - dot command is equivalent of "source"
55
56 if [ ! -f "$pth"/xcommon.sh ]
57 then
58 echo "ERROR: Unable to find '$pth/xcommon.sh' file" >&2
59 exit 1
60 fi
61
62 . "$pth"/xcommon.sh
63
64 # initialize specific flags
65
66 dbase=""
67 flag="none"
68 turbo=false
69 stream=false
70
71 recname=""
72 settag=""
73 xmltag=""
74 doctype=""
75 recskip=0
76
77 while [ $# -gt 0 ]
78 do
79 case "$1" in
80 -version )
81 version=$( einfo -version )
82 echo "$version"
83 exit 0
84 ;;
85 -h | -help | --help | help )
86 version=$( einfo -version )
87 echo "xfetch $version"
88 echo ""
89 echo "USAGE: xfetch"
90 echo " -db"
91 echo " -id"
92 echo " -input"
93 echo " -stream"
94 echo ""
95 exit 0
96 ;;
97 * )
98 break
99 ;;
100 esac
101 done
102
103 while [ $# -gt 0 ]
104 do
105 tag="$1"
106 rem="$#"
107 case "$tag" in
108 -strict )
109 flag="strict"
110 shift
111 ;;
112 -mixed )
113 flag="mixed"
114 shift
115 ;;
116 -flag )
117 flag=$2
118 shift
119 shift
120 ;;
121 -db )
122 CheckForArgumentValue "$tag" "$rem"
123 shift
124 dbase="$1"
125 shift
126 ;;
127 -id )
128 CheckForArgumentValue "$tag" "$rem"
129 shift
130 ids="$1"
131 shift
132 while [ $# -gt 0 ]
133 do
134 case "$1" in
135 -* )
136 break
137 ;;
138 * )
139 # concatenate run of UIDs with commas
140 ids="$ids,$1"
141 shift
142 ;;
143 esac
144 done
145 ;;
146 -input )
147 CheckForArgumentValue "$tag" "$rem"
148 shift
149 input="$1"
150 shift
151 ;;
152 -tag )
153 recname=$2
154 shift
155 shift
156 ;;
157 -skip )
158 recskip=$2
159 shift
160 shift
161 ;;
162 -xml )
163 xmltag=$2
164 shift
165 shift
166 ;;
167 -doctype )
168 doctype=$2
169 shift
170 shift
171 ;;
172 -set )
173 settag=$2
174 shift
175 shift
176 ;;
177 -turbo )
178 turbo=true
179 shift
180 ;;
181 -stream )
182 stream=true
183 shift
184 ;;
185 -* )
186 exec >&2
187 echo "$0: Unrecognized option $1"
188 exit 1
189 ;;
190 * )
191 break
192 ;;
193 esac
194 done
195
196 # check for ENTREZ_DIRECT message or piped UIDs unless database and UIDs provided in command line
197
198 if [ -z "$dbase" ]
199 then
200 ParseStdin
201 elif [ -z "$ids" ] && [ -z "$input" ]
202 then
203 ParseStdin
204 fi
205
206 # set pubmed as default database for the time being
207 if [ -z "$dbase" ]
208 then
209 dbase="pubmed"
210 fi
211
212 # eventually will require explicit database in argument or piped message
213 if [ -z "$dbase" ]
214 then
215 echo "Must supply database in -db argument"
216 exit 1
217 fi
218
219 # normalize date arguments
220
221 FixDateConstraints
222
223 # get path to local archive folder
224
225 FindArchiveFolder
226
227 # get database-specific parameters from xfetch.ini configuration file
228
229 if [ ! -f "$pth"/xfetch.ini ]
230 then
231 echo "ERROR: Unable to find '$pth/xfetch.ini' file" >&2
232 exit 1
233 fi
234
235 mssg=$(
236 cat "${pth}/xfetch.ini" |
237 ini2xml |
238 xtract -rec Rec -pattern "ConfigFile/*" -select "$dbase" |
239 tr '\n' ' '
240 )
241 if [ -n "$mssg" ]
242 then
243 ParseConfig "$mssg" Rec recname name settag set xmltag xml doctype doctype recskip skip
244 fi
245
246 if [ -z "$recname" ] || [ -z "$settag" ]
247 then
248 echo "ERROR: Missing -tag or -set from database-specific wrapper in xfetch.ini file" >&2
249 exit 1
250 fi
251
252 # call rchive -stream
253
254
255 if [ "$stream" = true ]
256 then
257 GetUIDs |
258 rchive -db "$dbase" -tag "$recname" -skip "$recskip" -flag "$flag" -stream "$archiveBase"
259
260 exit 0
261 fi
262
263 # call rchive -fetch
264
265 if [ -n "$xmltag" ]
266 then
267 echo "$xmltag"
268 fi
269 if [ -n "$doctype" ]
270 then
271 echo "$doctype"
272 fi
273 if [ -n "$settag" ]
274 then
275 echo "<${settag}>"
276 fi
277 if [ "$turbo" = true ]
278 then
279 GetUIDs |
280 rchive -gzip -db "$dbase" -tag "$recname" -flag "$flag" -turbo -fetch "$archiveBase"
281 else
282 GetUIDs |
283 rchive -gzip -db "$dbase" -tag "$recname" -flag "$flag" -fetch "$archiveBase"
284 fi
285 if [ -n "$settag" ]
286 then
287 echo "</${settag}>"
288 fi
289
290 exit 0