#!/usr/bin/perl

open(TXT,"toc.txt");
open(TOC,">toc.tex");
open(SPI,">toc.spires");
while (<TXT>) {
   chomp(); $name = $_;
   $title = undef; $author = undef; $abstract = undef; $doi=undef;
   $spiaut = ""; %aff = undef;
   open (FILE,"$name.tex");
   while (<FILE>) {
      chomp(); $line = $_;
      $line=~s/^%.*$//; $line=~s/([^\\])%.*$/$1/;
      if ($line=~s/^.*\\title\{//) {
         $title = &extr($line) ;
      }
      if ($line=~/\\contribID\{([^}]+)\}/) {
         $doi=$1; $doi=~s/\\//g;
      }
      if ($line=~s/^.*\\author\{//) {
         $author = &extr($line) ; $spiaut = $author;
         $author =~s/ *\} *\\\\\[1ex.*$//;
         $author =~s/\$[\^\d]+\$//g;
         $author =~s/^ *\{ *\\slshape *//;
         
         $spiaut =~s/\{ *\\slshape *//; $spiaut =~ s/ *\} *$//;$spiaut =~ s/\\\\/ /g;
         if ($spiaut =~/^(.*)\} *\[[\d.]+ex\](.*)$/) {
            $spiaut = "";
            $sau = $1; $saf = $2;
            @sau = split(',',$sau);
            foreach $s (@sau) {
# $^{1,13}$   -> 1,13
               if ($s =~ s/\$ *\^\{? *([\d,]+) *\}? *\$//) {$df=$1} else {$df = undef;}
               $s =~ s/^(.*) ([^ ]+) *$/$2, $1/;
               $spiaut =  "$spiaut$s;";
               if ($df) { 
                  @df = split(',',$df);
                  foreach (@df) {$spiaut = "$spiaut=$_;";}
               }
            }
            $saf =~ s/\$ *\^\{? *(\d+) *\}? *\$/;$1=/g;$saf =~ s/^ *;//;
            $spiaut = "$spiaut\n2$saf;" ; 
            $spiaut =~ s/ +;/;/g; $spiaut =~ s/  +/ /g;
         }         
      }
      
      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/;
         }
      }
   
      if ($title && $author && $abstract) { last; }
   }
   
   print SPI "1 00001             ********************\n";
   print SPI "I* T\n";
   print SPI "C\n";
   
   if ($title) {
      print TOC "\\coltoctitle\{$title\}\n";
      print SPI "1$title.\n";
   }
   if ($author) {
      print TOC "\\coltocauthor{$author\}\n";
      print SPI "2$spiaut\n";
   }
   if ($doi) {
      print SPI "3URL=DOI; URLDOC=10.3204/DESY-PROC-2008-99/$doi;\n";
   }
   print TOC "\\Includeart\{\\CAUT\}\{\\CTIT\}\{$name\}\n\n";
   print SPI "3c: C99/99/99\n";
   if ($abstract) {print SPI "Abstract: $abstract\n";}
   close(FILE);
}
close(TOC);
close(TXT);
close(SPI);

sub extr {
   $var = $_[0] ;
   $var =~ s/\{([^{}]*)\}/&%$1%&/g;
   until ($var =~s/\}.*$//) {
      $line = <FILE>; chomp($line);
      $line=~s/^%.*$//; $line=~s/([^\\])%.*$/$1/;
      $var = $var . $line;
      $var =~ s/\{([^{}]*)\}/&%$1%&/g;
   }
   $var =~s/&%/{/g; $var =~s/%&/}/g;
   $var;
}
