#!/usr/bin/perl -w
# fix a PDFlatex 'ifeffit.out' file.  Fairly specialized for Feffit
$file  = $ARGV[0];
$savefile = $file.".bak"; 
rename ($file,$savefile) or die "couldn't rename $file\n";
open(IN,  "<$savefile")  or die "couldn't open $savefile\n";
open(OUT, ">$file")      or die "couldn't open $file\n";

while (<IN>) {
    chomp; 
    s/\\_/_/go;
    s/\\137/_/go;
    $line = $_;
    if (/\\BOOKMARK \[(.*)\]{(.*)}{(.*)}{(.*)}/ ) {
 	$n = $1; $x1 = $2;    $x2 = $3;	$x3 = $4;
	$x2 =~ s/\{//go;  
	$x2 =~ s/\}//go; 
	$x2 =~ s/\$//go;
	$x2 =~ s/\\chi /chi/go;
	$x2 =~ s/nscifeffit/IFEFFIT/go;
	$x2 =~ s/nscfeff/FEFF/go;
	$x2 =~ s/ \(k\) / chi(k) /go;
	$x2 =~ s/PD1OT1ptmptmmmnn//go;
	$x2 =~ s/\\(\w*) //go; 
	$line = " \\BOOKMARK [$n]{$x1}{$x2}{$x3}"; 
     }
    print OUT "$line\n";
}
close(IN);
close(OUT);
