#!/bin/csh
# file: ads-name-bib
# init: Feb 18 2021  Rob Rutten  Deil = new recipe from Alberto Accomazzi
# last: Feb 19 2021  Rob Rutten  Deil
# note: 1 parameter = ADS search name (eg: rutten-rob)
#       !! remove Bearer key twice when spreading this script to others

# check parameters
if ($#argv == 0) then 
  echo === usage: ads-name-bib adsname without quotes
  echo === example: ads-name-bib rutten-rob
  exit
endif

# do this work in /tmp
cd /tmp

# get ADS search string for this name per database+namestring
printf "database: astronomy " > basestring
cat basestring ~/rr/www/bibfiles/ads/namestrings/$1 | urlencode > $1adsurl

# get the file Alberto's way
curl -H "Authorization: Bearer hahahahahahaha"  "https://api.adsabs.harvard.edu/v1/search/query?q="`cat $1adsurl`"&fl=bibcode&rows=1999"  | jq '{ bibcode: [ .response.docs[].bibcode ]}' | curl -H "Authorization: Bearer:hahahahahahaha" -X POST -d @- https://api.adsabs.harvard.edu/v1/export/bibtex |  jq -r '.export' > /tmp/$1.bib

# copy to bibraw for ads-correct-bib error checking
cp /tmp/$1.bib ~/rr/www/bibfiles/ads/bibraw/.

# correct ADS bibfile deformations 
ads-correct-bib /tmp/$1.bib

# move corrected version to ads/bib
mv /tmp/$1.bib ~/rr/www/bibfiles/ads/bib/.

# clean /tmp
rm  -f /tmp/$1adsurl /tmp/basestring

# back to calling dir
back
