#!/bin/tcsh -f
#(ie run the tshell on this but don't read the .cshrc or .tcshrc)

set ver = "version = 1.06 of replacebib 2014 May 22"
#       2014 May 22, 1.06: upgrade web locations
#       2012 Jun 19, 1.05: upgrade according to Ren?e Otte's email
#       2012 Jun 19, 1.03: merge two versions of replacebib!
#                          comment out original bibliography call!
#       2009 Nov 14, 1.02: improve version description
#       2009 Jun 26, 1.01: functional - add author documentation
#       2009 Jun 26, 1.00: origin 

if ($#argv == 0) then
   echo 'usage: replacebib [source name] [target name]'
   echo 'Replace the bibliography calls in a LaTeX document'
   echo 'with the bibliography text itself.' # usage
   echo 'This is required by PNAS as described in the instructions at' # usage
   echo 'http://www.pnas.org/site/misc/LaTex.shtml' # usage

   echo
   echo 'The source is the latex file to use as input.'
   echo 'The target is the latex file with the bibliography inserted.'

   echo
   echo 'SET UP:'

   echo
   echo '1. Below your \begin{document} line put this line:'
   echo '\bibliographystyle{pnas}'
   echo 'Use the pnas.bst file available from'
   echo 'https://alum.mit.edu/www/toms/latex.html'
   echo "(Note: You can use any format, this program won't care.)"

   echo
   echo '2. At the point where your bibliograph should go, insert:'
   echo '\bibliography{all}'
   echo 'where "all" is the name of your bibliograph database eg "all.bib".'
   echo 'This is just above "\end{article}" for the PNAS style.'

   echo
   echo '3. Use the pnas.bst file available from'
   echo 'https://alum.mit.edu/www/toms/latex.html'

   echo
   echo '4. Run latex to create your source.bbl file.'

   echo
   echo 'WHAT THIS PROGRAM DOES:'
   echo
   echo 'Remove the \bibliographystyle{pnas} line.'
   echo 'Replace \bibliography{...} with the source bbl file.'

   echo
   echo 'If there is only one argument (any token) then:'
   echo '- the source is set to "paper"'
   echo '- the target is set using the version name.'
   echo 'The version name is on a line containing "version = "'.
   echo 'The version name is a space delimited word containing ".tex".'

   # The version can be found by the ver program:
   # https://alum.mit.edu/www/toms/delila/ver.html
   # However, this is coded into replacebib so there is no dependency.

   echo
   echo 'For example if your paper contains:'
   echo '   \newcommand{\theversion}%'
   echo '      {{version = 1.88 of emmgeo.tex 2009 Nov 10}}'
   echo 'and you run this script with the argument "-bib":'
   echo '     replacebib -bib'
   echo 'then the target name will be "emmgeo-bib.tex"'

   echo
   echo 'RESULTS:'
   echo
   echo 'The output target file is generated.'
   echo 'The program reports the line where the biblography was inserted'
   echo 'and show the first few and last difference lines'
   echo 'between the input and output files.'
   echo 'You should verify that the \bibliographystyle{pnas}'
   echo 'line is gone and that the entire bibliography has been inserted.'

   echo
   echo "Permanent location of this program:"
   echo "https://alum.mit.edu/www/toms/ftp/replacebib"

   echo
   echo "$ver"
   echo

   echo "  Dr. Thomas D. Schneider"
   echo "  toms@alum.mit.edu"
   echo "  toms@alum.mit.edu (permanent)"
   echo "  https://alum.mit.edu/www/toms (permanent)"
   exit
endif

if ($#argv == 1) then
   set source = 'paper'
   # now use paper.tex to find the target name

   if !(-f ${source}.tex) then
      echo "There is no ${source}.tex file."
      exit 
   #else
   #   echo "${source}.tex exists."
   endif
   set version = "`grep 'version =' ${source}.tex`"
   if ("$version" == '') then
      echo 'No version line exists - HALT'
      exit
   endif
   echo "Using version line:"
   echo "$version"
   set rawtarget = `echo "$version" | tr ' ' '\n' | grep '.tex' `
   set target = `echo "$rawtarget" | sed "s/.tex/$1/"`
else
   set source = `echo $1 | sed 's/.tex//'`
   set target = `echo $2 | sed 's/.tex//'`
endif

if !(-f ${source}.tex) then
   echo There is no ${source}.tex file.
   exit 
else
   echo "source ${source}.tex exists."
endif

if !(-f ${source}.bbl) then
   echo There is no ${source}.bbl file.
   exit 
else
   echo "source ${source}.bbl exists."
endif

echo the target name is: "${target}.tex"

if ("${target}.tex" == "${source}.tex") then
   echo "${source}.tex = ${target}.tex - HALT to avoid overwriting source"
   exit
endif

# echo ---- thingy
# grep '\\bibliography{' ${source}.tex
# echo ----
# exit

# 2012 Jun 19
# On Sun OS
# grep '\bibliography{' ${source}.tex
# works and identifies the bibliography line.
# On Mac OS X 10.6.8 and Cygwin this
# fails to find the bibliography line.  However
# grep '\\bibliography{' ${source}.tex
# or
# grep 'bibliography{' ${source}.tex
# works on Sun and OS X.  Thanks to R.A. Otte for this tip.

# grep '\bibliography{' ${source}.tex
set breakpoint = `grep -n '\\bibliography{' ${source}.tex|tr ':' '\n' |head -1`
if ("$breakpoint" == '') then
   echo "'\bibliography{' is NOT in ${source}.tex"
   exit
endif
echo "'\bibliography{' is in ${source}.tex at line $breakpoint"
@ headpart = $breakpoint - 1

head -${headpart} ${source}.tex |\
grep -v '\\bibliographystyle{' |\
cat > ${target}.tex

# echo "new stuff" >> ${target}.tex
cat "${source}.bbl" >> ${target}.tex

tail -n +${breakpoint} ${source}.tex |\
sed 's/\\bibliography{/% original source: \\bibliography{/' |\
cat >> ${target}.tex

#cat ${target}.tex

echo "------- diff ${source}.tex ${target}.tex | head"
diff ${source}.tex ${target}.tex | head

echo "------- diff ${source}.tex ${target}.tex | tail"
diff ${source}.tex ${target}.tex | tail
echo "-------"

