#!/usr/local/bin/perl

#----------------------------------------------------------------------
#Program:        bibsort
#Authors:        C. Ewels, J. Elsner, University of Exeter, UK.
#Date:           12th July 1997
#Usage:          bibsort inputfile.tex > outputfile.tex
#Location:       /u3/aimpro/bin/         /u3/aimpro/bin/IRIX5/
#----------------------------------------------------------------------
#This perl program takes a standard LaTeX file and sorts the
#references into the order that they are called with \cite.  Currently
#unless using bibtex this had to be done manually.  Be warned - any
#\bibitem{} references which are not referred to from the text are
#removed, so it should only be used on a final version of a paper.
#Duplicate \bibitem{} listings are placed consecutively in the output
#file.  Nothing else is altered.

#Please report bugs and suggestions to eddy (ewels@excc.ex.ac.uk) or
#joachim (elsner@physik.tu-chemnitz.de)
 
# Things it doesn't do yet:
# 1. It misses out any references that aren't cited (probably good)
# 2. It doesn't handle \include files
# 3. It knows about comment lines but commented \% will slip through.
# 4. Note that citations in tables and figures are treated for the 
#    position they appear in the file, not where in the text the table
#    or figure is first referenced.

if (@ARGV != 1) {die "Usage: bibsort <texfile> \n";}; 

open(texfile,@ARGV[0]) || die "cannot open file @ARGV[0] for reading";

# First write out everything up to the first time \bibitem{ appears..

while(<texfile>) {
        if ( /\\bibitem/) {last;}
        {print "$_";} }
close(texfile);


# Now create an array of all the citations in order

open(texfile,@ARGV[0]);
while(<texfile>) {
# Now split the input file just after every \cite{ appears.
        @TEX = split(/cite{/,$_);
	# `n' is the citation number we're on at the moment.
        # @TEX[0] contains text before the cite{
	for ($n = 1; $n < @TEX;$n++) { 
	   # Now put the new \cite{XXX} `XXX' bit into the CITE stack...
           @NEWREF=split(/}/,@TEX[$n]);
	   #Next incorporates these refs into the CITE array, splitting
	   #records that are duplicates seperated by commas at the same time.
       	   @CITE= (@CITE,split(/\,/,@NEWREF[0]));  
	 }
}
close(texfile);

for ($n=0; $n < @CITE; $n++) {
# Now lets see if this reference has previously been cited?
       $duplicate = "N";
	for ($m =0; $m < $n; $m++) {
	# If the reference in the stack is the same as our current one, set
	# the flag..
	 if (@CITE[$m] eq @CITE[$n]){$duplicate = "Y";} 
        }
        if ($duplicate eq "N") {@CITESORT = (@CITESORT,@CITE[$n])}
   }

# Now paste in the \bibitem bits in the correct order
for ($n=0; $n < @CITESORT; $n++){
open(texfile,@ARGV[0]);
$writeout = "F";
$duplicate = "N";
while(<texfile>) {
# Have a flag to pick up if someone has two \bibitem{} with same name
       warn "Warning : Duplicate \\bibitem\{@CITESORT[$n]\}\n"
        if (/\\bibitem/ && /{@CITESORT[$n]}/ && $duplicate eq "Y"); 
# If the references have ended then everything stops!
       if ( /end{reference/ || /end{thebibliograph/) {$writeout = "F";}

# Does this line contain either \bibitem{, or \bibitem{XXX}?
# If it contains both, that means it's time to start writing out.
# If it just contains one, it means we're onto a new reference and
# it's time to stop writing out.
       if (/\\bibitem/ && /{@CITESORT[$n]}/ ) 
	   {$writeout = "T"; $duplicate = "Y";}
       if (/\\bibitem/ && !/{@CITESORT[$n]}/ ) {$writeout = "F";}
       if ($writeout eq "T") {print ($_);}
       }
close(texfile);
}

# Finally paste on the end of the file from the \end{references} onwards

$start = "F";
open(texfile,@ARGV[0]);
while(<texfile>) {
        if ((/\\end{references}/) || (/\\end{reference}/) ||
           (/\\end{thebibliography}/) || (/\\end{bibliography}/))
		     {$start = "T";}
        if ($start eq "T") {print ($_);}
		 }
close(texfile);

# Now lets check whether any references have not been cited at all

	   open(texfile,@ARGV[0]);
	   while(<texfile>) {
# Skip lines without bibitem on them..
	       if (!/bibitem/) {next;}
# Split the input file just after every \bibitem{ appears.
	       @TEX = split(/{/,$_);
# Pull from \bibitem{XXX} the `XXX' bit into variable `bibitem'.
	       @BIB = split(/}/,@TEX[1]);
	       $bibitem=@BIB[0];
# Compare $bibitem to the list of papers that are cited.
		   $found = "N";
		   for ($n = 0; $n < @CITESORT; $n++){
		       if ($bibitem eq @CITESORT[$n]) {$found="Y";}
		   }
           warn "Warning : \\bibitem{$bibitem} is not cited and was removed\n"
		   if ($found eq "N");
	   }
	   close(texfile);
	       



# Finally play them out...

system("sfplay /u3/ewels/store/analysis-verified.au");

