#!/usr/bin/perl -w
#
#    Make sure we are in the main directory, not bin
#
  if (-d "bin") {
#      print "We are in main directory \n";
  }
  else{
#      print "Not in correct directory \n";
      chdir("../");
      if (-d "bin") {
#         print "We are in main directory \n";
      }
      else{
          print "Error gen_indexhtml-pl must be run from main or bin directory \
n";
          exit;
      }   
  }  

# Setting up the model 

# 0. decide if running from the web or not

    $web    = 0;
    $bindir = "bin";
    $modeldir = "../Models";
    foreach $line (@ARGV){
    chomp($line);
	if($line eq "--web"){
    	$web      = 1;
	$bindir   = "$ENV{'MADGRAPH_BASE'}/MG_ME/WebBin";
        $modeldir = "$ENV{'MADGRAPH_BASE'}/MG_ME/Models";
     }
    }
    
# 1. Reads in the proc_card.dat 

open (INPUT, "Cards/proc_card.dat");
@input=<INPUT>;
$length=$#input;
close(INPUT);

# 2. Extracts the model name and copies the relevant files into Source/MODEL
    my $model='none';
    my $found=0;
    foreach $i (@input){
	if($found==1){$model = $i;$found=0};
	if($i =~ '# Begin MODEL'){$found=1};
	}
	chomp($model);
    $model =~ s/\s+//g;  # clean from spaces
	print ">>$model<< \n";
  
    
	if (-d $modeldir."/".$model) {
      print "Model $model found  in $modeldir \n";
      }
  	else{print "Error Model $model not found \n";
          exit;
      }   

system ("rm -f Source/MODEL/* >/dev/null");
system ("cp -p $modeldir/$model/* Source/MODEL/.");
if (-e "Source/MODEL/ident_card.dat") {
    system ("cd Cards;ln -sf ../Source/MODEL/ident_card.dat");
}
if (-e "Source/MODEL/param_card.dat") {
    system ("mv Source/MODEL/param_card.dat Cards/");
    system ("cp -p Cards/param_card.dat Cards/param_card_default.dat");
}
if (-e "Source/MODEL/particles.dat") {
    system ("cd SubProcesses;ln -sf ../Source/MODEL/particles.dat .");
}
if (-e "Source/MODEL/interactions.dat") {
    system ("cd SubProcesses;ln -sf ../Source/MODEL/interactions.dat .");
}
if (-e "Source/MODEL/coupl.inc") {
    system ("cd SubProcesses;ln -sf ../Source/MODEL/coupl.inc .");
    system ("cd Source;ln -sf ./MODEL/coupl.inc .");
}
if (-e "Source/run.inc") {
    system ("cd SubProcesses;ln -sf ../Source/run.inc .");
}

# $B$ EXTRACT_TF $E$ ! Don't edit this line, this is a tag for MadWeight

# 3. Extracts information on multiparticles 

    $found=0;
    @multi=();
    foreach $i (@input){
	if($i =~ '#\s+End\s+MULTIPARTICLES'){$found=0};
	if($found==1){push(@multi,$i)};
	if($i =~ '#\s+Begin\s+MULTIPARTICLES'){$found=1};
	}

# 4. Write the information on multiparticles in particles.dat 	

open (INPUT, "Source/MODEL/particles.dat");
@input=<INPUT>;
$length=$#input;
close(INPUT);

open (OUTPUT, "> Source/MODEL/particles.dat");
$ini = 0;
$listpos = 0;
while($listpos<=$length && $ini==0){
    if($input[$listpos] =~ /MULTIPARTICLES/) {
	$ini=$listpos;
	print OUTPUT $input[$listpos]
	}        
    else{ 
	print OUTPUT $input[$listpos]
	}
    $listpos = $listpos + 1;
}

foreach $line (@multi){
    print OUTPUT "$line";
};
close(OUTPUT);




