summaryrefslogtreecommitdiff
path: root/nquire
blob: f483b01d6b47cfce826bae1d534063a997796252 (plain)
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
#!/bin/sh

# ===========================================================================
#
#                            PUBLIC DOMAIN NOTICE
#            National Center for Biotechnology Information (NCBI)
#
#  This software/database is a "United States Government Work" under the
#  terms of the United States Copyright Act.  It was written as part of
#  the author's official duties as a United States Government employee and
#  thus cannot be copyrighted.  This software/database is freely available
#  to the public for use. The National Library of Medicine and the U.S.
#  Government do not place any restriction on its use or reproduction.
#  We would, however, appreciate having the NCBI and the author cited in
#  any work or product based on this material.
#
#  Although all reasonable efforts have been taken to ensure the accuracy
#  and reliability of the software and data, the NLM and the U.S.
#  Government do not and cannot warrant the performance or results that
#  may be obtained by using this software or data. The NLM and the U.S.
#  Government disclaim all warranties, express or implied, including
#  warranties of performance, merchantability or fitness for any particular
#  purpose.
#
# ===========================================================================
#
# File Name:  nquire
#
# Author:  Jonathan Kans, Aaron Ucko
#
# Version Creation Date:   03/28/2020
#
# ==========================================================================

# pth must contain cacert.pem certificate (previously within aux/lib/perl5/Mozilla/CA/ subdirectory)

pth=$( dirname "$0" )

case "$pth" in
  /* )
    ;; # already absolute
  *  )
    pth=$(cd "$pth" && pwd)
    ;;
esac

case ":$PATH:" in
  *:"$pth":* )
    ;;
  * )
    PATH="$PATH:$pth"
    export PATH
    ;;
esac

# set up colors for error report

ColorSetup() {

  if [ -z "$TERM" ] || [ ! -t 2 ]
  then
    RED=""
    BLUE=""
    BOLD=""
    FLIP=""
    INIT=""
  elif command -v tput >/dev/null
  then
    RED="$(tput setaf 1)"
    BLUE="$(tput setaf 4)"
    BOLD="$(tput bold)"
    FLIP="$(tput rev)"
    INIT="$(tput init)"
  else
    # assume ANSI
    escape="$(printf '\033')"
    RED="${escape}[31m"
    BLUE="${escape}[34m"
    BOLD="${escape}[1m"
    FLIP="${escape}[7m"
    INIT="${escape}[0m"
  fi
  LOUD="${INIT}${RED}${BOLD}"
  INVT="${LOUD}${FLIP}"
}

ColorSetup

# allow environment variable to set preference for curl or wget (undocumented)

helper=""

if [ -n "${NQUIRE_HELPER}" ]
then
  helper="${NQUIRE_HELPER}"
fi

# allow environment variable to set curl connection timeout (undocumented)

timeout=20

if [ -n "${NQUIRE_TIMEOUT}" ]
then
  timeout="${NQUIRE_TIMEOUT}"
fi

# allow environment variable to set IPv4 flag (undocumented)

ip_ver_flag=""

case "${NQUIRE_IPV4}" in
  [TtYy]* | 1 | [Oo][Nn] )
    # force IPv4
    ip_ver_flag=-4
    ;;
  "" | * )
    ;;
esac

# return contents of XML object or element

ParseXMLObject() {

  mesg=$1
  objc=$2

  echo "$mesg" | sed -n "s|.*<$objc[^>]*>\\(.*\\)</$objc>.*|\\1|p"
}

# check for leading flags

debug=false
log=false
timer=false
raw=false

while [ $# -gt 0 ]
do
  case "$1" in
    -newmode | -oldmode )
      shift
      ;;
    -version )
      version=$( einfo -version )
      echo "$version"
      exit 0
      ;;
    -h | -help | --help | help )
      version=$( einfo -version )
      echo "nquire $version"
      echo ""
      cat "$pth/help/nquire-help.txt"
      echo ""
      exit 0
      ;;
    -examples )
      version=$( einfo -version )
      echo "nquire $version"
      echo ""
      cat "$pth/help/nquire-examples.txt"
      echo ""
      exit 0
      ;;
    -puglist )
      shift
      res=$( cat | tr -d '\n' )
      obj=$( ParseXMLObject "$res" IdentifierList )
      numx=$( ParseXMLObject "$obj" Size )
      dbsx=$( ParseXMLObject "$obj" EntrezDB )
      webx=$( ParseXMLObject "$obj" EntrezWebEnv )
      keyx=$( ParseXMLObject "$obj" EntrezQueryKey )
      echo "<ENTREZ_DIRECT>"
      if [ -n "$dbsx" ]
      then
        echo "  <Db>${dbsx}</Db>"
      fi
      if [ -n "$webx" ]
      then
        echo "  <WebEnv>${webx}</WebEnv>"
      fi
      if [ -n "$keyx" ]
      then
        echo "  <QueryKey>${keyx}</QueryKey>"
      fi
      if [ -n "$numx" ]
      then
        echo "  <Count>${numx}</Count>"
      fi
      echo "</ENTREZ_DIRECT>"
      exit 0
      ;;
    -pugwait )
      shift
      res=$( cat | tr -d '\n' )
      obj=$( ParseXMLObject "$res" Waiting )
      lkey=$( ParseXMLObject "$obj" ListKey )
      goOn=true
      count=0

      while [ "$goOn" = true ]
      do
        count=$((count + 1))
        sleep 5
        poll=$( nquire -pugrest compound listkey "$lkey" cids XML )
        obj=$( ParseXMLObject "$poll" Waiting )
        wtng=$( ParseXMLObject "$obj" Message )
        if [ -z "$wtng" ]
        then
          goOn=false
        fi
        if [ "$count" -gt 40 ]
        then
          echo "ERROR Search $lkey timed out" >&2
          exit 1
        fi
      done
      cids=$(
        echo "$poll" |
        xtract -pattern IdentifierList -sep "\n" -element CID |
        sort -n | uniq
      )
      numx=$( echo "$cids" | wc -l | tr -d ' ' )
      echo "<ENTREZ_DIRECT>"
      echo "  <Db>pccompound</Db>"
      echo "  <Count>${numx}</Count>"
      echo "$cids" |
      while read uid
      do
        echo "  <Id>${uid}</Id>"
      done
      echo "</ENTREZ_DIRECT>"
      exit 0
      ;;
    -debug )
      debug=true
      shift
      ;;
    -log )
      log=true
      shift
      ;;
    -timer )
      timer=true
      shift
      ;;
    -raw )
      raw=true
      shift
      ;;
    -curl )
      # override setting from environment variable (undocumented)
      helper="curl"
      shift
      ;;
    -wget )
      # override setting from environment variable (undocumented)
      helper="wget"
      shift
      ;;
    * )
      # allows while loop to check for multiple flags
      break
      ;;
  esac
done

# elapsed time variable

elapsed=""

# check for presence of curl or wget

case "$helper" in
  curl | CURL | CUrl | Curl | cUrl | cURL )
    binary=$( command -v curl )
    if [ ! -x "$binary" ]
    then
      echo "${INVT} ERROR: ${LOUD} nquire is unable to find curl executable${INIT}" >&2
      exit 1
    fi
    ;;
  wget | WGET | WGet | Wget | wGet | wGET )
    binary=$( command -v wget )
    if [ ! -x "$binary" ]
    then
      echo "${INVT} ERROR: ${LOUD} nquire is unable to find wget executable${INIT}" >&2
      exit 1
    fi
    ;;
  * )
    binary=$( command -v curl )
    if [ ! -x "$binary" ]
    then
      binary=$( command -v wget )
    fi
    if [ ! -x "$binary" ]
    then
      echo "${INVT} ERROR: ${LOUD} nquire is unable to find either curl or wget${INIT}" >&2
      exit 1
    fi
    ;;
esac

# subset of perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"'

Escape() {

  echo "$1" |
  sed -e "s/%/%25/g" \
      -e "s/!/%21/g" \
      -e "s/#/%23/g" \
      -e "s/&/%26/g" \
      -e "s/'/%27/g" \
      -e "s/*/%2A/g" \
      -e "s/+/%2B/g" \
      -e "s/,/%2C/g" \
      -e "s|/|%2F|g" \
      -e "s/:/%3A/g" \
      -e "s/;/%3B/g" \
      -e "s/=/%3D/g" \
      -e "s/?/%3F/g" \
      -e "s/@/%40/g" \
      -e "s/|/%7C/g" \
      -e "s/ /%20/g" |
  sed -e 's/\$/%24/g' \
      -e 's/(/%28/g' \
      -e 's/)/%29/g' \
      -e 's/</%3C/g' \
      -e 's/>/%3E/g' \
      -e 's/\[/%5B/g' \
      -e 's/\]/%5D/g' \
      -e 's/\^/%5E/g' \
      -e 's/{/%7B/g' \
      -e 's/}/%7D/g'
}

# initialize variables

mode=""

url=""
sls=""

arg=""
amp=""
cmd=""
pfx=""

isPugRest=false

# optionally include nextra.sh script, if present, for internal NCBI maintenance functions (undocumented)

if [ -f "$pth"/nextra.sh ]
then
  # dot command is equivalent of "source"
  . "$pth"/nextra.sh
fi

# get extraction method or shortcut

if [ $# -gt 0 ]
then
  case "$1" in
    -url | -get | -lst | -dir | -ftp | -dwn | -asp )
      mode="$1"
      shift
      ;;
    -ncbi )
      # shortcut for NCBI base
      shift
      url="https://www.ncbi.nlm.nih.gov"
      sls="/"
      mode="-url"
      ;;
    -eutils )
      # shortcut for EUtils base
      shift
      url="https://eutils.ncbi.nlm.nih.gov/entrez/eutils"
      sls="/"
      mode="-url"
      ;;
    -pugrest | -pubchem )
      # shortcut for PubChem Power User Gateway (PUG) REST base (keeps -pubchem legacy shortcut)
      shift
      url="https://pubchem.ncbi.nlm.nih.gov/rest/pug"
      sls="/"
      mode="-url"
      isPugRest=true
      ;;
    -pugview )
      # shortcut for PubChem Power User Gateway (PUG) View base
      shift
      url="https://pubchem.ncbi.nlm.nih.gov/rest/pug_view"
      sls="/"
      mode="-url"
      ;;
    -pubrest )
      exec >&2
      echo "${INVT} ERROR: ${LOUD} '$1' is a misspelling - use '-pugrest' with a 'g'${INIT}" >&2
      exit 1
      ;;
    -pubview )
      exec >&2
      echo "${INVT} ERROR: ${LOUD} '$1' is a misspelling - use '-pugview' with a 'g'${INIT}" >&2
      exit 1
      ;;
    -* )
      exec >&2
      echo "${INVT} ERROR: ${LOUD} '$1' is not a recognized nquire option${INIT}" >&2
      exit 1
      ;;
    * )
      echo "${INVT} ERROR: ${LOUD} '$1' is not a recognized nquire command${INIT}" >&2
      exit 1
      ;;
  esac
fi

# collect URL directory components

while [ $# -gt 0 ]
do
  case "$1" in
    -* )
      # otherwise leading dash indicates end of path, switch to arguments
      break
      ;;
    * )
      dir="$1"
      # remove trailing slash directory delimiter
      dir=${dir%/}
      shift
      url="$url$sls$dir"
      sls="/"
      ;;
  esac
done

# collect argument tags paired with (escaped) values

isInChI=false

while [ $# -gt 0 ]
do
  case "$1" in
    -* )
      isInChI=false
      cmd="$1"
      if [ "$cmd" = "-inchi" ]
      then
        isInChI=true
      fi
      # remove leading dash from argument
      cmd=${cmd#-}
      # add argument and command
      arg="$arg$amp$cmd"
      # subsequent commands preceded by ampersand
      amp="&"
      # precede first value (if any) with equal sign
      pfx="="
      shift
      ;;
    * )
      val="$1"
      # remove initial backslash used to protect leading minus sign
      val=${val#\\}
      # add "InChI=" prefix if missing in -inchi argument to -pugrest
      if [ "$isPugRest" = true ] && [ "$isInChI" = true ]
      then
        case "$val" in
          "InChI="* )
            ;;
          * )
            val="InChI=${val}"
            ;;
        esac
      fi
      # URL encoding
      if [ "$raw" = true ]
      then
        val=$( echo "$val" | sed -e "s/&/%26/g" -e "s/=/%3D/g" )
      else
        val=$( Escape "$val" )
      fi
      arg="$arg$pfx$val"
      # concatenate run of values with commas
      pfx=","
      shift
      ;;
  esac
done

# reality checks

if [ -z "$mode" ]
then
  echo "${INVT} ERROR: ${LOUD} nquire mode (-url, -get, -lst, -dir, -ftp, -dwn, -asp) is missing${INIT}" >&2
  exit 1
elif [ -z "$url" ]
then
  echo "${INVT} ERROR: ${LOUD} nquire is missing the URL${INIT}" >&2
  exit 1
fi

# debugging output to stderr

if [ "$debug" = true ]
then
  if [ -z "$arg" ]
  then
    echo "PTH $pth" >&2
    echo "URL $url" >&2
  elif [ "$mode" = "-url" ]
  then
    echo "curl -fsSL \"$url\" -d \"$arg\"" >&2
  elif [ "$mode" = "-get" ]
  then
    echo "curl -fsSL \"$url?$arg\"" >&2
  else
    echo "PTH $pth" >&2
    echo "URL $url" >&2
    echo "ARG $arg" >&2
  fi
  exit 0
fi

# pause if Entrez Utilities server to avoid exceeding request frequency limit

slow=false

case "${SLOW_EDIRECT}" in
  "" | [FfNn]* | 0 | [Oo][Ff][Ff] )
    ;;
  * )
    slow=true
    ;;
esac

hasperl=$( command -v perl )

case $url in
  *"dev.ncbi.nlm.nih.gov/entrez/eutils/"* | *"internal.ncbi.nlm.nih.gov/entrez/eutils/"* )
    if [ "$slow" = true ]
    then
      sleep 1
    elif [ -x "$hasperl" ]
    then
      perl -MTime::HiRes -e 'Time::HiRes::usleep(1000)'
    fi
    ;;
  *"eutilspreview.ncbi.nlm.nih.gov/entrez/eutils/"* )
    if [ "$slow" = true ]
    then
      sleep 1
    elif [ -x "$hasperl" ]
    then
      perl -MTime::HiRes -e 'Time::HiRes::usleep(1000)'
    fi
    ;;
  *"/entrez/eutils/"* )
    if [ "$slow" = true ]
    then
      sleep 1
    elif [ -x "$hasperl" ]
    then
      case $arg in
        *"api_key="* )
          perl -MTime::HiRes -e 'Time::HiRes::usleep(110000)'
          ;;
        * )
          perl -MTime::HiRes -e 'Time::HiRes::usleep(350000)'
          ;;
      esac
    else
      sleep 1
    fi
    ;;
  *"pubchem.ncbi.nlm.nih.gov/rest/"* )
    if [ -x "$hasperl" ]
    then
      perl -MTime::HiRes -e 'Time::HiRes::usleep(210000)'
    else
      sleep 1
    fi
    ;;
  *"ftp.ncbi.nlm.nih.gov"* | *"nlmpubs.nlm.nih.gov"* )
    sleep 1
    ;;
esac

# accommodate CYGWIN using Windows version of curl that fails to recognize Cygwin paths

fix_path="printf %s"
case "$(uname -s)" in
  CYGWIN_NT* )
    if curl --version | grep -q Windows
    then
      fix_path="cygpath -w"
    fi
    ;;
esac

# try to get time in milliseconds

GetTime() {

  if [ -x "$hasperl" ]
  then
	perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000)'
  else
    echo $(($(date +'%s * 1000 + %-N / 1000000')))
  fi
}

# common function to execute curl or wget command

SendRequest() {

  when=$( date )

  starttime=""

  if [ "$timer" = true ]
  then
    starttime=$( GetTime )
  fi

  case "$binary" in
    */curl )
      if [ "$log" = true ]
      then
        echo "${BLUE}$@${INIT}" >&2
      fi

      temp=$(mktemp /tmp/NQUIRE_HEADER.XXXXXXXXX)

      if [ -f "$pth"/cacert.pem ]
      then
        curl --http1.0 --connect-timeout "$timeout" -fsSL $ip_ver_flag \
             --cacert "$($fix_path "$pth"/cacert.pem)" \
             -D "$($fix_path "$temp")" "$@"
      else
        curl --http1.0 --connect-timeout "$timeout" -fsSL $ip_ver_flag \
             -D "$($fix_path "$temp")" "$@"
      fi

      # capture and check curl return value
      res=$?
      if [ "$res" -ne 0 ]
      then
        # report failure
        echo "${INVT} ERROR: ${LOUD} curl command failed ( $when ) with: ${res}${INIT}" >&2
        echo "${BLUE}$@${INIT}" >&2
        # show return code in first line of header
        head -n 1 "$temp" >&2
      fi

      rm "$temp"
      ;;
    */wget )
      if [ "$log" = true ]
      then
        echo "${BLUE}$@${INIT}" >&2
      fi

      temp=$(mktemp /tmp/NQUIRE_HEADER.XXXXXXXXX)

      # wget needs --no-remove-listing for ftp listing?

      full_output=""
      if [ -f "$pth"/cacert.pem ]
      then
        wget -qS -O - --ca-certificate="$pth"/cacert.pem "$@" 2> "$temp"
      else
        wget -qS -O - --no-check-certificate "$@" 2> "$temp"
      fi

      # capture and check wget return value
      res=$?
      if [ "$res" -ne 0 ]
      then
        # report failure
        echo "${INVT} ERROR: ${LOUD} wget command failed ( $when ) with: ${res}${INIT}" >&2
        echo "${BLUE}$@${INIT}" >&2
        # show return code in first line of header
        head -n 1 "$temp" >&2
      fi

      rm "$temp"
      ;;
  esac

  if [ "$timer" = true ]
  then
    stoptime=$(GetTime)
    elapsed=$((stoptime - starttime))
  fi
}

# can use Aspera if installed

APPPATH=""
KEYPATH=""
KEYNAME=asperaweb_id_dsa.openssh

HasAspera() {

  case "$( uname -s )" in
    Darwin )
      sysdir='/Applications/Aspera Connect.app/Contents/Resources'
      sysdir2=/bin
      userdir=$HOME$sysdir
      ;;
    CYGWIN_NT* )
      sysdir='/cygdrive/c/Program Files/Aspera/Aspera Connect/bin'
      sysdir2='/cygdrive/c/Program Files (x86)/Aspera/Aspera Connect/bin'
      userdir="$( cygpath -H )/$USER/AppData/Local/Programs/Aspera/Aspera Connect/bin"
      ;;
    * )
      sysdir=/opt/aspera/bin
      sysdir2=/bin
      userdir=$HOME/.aspera/connect/bin
      ;;
  esac
  for d in "$sysdir" "$sysdir2" "$userdir"
  do
    if "$d/ascp" --version 2>&1 | grep '^Aspera' >/dev/null
    then
      APPPATH=$d
      break
    fi
  done
  if [ -z "$APPPATH" ]  &&  ascp --version 2>&1 | grep '^Aspera' >/dev/null
  then
    APPPATH=$( type -path ascp )
    APPPATH=$( dirname "$APPPATH" )
  fi
  if [ -z "$APPPATH" ]
  then
    return 1
  fi

  for d in "$APPPATH" "$sysdir" "$sysdir2" "$userdir"
  do
    if [ -f "$d/../etc/$KEYNAME" ]
    then
      KEYPATH=$d/../etc
      break
    elif [ -f "$d/$KEYNAME" ]
    then
      KEYPATH=$d
      break
    fi
  done
  if [ -z "$KEYPATH" ]
  then
    return 1
  fi

  return 0
}

if [ "$mode" = "-asp" ]
then
  HasAspera
  if [ "$?" = 1 ]
  then
    # Aspera not found, revert to download with FTP
    mode="-dwn"
  else
    # add colon before first slash in URL for Aspera
    url=$(echo "$url" | sed -e 's|/|:/|')
  fi
fi

# common method for file download

failed=0

DownloadOneFile() {

  urlfl="$1"
  fl="$2"

  if [ ! -f "$fl" ]
  then
    # only download if local file does not already exist
    case "$mode" in
      -dwn )
        SendRequest "$urlfl" > "$fl"
        ;;
      -asp )
        "$APPPATH/ascp" -T -q -k 1 -l 500m -i "$KEYPATH/$KEYNAME" \
        "anonftp@$urlfl" "."
        ;;
    esac
    if [ ! -f "$fl" ]
    then
      failed=$((failed + 1))
      # report failure to download requested file
      echo "" >&2
      echo "${RED}${fl} FAILED${INIT}" >&2
    fi
  fi
}

# send request with method-specific arguments

case "$mode" in
  -url )
    case "$binary" in
      */curl )
        if [ -n "$arg" ]
        then
          SendRequest "$url" -d "$arg"
        else
          SendRequest "$url"
        fi
        ;;
      */wget )
        if [ -n "$arg" ]
        then
          SendRequest --post-data="$arg" "$url"
        else
          SendRequest --post-data="" "$url"
        fi
        ;;
    esac
    ;;
  -get )
    if [ -n "$arg" ]
    then
      SendRequest "$url?$arg"
    else
      SendRequest "$url"
    fi
    ;;
  -lst )
    case "$binary" in
      */curl )
        SendRequest "$url/" |
        tr -s ' ' | tr ' ' '\t' | cut -f 9 | grep '.'
        ;;
      */wget )
        echo "${INVT} ERROR: ${LOUD} -lst not supported for wget${INIT}" >&2
        exit 1
        ;;
    esac
    ;;
  -dir )
    case "$binary" in
      */curl )
        SendRequest "$url/" |
        tr -s ' ' | tr ' ' '\t' | cut -f 5,9 | grep '.'
        ;;
      */wget )
        echo "${INVT} ERROR: ${LOUD} -dir not supported for wget${INIT}" >&2
        exit 1
    esac
    ;;
  -ftp )
    if [ -t 0 ]
    then
      SendRequest "$url"
    else
      # read file names from stdin, URL contains base address of server
      while IFS=$'\t' read fl
      do
        SendRequest "$url/$fl"
      done
    fi
    ;;
  -dwn | -asp )
    if [ -t 0 ]
    then
      # file name is included in URL constructed from command line arguments
      fl=$( basename "$url" )
      DownloadOneFile "$url" "$fl"
    else
      # read file names from stdin, URL contains base address of server
      while IFS=$'\t' read fl
      do
        DownloadOneFile "$url/$fl" "$fl"
      done
    fi
    sfx=""
    if [ "$failed" -gt 0 ]
    then
      if [ "$failed" -gt 1 ]
      then
        sfx="S"
      fi
      echo "" >&2
      echo "${LOUD}FAILED TO DOWNLOAD $failed FILE${sfx}${INIT}" >&2
      exit 1
    fi
    ;;
  * )
    echo "${INVT} ERROR: ${LOUD} '$1' is not a recognized nquire option${INIT}" >&2
    exit 1
    ;;
esac

if [ "$timer" = true ]
then
  echo "NQUIRE MILLISECONDS ELAPSED $elapsed" >&2
fi