#!/bin/sh

encoded=$(echo "$2" | sed -e 's/ /%20/g' -e 's/\&/%26/g' -e 's/'\''/%27/g' \
  -e 's/(/%28/g' -e 's/)/%29/g' -e 's/,/%2c/g' -e 's/\[/%5b/g' -e 's/\]/%5d/g')

base='http://eutils.ncbi.nlm.nih.gov/entrez/eutils'
suffix="&rettype=xml&retmax=200"
if [ -n "$3" ]; then
suffix="&rettype=xml&retmax=200&reldate=$3"
fi

res=`curl -s "$base/esearch.fcgi?db=$1&term=$encoded$suffix"`

flt=`echo $res | xmllint --format - | \
  perl -nle 'print /(?<=<Id>).*?(?=<\/Id>)/g' | grep [0-9]`

for uid in $flt
do
echo "$uid"
done
