#!/usr/local/bin/perl

$cnum = "C09/05/11";
$cpbn = "In *DESY 2009, Photon 2009*";
$procno = "DESY-PROC-2009-03";

# anderes tex template:
$instform = 0;

#$setdoi = undef;
$setdoi = 1;

$dir = ".";
open(TXT,"toc.txt");
open(TOC,">$dir/toc.tex");
open(SPI,">$dir/toc.spires");
while (<TXT>) {
   chomp(); s/\.tex$//; $name = $_;
   $title = undef; $author = undef; $abstract = undef; $doi="xxx";
   $spiaut = ""; %aff = undef; $index = ""; $col=""; $saf=undef;
   open (FILE,"$name.tex");
   while (<FILE>) {
      chomp(); $line = $_;
      $line=~s/^%.*$//; $line=~s/([^\\])%.*$/$1/;
      if ($line=~s/^.*\\title\{//) {
         $title = &extr($line) ;
         $title =~ s/\\\\/ /g; 
         $title=~s/^ +//; $title=~s/ +$//; $title=~s/ +/ /g;
      }
      if ($line=~/\\contribID\{([^}]+)\}/) {
         $doi=$1; $doi=~s/\\//g;
      }
      if ($line=~s/^.*\\author\{//) {
         $author = &extr($line) ; 
# split off affiliations
         if ($author=~s/\\\\\[[\d.]+ex\](.*)$//) {$saf = $1;}
# delete institute numbers
         $author =~s/\$ *\^[^\$]+\$//g;
# try to find collaborations
         if ($author=~s/^ *\{ *\\slshape *(.*)\}(.*)//) {$author=$1; $col=$2;}
         if ($author=~s/\((.*)\)//) {$col = $1;}
         if ($author=~s/for the(.*)$//i) {$col = $1;}
         if ($author=~s/on behalf of the(.*)$//i) {$col = $1;}
# cleanup
         $author=~s/\\\\/ /g;
         $author=~s/\\&/,/g; $sau=~s/ and /, /g;
         $author=~s/\.([A-Z])/. $1/g;  # add space after .         
         
# clean up for spires
         $sau = &spiclean($author);
         $sau=~s/[{}\\]//g;            
         $sau=~s/\\?"([auoAUO])/$1</g;            
         $sau=~s/\\([oO])/$1\//g; 
         @sau = split(',',$sau);
         foreach $s (@sau) {
            $s =~ s/^(.*) ([^ ]+) *$/$2, $1/; #lastname in front
            $spiaut =  "$spiaut$s; ";
         }  
         @iau =  split(',',$author);
         foreach $s (@iau) {
            $s =~ s/^(.*) ([^ ]+) *$/$2, $1/; #lastname in front
            $index = "$index\\index{$s\}\n";
         }  
      }
      if ($line=~s/^.*\\institute\{//) {
         $saf = &extr($line) ; 
      }
      
      if ($line=~s/^.*\\begin\{abstract\}//) {
         $abstract = $line;
         $line = <FILE>; chomp($line);
         $line=~s/^%.*$//; $line=~s/([^\\])%.*$/$1/;
         until ($line =~s/\\end\{abstract.*$//) {
            $abstract = "$abstract\n$line";
            $line = <FILE>; chomp($line);
            $line=~s/^%.*$//; $line=~s/([^\\])%.*$/$1/;
         }
      }
      $abstract =~ s/\\\\/ /g;
      $abstract =~s//\n/g; 
      $abstract =~s/\n\s*\n/\n/g; 
   
      if ($title && $author && $abstract) { last; }
   }
            
   if ($col=~/coll/i || $col=~/exp/i) {
      $col=~s/the //i; $col=~s/for //i; $col=~s/on behalf of //i;
      $col=~s/^ +//; $col=~s/ +$//; $col=~s/ +/ /; $col=~s/[()]//g;
   } else {
      $col = undef;
   }
   
   if ($saf) {
      $saf =~ s/\$ *\^\{? *(\d+) *\}? *\$/\n2a: $1=/g; $saf =~ s/^\n2a: //;
      if($saf=~/\w/) {$spiaut = "$spiaut\n2a: $saf;" ; }
   }
   $spiaut =~ s/ +;/;/g; $spiaut =~ s/  +/ /g;
   $spiaut =~ s/;+/;/g;
   $spiaut =~ s/\\\\/ /g;
   
#   unless ($title) { last; }
   
   print SPI "1 00001             ********************\n";
   print SPI "I* T\n";
   print SPI "C\n";
   
   if ($title) {
      print TOC "\\coltoctitle\{$title\}\n";
      $title = &spiclean($title);
      print SPI "1$title.\n";
   }
   if ($col) {
      print SPI "A$col;\n";
   }
   if ($author) {
      print TOC $index;
      print TOC "\\coltocauthor{$author\}\n";
      print SPI "2$spiaut\n";
   }
   print SPI "3$cpbn 1-2.\n";
   if ($setdoi) {
      print SPI "3URL-STR(9); URL=DOI;\n";
      print SPI "3URLDOC=10.3204/$procno/$doi;\n";
   }
   print TOC "\\Includeart\{\\CAUT\}\{\\CTIT\}\{$name\}\n\n";
   print SPI "3c: $cnum\n";
   if ($abstract and $setdoi) {print SPI "Abstract: $abstract\n";}
   close(FILE);
}
close(TOC);
close(TXT);
close(SPI);

sub extr {
   $var = $_[0] ;
   while ($var =~ /\{([^{}]*)\}/) {$var =~ s/\{([^{}]*)\}/&#$1#&/g;}
   until ($var =~s/\}.*$//) {
      $line = <FILE>; chomp($line);
      $line=~s/^%.*$//; $line=~s/([^\\])%.*$/$1/;
      $var = $var . $line;
      while ($var =~ /\{([^{}]*)\}/) {$var =~ s/\{([^{}]*)\}/&#$1#&/g;}
   }
   $var =~s/&#/{/g; $var =~s/#&/}/g;
#   $var =~s/\\\\/ /g; 
   $var =~s// /g; $var =~s/ +/ /g; $var =~s/\\,/ /g;
   $var =~s/([^\\])~/$1 /g;
   $var =~s/\\protect//g; $var=~s/\\footnote\{[^}]*\}//g;$var=~s/\\thanks\{[^}]*\}//g;
   $var;
}
sub spiclean{
   $var = $_[0] ;
   $var=~s/\\['`^~vc] *//g;            
   $var;
}
