#!/usr/bin/perl
#
# This script generates a runme file ("runme.default") from the
# input.defines file, setting all parameters to "default."
#
# Note that the runme may fail at first due to missing supporting files
# (such as a "swdata" file) or unset user-specific settings (such as 
# LOCALF77 and TARGET).
#
# WDC, SSC/UNH, 2014-08, D.Cramer@unh.edu
# 

my $ggcmdir = $ENV{'OPENGGCMDIR'};
my $f_defines = "$ggcmdir/include/input.defines";
print $f_defines;

#keys are: help,scope,type,def

open (OUTFILE,">runme.default") || die "Cannot open file: $!\n";
print OUTFILE "#!/usr/bin/env bash\n";
print OUTFILE "\$OPENGGCMDIR/bin/script.runme \$(pwd) \$(basename \$0) 0; exit\n";
print OUTFILE "\n";
readDefines($f_defines, OUTFILE);
close(OUTFILE);

exit 0;

# ======================================================================
# printParam

sub printParam {
  my $param = shift;
  my $fh_out = shift;

  my $nameu = uc($param->{name});
  my $help = $param->{help};
  my $scope = $param->{scope}; #shell,logical,hash,inf(2+ sep by ':')
  my $type = $param->{type};   #txt,text(?),bool,choice,hidden
  my $value = $param->{def};

  $value =~ s/\s*\{\s*(.*?)\s*\}\s*/$1/;
  $help =~ s/\s\s+/ /g;
    
  if ($type ne "hidden") {
    #print $fh_out "$nameu\t\t$value\t# $help (def=$value)\n";
    #printf $fh_out ("%-15s %-10s # %-s (def=%-s)\n",$nameu,$value,$help,$value);
    printf $fh_out ("%-15s  default  # %-s (def=%-s)\n",$nameu,$help,$value);
  }
}

#-----below is shamelessly copied and modified from script.includes-----

# ======================================================================
# readDefines

sub readDefines {
  my $filename = shift;
  my $fh_out = shift;
  my $paramref;
  my @comments;
  my $first = 1;

  open (INFILE,"<$filename") || die "Cannot open file: $!\n";
  my $line = '';
  while(<INFILE>) {
    if ( /^#/ ) {
      if ( $first ) {
        # print comments until first parameter found
        print $fh_out $_;
      } else {
        # save comments 
        push(@comments, $_);
      }
      next;
    }
    chop;
    if ( /^\w/ ) {
      # next variable
      my $prevLine = $line;
      $line = $_;
      if ($prevLine) {
	# process current variable
        if ( $first ) { $first = 0; }
	$paramref=processDefineLine($prevLine);
        printParam($paramref,$fh_out);
        if (@comments > 0) {
          print $fh_out @comments;
          @comments = ();
        }
      }
    } else {
      # continuation line, append
      $line .= $_;
    }
  }
  if (@comments > 0) {
    print $fh_out @comments;
    @comments = ();
  }
  $paramref=processDefineLine($line);
  printParam($paramref,$fh_out);
}

# The parsing rules: Look for key=val pairs
# val can be a a string without whitespace
# if it contains whitespace, it needs to be enclosed into { braces }
# if it contains braces, it needs to be enclosed into {{ double braces }}

sub processDefineLine {
  my $line = shift;

  my %h;
  ( my $name, $_ ) = split(' ', $line, 2);
  $h{name} = lc($name);

  while ($_) {
    s/^\s*//;
    if ( $_ eq '' ) {
      last;
    }
    ( my $key, $_ ) = split('=', $_, 2);
    if ( s/^\s*\{\{(.*?)\}\}// ||
	 s/^\s*\{(.*?)\}// ||
	 s/^([^\s]*)// ) {
      $h{$key} = $1;
    } else {
      print STDERR $indent."WARNING: no value for '$key' in '$name'.\n";
      $h{$key} = '';
    }
  }
  return \%h;
}

