#!/bin/sh # # Search the GIN web server. For information use the -h option. # # S. Flower, Sep 2007 ################################################################################ # print a message to stderr ################################################################################ echo_stderr () { msg="$1" shift while [ $# -gt 0 ] ; do msg="$msg $1" shift done echo "$msg" 1>&2 } ################################################################################ # print a usage message and exit ################################################################################ usage () { echo_stderr "usage: $prog_name [-d] [-h] [-l largest-id] [-n max-results]" echo_stderr " [-p host[:port]] [-s smallest-id]" echo_stderr " [-u username:password] " echo_stderr " " echo_stderr "notes: use -d to turn on debugging" echo_stderr " use -h to print this information and exit" echo_stderr " use -l to specify the maximum file ID to return" echo_stderr " use -n to specify the maximum number of results to return" echo_stderr " use -p to specify a proxy web-server" echo_stderr " use -s to specify the minimum file ID to return" echo_stderr " use -u if a username/password is needed (default = none)" echo_stderr " datafile says where to put the search results" echo_stderr " the url is the absolute address of the cache" echo_stderr " application, without any query string information, e.g." echo_stderr " https://gin-upload.bgs.ac.uk/GINFileUpload/Cache" echo_stderr " " echo_stderr "exit: code=0, download succeeded" echo_stderr " code=1, download failed, don't retry" echo_stderr " code=2, download failed, may be retried later" exit 1 } ################################################################################ # start of main script body ################################################################################ # configuration CURL=curl prog_name=gin_search.sh # process command line debug=0 while getopts dhu:s:l:n:p: opt ; do case $opt in d) debug=1 ;; h) usage ;; p) phostport="$OPTARG" ;; u) userpass="$OPTARG" ;; s) smallest_id="$OPTARG" ;; l) largest_id="$OPTARG" ;; n) max_n_results="$OPTARG" ;; /?) usage ;; esac done i=`expr $OPTIND - 1` shift $i if [ $# -ne 2 ] ; then echo_stderr "$prog_name: error in command line" usage fi datafile="$1" address="$2" # check any proxy information if [ -n "$phostport" ] ; then phost=`echo "$phostport" | awk -F: '{print $1}'` pport=`echo "$phostport" | awk -F: '{if (NF < 2) print "" ; else print $2}'` fi # check any username/password information if [ -n "$userpass" ] ; then username=`echo "$userpass" | awk -F: '{if (NF != 2) print "" ; else print $1}'` if [ -z "$username" ] ; then echo_stderr "$prog_name: error in username/password (missing password)" exit 1 fi password=`echo "$userpass" | awk -F: '{print $2}'` fi # check the datafile is accessible touch $datafile >/dev/null 2>&1 if [ ! -w "$datafile" ] ; then echo_stderr "$prog_name: file $datafile cannot be created or overwritten" exit 1 fi # get a temporary file curl_err="/tmp/curl_$$_err" # tell the user what's going to happen if [ $debug -ne 0 ] ; then echo_stderr "$prog_name: using GIN server at $address" if [ -n "$username" ] ; then echo_stderr "$prog_name: username=$username, password=$password" fi fi # build the curl command curl_cmd="-FFormat=plain -FRequest=Search" if [ -n "$phost" ] ; then if [ -n "$pport" ] ; then curl_cmd="$curl_cmd -x $phost:$pport" else curl_cmd="$curl_cmd -x $phost" fi fi if [ -n "$username" ] ; then curl_cmd="$curl_cmd -u $username:$password" fi if [ -n "$smallest_id" ] ; then curl_cmd="$curl_cmd -FSmallestID=$smallest_id" fi if [ -n "$largest_id" ] ; then curl_cmd="$curl_cmd -FLargestID=$largest_id" fi if [ -n "$max_n_results" ] ; then curl_cmd="$curl_cmd -FMaxNResults=$max_n_results" fi curl_cmd="$curl_cmd ${address}" if [ $debug -ne 0 ] ; then echo_stderr "$prog_name: command = $CURL $curl_cmd" fi # run the curl command - remove Windows CR characters from the output (which # may have come from a web server) - this isn't needed for stderr $CURL $curl_cmd 2>$curl_err | tr -d '\r' >$datafile curl_status=$? if [ -r $curl_err ] ; then n_lines_err=`cat $curl_err | wc -l | awk '{print $1}'` else n_lines_err=0 fi # in debug mode show the output from CURL if [ $debug -eq 1 ] ; then echo "$prog_name: curl exit status = $curl_status" echo "------------------------------ stderr from CURL ------------------------------" cat $curl_err echo "--------------------------- end of stderr from CURL --------------------------" fi # work out what happened and tell the user exit_val=0 if [ $curl_status -ne 0 ] ; then exit_val=1 case $n_lines_err in 0) echo_stderr "$prog_name: error downloading file" ;; 1) msg=`head -1 $curl_err` echo_stderr "$prog_name: curl error: $msg" ;; *) msg=`tail -1 $curl_err` echo_stderr "$prog_name: curl error: $msg ..." ;; esac # some special CURL exit codes mean that the operation could be # retried later case $curl_status in 7|18|23|27|52|55|56) exit_val=2 ;; esac elif [ ! -r $datafile ] ; then echo_stderr "$prog_name: $datafile was not created" exit_val=1 else n_lines_out=`cat $datafile | wc -l | awk '{print $1}'` if [ $n_lines_out -le 0 ] ; then # this is where you end up when the search retrieves no entries exit_val=0 else orig_status=`head -1 $datafile` lwr_status=`echo "$orig_status" | tr '[:upper:]' '[:lower:]'` case $n_lines_out in 1) msg="No further information" ;; 2) msg=`head -2 $datafile | tail -1` ;; *) msg2=`head -2 $datafile | tail -1` msg3=`head -3 $datafile | tail -1` msg="$msg2 - $msg3" ;; esac case "$lwr_status" in information*|success*|warning*|error*|exception*|fatal*) echo_stderr "$prog_name: $orig_status - $msg" exit_val=1 ;; *) ;; esac fi fi # if you get here, the download was successful if [ $exit_val -ne 0 ] ; then rm -f $datafile fi rm -f $curl_err exit $exit_val