#!/usr/local/bin/perl
#
# translate old (Version 5) bem model files to new (Version 6) format
#
# usage: m5tom6 <name>
#        translates <name>.m5 to <name>.m {and <name>.t}
#
# Bemerkungen: 
#   "typ:gap" wird nicht mehr unterstützt.
#   kann nur 1-Topf-Versuche übersetzen, nicht Kaskaden





$nam=$ARGV[0];

$in="$nam.m5";
die "$in not found." unless -f $in;

$d=`date`;
$hdg="* translated by m5tom6 $d\n";
open(INP,$in);
open(OUT,">$nam.m");
$tfile=0;
$theta=1.;

print OUT $hdg;
$sf1="";
$sf2="";
while(<INP>){
    $out=0;
    s/:([\.\+\-0-9])/ $1/g;
    s/^end/* end/;
    s/^run/* run/;
    s/^wptslt/* wptslt/;
    s/^estcon/estsng/;
    s/^cex/xeqs/;
    s/^(vls.*)pco2/$1PCO2/;
    s/([0-9]\.)d([\+\-])/$1e$2/g;

    $nwrds=split(' ');
    $t="\U$_[0]";
    if($t eq "CO2"){
	$s=$_[2]+$_[3];
	print OUT "gas CO2 $_[1]\n";
	print OUT "elt CO3 -2\n";
	print OUT "cpl (H)(CO3) $_[3]\n";
	printf OUT "cpl (H)2(CO3) %g\n",$s;
	$out=1;
    }

    if($t eq "ANI"){
	print OUT "elt $_[1] -$_[2]\n";
	$out=1;
    }
    if($t eq "CAT"){
	$iscat{$_[1]}=1;
	print OUT "elt $_[1] $_[2]\n";
	$out=1;
    }

    if($t eq "VLS"){
	$x="";
	for($i=0;$i<=$#_;$i++){
	    $a="\U$_[$i]";
	    if($a eq "SSR"){
		$_[$i]="rho 1 theta0";
		$f=1./$_[$i+1];
		$_[$i+1]=sprintf("%g",$f);
		$theta=$f;
	    } 
	    if($a eq "CEC"){
		print OUT "xs0 cec $_[$i+1]\n";
		$_[$i]='';
		$_[$i+1]='';
		$nwords -= 2;
	    }
	    if($a eq "ESTSTR" && $cex){
		$x .= "istr $_[$i+1] ";
	    }
	    if($a eq "ESTPH" && $cex){
		$x .= "pH $_[$i+1] ";
	    }
	}
	if($nwrds >1){
	    for($i=0;$i<=$#_;$i++){
		print OUT "$_[$i] ";
	    }
	    print OUT "\n";
	    $out=1;
	}
	if($x){
	    print OUT "xeql $x\n";
	}
    }


    if($t eq "CEC"){
	$cex=1;
	for($i=0;$i<=$#_;$i++){
	    $a="\U$_[$i]";
	    if(substr($a,0,3) eq "TYP"){
		$_[$i]='';
		$nwrds -= 1;
	    }
	}
	if($nwrds > 1){
	    $cex=0;
	    $_[0]="xpksel";
	    for($i=0;$i<=$#_;$i++){
		print OUT "$_[$i] ";
	    }
	    print OUT "\n";
	}
	$out=1;
    }

    if($t eq "ESTSNG" && $cex){
	$x="";
	for($i=1;$i<=$#_;$i += 2){
	    if($iscat{$_[$i]}){
		$x .= "$_[$i] $_[$i+1] ";
	    }
	}
	if($x){
	    print OUT "xeql $x\n";
	}
    }

    if($t eq "SLT" && substr("\U$_[2]",0,3) eq "SFC"){
	$sf1="slt $_[1]";
	$out=1;
    }
    if($t eq "P01"){
	$c=-$_[2];
	$c=sprintf("%6g",$c);
	$sf2="spfun 1 $_[3] $_[1] $c";
	$out=1;
    }
    if($t eq "P02"){
	$sf2="spfun 2 $_[1] $_[2] $_[3] $_[4] $_[5] $_[6] ";
	$out=1;
    }
    if($sf1 && $sf2){
	print OUT "$sf1 $sf2\n";
	$sf1="";
	$sf2="";
    }
 
   if(($t eq "EXP" || "\U$_[1]" eq "END") && $vol>0){
	printf OUT "vinp %6g\nvout %6g\n",$vol,$vol;
	$vol=0;
    }

    if($t eq "EXP"){
	if($tfile==0){
	    open(OUT,">$nam.t");
	    print OUT $hdg;
	    $tfile=1;
	    print OUT "define A $nam.m\n\n";
	}
	$out=1;
	for($i=0;$i<=$#_;$i++){
	    $a="\U$_[$i]";
	    if($a eq "NREP"){
		print OUT "steps $_[$i+1]\ncell A\n";
	    } 
	    if($a eq "FLOW"){
		$vol=$_[$i+1]*$theta*1000;
		$v=1;
	    }
	}
    }

    if($t eq "CTIN"){
	for($i=0;$i<=$#_;$i++){
	    $a="\U$_[$i]";
	    if($a eq "PCO2"){
		print OUT "dpgas CO2  $_[$i+1]\n";
		$_[$i]='';
		$_[$i+1]='';
		$nwrds -= 2;
	    }
	}
	if($vol > 0.0){
	    printf OUT "vinp %6g ",$vol;
	}
	else{
	    print OUT "cinp ";
	}
	if($nwrds > 1){
	    for($i=1;$i<=$#_;$i++){
		print OUT "$_[$i] ";
	    }
	}
	print OUT "\n";
	if($vol > 0.0){
	    printf OUT "vout %6g\n",$vol;
	    $vol=0;
	}
	$out=1;
    }


  if($out == 0){
	print OUT;
    }
}

