#!/bin/csh -f
#(ie run the cshell on this but don't read the .cshrc)

echo version = 1.12 of mksubbib 2019 Aug 26
# 2019 Aug 26, 1.12: remove material inside [] in cites
# 2019 Jul 30, 1.11: 'cat: /tmp/schneidt.mksubbib: No such file or directory'???
# 2012 Jul 03, 1.10: generalize for non-delila systems
# 2012 Jun 19, 1.09: upgrade documentation for release; remove APS code;
#                    remove censor dependency
# 2008 May 01, 1.08: modify so it handles APS
#                    American Physics Society REVTeX4/apsrev.bst
#                    http://authors.aps.org/revtex4/
# 2005 Oct 14, 1.07: otherstuff set to empty as default.
# 2005 Aug  6, 1.06: secondary bib database
# 2005 Aug  6, 1.05: correct documentation
# 2005 Jul 11, 1.04: spelling correction
# 2004 Jun 14, 1.03: report number of entries
# 2003 Feb 15, 1.02: document upgrade
# 2001 Aug 20, 1.01: functional
# origin 2001Aug20.22:45:55

if ($#argv == 0) then
  echo 'usage: mksubbib [otherbib]'
  echo 'Make a sub-bibliography.'
  echo 'Create a subset of a LaTeX bibliography corresponding' # usage
  echo 'just to the references of a LaTeX document.' # usage
  echo ''
  echo 'For a given paper, paper.tex and paper.bbl, create a subdirectory'
  echo 'and run this script in the sub directory.  The resulting'
  echo 'file will be in paper.bib.'
  echo ''
  echo 'required: subbib.bst'
  echo '  The program will attempt to link to it in the delila directory'
  echo '  If that fails it will try to get it using wget.'
  echo ''
  echo 'required: ../paper.bbl'
  echo 'a LaTeX bibliography must be in the directory above.'
  echo ''
  echo 'required: ../all.bib'
  echo 'a bibliography database from which to extract everything must'
  echo 'be in the directory above'
  echo ''
  echo 'If there is one argument, then that is the name of a second'
  echo "bibliography database.  Suppose it is called 'other.bib'."
  echo 'Then there must be a file called ../other.bib.'
  echo 'The usage would be:'
  echo '   mksubbib other'
  echo ''
  echo 'Program dependencies:'
  echo ' lkdelila'
  echo ' latex'
  echo ' bibtex'
  echo ' wget (if the delila directory does not exist)'
  echo ''
endif

# *****************************************************************************

if !(-f ../paper.bbl) then
  echo "There must be a ../paper.bbl file to use"
  echo "as the basis of the bibliography"
  exit
endif

if !(-f ../all.bib) then
  echo "There must be a ../all.bib file to use"
  echo "as the basis of the bibliography"
  exit
else
  if !(-e all.bib) then
     lk all.bib ../all.bib
  endif
endif

# Look for a subbib.bst file
if !(-f subbib.bst) then
  # Does lkdelila exist in the user's path?
  if (`which lkdelila|grep '/'|wc -l` > 0) then
     lkdelila subbib.bst
  endif
endif

if !(-f subbib.bst) then
  # Does wget exist in the user's path?
  if (`which wget|grep '/'|wc -l` > 0) then
     wget subbib.bst
  endif
endif

# If the above attempts failed, die:
if !(-e subbib.bst) then
  echo 'Could not find subbib.bst!'
  echo 'Obtain it from:'
  echo 'https://alum.mit.edu/www/toms/ftp/subbib.bst'
  exit
endif

if ($#argv == 1) then
   # The argument is the name of the other bib
   set other = $1
   if !(-f ../$other.bib) then
      echo There is no ../$other.bib, ignoring argument
      set otherstuff = ""
   else
      set otherstuff = ",other"
      lk other.bib ../$other.bib
   endif
else
   set otherstuff = ""
endif

set filestodelete = "paper.aux paper.bbl paper.blg paper.dvi paper.log paper.tex"
foreach filetodelete ( $filestodelete )
   echo 'y' | rm -f filetodelete
end
echo -n '' > paper.bib

set keys = paper.tex
echo '\documentclass[12pt]{article}' > $keys
echo '\begin{document}' >> $keys
echo '\bibliographystyle{subbib}' >> $keys

# Mechanism for using censor (removed 2019 Jul 30)
# set tmp1 = /tmp/`whoami`.mksubbib
# echo '(* turn on censor program:  ' > $tmp1
# cat $tmp1 ../paper.bbl |\
# for APS remove material inside [ ] \  # unnecessary in general 2012 Jun 19 \
#sed 's/\[/[[/' |\  # unnecessary in general 2012 Jun 19 \
#sed  's/]/]]/' |\  # unnecessary in general 2012 Jun 19 \
# censor |\  # unnecessary? 2012 Jun 19 \

# 2019 Aug 26: modify to remove '[...]' material
cat ../paper.bbl |\
grep 'bibitem' |\
sed -e "s/bibitem/cite/" |\
#sed 's/\[/[BRACKET-BEGIN/' |\
#sed 's/]/BRACKET-END]/' |\
#sed 's/\[/[BRACKET-BEGIN/' |\
#sed 's/]/BRACKET-END]/' |\
sed 's/\[/[BRACKET-BEGIN/' |\
sed 's/]/BRACKET-END]\\cite/' |\
tr ']' '\n' |\
grep -v 'BRACKET-BEGIN' |\
cat >> $keys

echo "\bibliography{all$otherstuff}" >> $keys
echo '\end{document}' >> $keys

echo +++++++++++++ $keys ++++++++++++++++++++++++++++++++++++++++++++++++++
cat $keys
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo

#echo BUBBA HALT
#tomdate
#exit

latex paper.tex
bibtex paper

mv paper.bbl paper.bib

echo '****************************'
echo "* Results are in `pwd -P`/paper.bib *"
echo '****************************'
echo "number of entries found:"
grep '^@' paper.bib | wc -l
echo '****************************'

# heta -d -3 paper.bib
