#!/usr/bin/env perl

my $test = 1;

-e "DONE" or die;
-e "annotated.dat" or die;
if (!$test) {
    !-e "optical.dat" or die;
    !-e "n.dat" or die;
}

chomp(my $gap = `getparam bcsgap`);

open(O, "lastblock annotated.dat |") or die;
while (<O>) {
    /(\S*)\s\((\S*)\)/ or next;
    $E = $1;
    $S = $2;
#    print "$E $S\n";
    if ($S == 1 and !defined($first1)) {
        $first1 = $E;
        next;
    }
    if ($S == 2 and !defined($first2)) {
        $first2 = $E;
        next;
    }
    if ($S == 1 and !defined($second1)) {
        $second1 = $E;
        next;
    }
    if ($S == 2 and !defined($second2)) {
        $second2 = $E;
        next;
    }
    if ($S == 1 and !defined($third1)) {
        $third1 = $E;
        next;
    }
    if ($S == 2 and !defined($third2)) {
        $third2 = $E;
        next;
    }
}
close O;

# First line: singlet states
# Second line: doublet states
open(O, ">subgap.dat");
print O "$first1 $second1 $third1\n";
print O "$first2 $second2 $third2\n";
close O;

$optical1 = $second1-$first1;
$optical2 = $second2-$first2;
$optical1 /= $gap;
$optical2 /= $gap;
print "$optical1 $optical2\n";
open(O, ">optical.dat");
print O "$optical1 $optical2\n";
close O;

# Third is ALWAYS a continuum state!
$continuum1 = $third1-$first1;
$continuum2 = $third2-$first2;
$continuum1 /= $gap;
$continuum2 /= $gap;
print "$continuum1 $continuum2\n";
open(O, ">continuum.dat");
print O "$continuum1 $continuum2\n";
close O;

# Binding energy of the second singlet state (wrt singlet continuum)
$bind = $third1 - $second1;
print "$bind\n";
open(B, ">bind.dat");
print B "$bind\n";
close B;

$EYSR = $first1-$first2;
$EYSR /= $gap;
print "$EYSR\n";
open(O, ">EYSR.dat");
print O "$EYSR\n";
close O;


open(L, "<log") or die;
while (<L>) {
    if (/Iteration\s(\S*)\/(\S*)/) {
        $nr = $1;
        $max = $2;
        if ($nr == $max) {
            last;
        }
    }
}

while (<L>) {
    if (/Diagonal matrix elements of operator P0/) {
        last;
    }
}
$_ = <L>;
/1:\s(\S*)\s(\S*)\s(\S*)/ or die;
$P01a = $1;
$P01b = $2;
$P01c = $3;
$_ = <L>;
/2:\s(\S*)\s(\S*)\s(\S*)/ or die;
$P02a = $1;
$P02b = $2;
$P02c = $3;

while (<L>) {
    if (/Diagonal matrix elements of operator P1/) {
        last;
    }
}
$_ = <L>;
/1:\s(\S*)\s(\S*)\s(\S*)/ or die;
$P11a = $1;
$P11b = $2;
$P11c = $3;
$_ = <L>;
/2:\s(\S*)\s(\S*)\s(\S*)/ or die;
$P12a = $1;
$P12b = $2;
$P12c = $3;

while (<L>) {
    if (/Diagonal matrix elements of operator P2/) {
        last;
    }
}
$_ = <L>;
/1:\s(\S*)\s(\S*)\s(\S*)/ or die;
$P21a = $1;
$P21b = $2;
$P21c = $3;
$_ = <L>;
/2:\s(\S*)\s(\S*)\s(\S*)/ or die;
$P22a = $1;
$P22b = $2;
$P22c = $3;

while (<L>) {
    if (/Diagonal matrix elements of operator n_d/) {
        last;
    }
}
$_ = <L>;
/1:\s(\S*)\s(\S*)\s(\S*)/ or die;
$n1a = $1; # Occupancies of the first three singlet states
$n1b = $2;
$n1c = $3;
$_ = <L>;
/2:\s(\S*)\s(\S*)\s(\S*)/ or die;
$n2a = $1;
$n2b = $2;
$n2c = $3;

print "$n1a $n2a\n";
open(O,">n.dat");
print O "$n1a $n2a\n";
close O;

print "$n1a $n1b $n1c $n2a $n2b $n2c\n";
open(O,">n1.dat");
print O "$n1a $n1b $n1c\n";
close O;
open(O,">n2.dat");
print O "$n2a $n2b $n2c\n";
close O;

print "$P01a $P01b $P01c $P02a $P02b $P02c\n";
open(O, ">P01.dat") or die;
print O "$P01a $P01b $P01c\n";
close O;
open(O, ">P02.dat") or die;
print O "$P02a $P02b $P02c\n";
close O;

print "$P11a $P11b $P11c $P12a $P12b $P12c\n";
open(O, ">P11.dat") or die;
print O "$P11a $P11b $P11c\n";
close O;
open(O, ">P12.dat") or die;
print O "$P12a $P12b $P12c\n";
close O;

print "$P21a $P21b $P21c $P22a $P22b $P22c\n";
open(O, ">P21.dat") or die;
print O "$P21a $P21b $P21c\n";
close O;
open(O, ">P22.dat") or die;
print O "$P22a $P22b $P22c\n";
close O;

if (!$test) {
    unlink "log";
    unlink "annotated.dat";
    unlink "data";
    unlink "mmalog";
    unlink "log2";
}
