#!/usr/bin/perl
#
# Configuration script for Vortex tracker code
# 
# Be sure to run as ./configure (to avoid getting a system configure command by mistake)
#

# make sure we do not buffer stdout
select((select(STDOUT), $|=1)[0]);
my %sw=(corepath=>'', libw3=>'', libz=>'', libjasper=>'', libpng=>'',
          libbacio=>'ARCH', os=>'ARCH', ompparallel=>'', dmparallel=>'',
          mach=>'',fc=>'$(SFC)', cc=>'$(SCC)', f90=>'$(SF90)',
          libg2=>'');
while(substr($ARGV[0],0,1) eq '-') {
    if($ARGV[0]=~/^-([a-zA-Z0-9_-]*)=(.*)$/ms) { # find NAME=VALUE in $ARGV[0]
        my ($opt,$val)=($1,$2);
        if(defined($sw{$1})) {
            $sw{$1}=$2;
        } else {
            warn "WARNING: ignoring unrecognized option $ARGV[0]\n"
        }
    }
    shift @ARGV;
}

#my (@fflags,@libs,@glob,$libdir);
#foreach $lib ( qw{g2 w3 bacio png z jasper} ) {
#    $LIB=uc($lib);
#    $libdir=$sw{"lib$lib"};
#    if(-d $libdir) {
#        foreach $subd (qw{lib64 lib32 lib}) {
#            @glob=glob("$libdir/$subd/lib$lib.*");
#            if($#glob>=0) {
#                push @libs,"-L$libdir/$subd","-l$lib";
#            }
#        }
#        foreach $subd (qw{include src/lib/$lib}) {
#            @glob=glob("$libdir/$subd/*.mod");
#            if($#glob>=0) {
#                push @fflags,"-I$libdir/$subd"
#            }
#        }
#    } elsif(! -z $libdir) {
#        warn("lib$lib (\$LIB_${LIB}_PATH): no path given for this library.  I'll assume it is in /usr.\n");
#        $sw{"lib$lib"}='/usr';
#        push @libs,"-l$lib";
#    }
#}
#my $tr_fflags=join(' ',@fflags);
#my $tr_libs=join(' ',@libs);

sub fixup_configure_lines($) {
    $_=$_[0];
    s/CONFIGURE_TRACKER_FFLAGS/$tr_fflags/g;
    s/CONFIGURE_TRACKER_LIBS/$tr_libs/g;
    s/CONFIGURE_PNG_PATH/$sw{libpng}/g ;
    s/CONFIGURE_Z_PATH/$sw{libz}/g ;
    s/CONFIGURE_G2_PATH/$sw{libg2}/g ;
    s/CONFIGURE_JASPER_PATH/$sw{libjasper}/g ;
    s/CONFIGURE_W3_PATH/$sw{libw3}/g ;
    s/CONFIGURE_BACIO_PATH/$sw{libbacio}/g ;
    s/CONFIGURE_FC/$sw{fc}/g ;
    s/CONFIGURE_F90/$sw{f90}/g ;
    s/CONFIGURE_CC/$sw{cc}/g ;
    return $_;
}

# parse the configure.defaults file

$validresponse = 0 ;
@platforms = qw ( serial ) ;

# Display the choices to the user and get selection
until ( $validresponse ) {
  printf "------------------------------------------------------------------------\n" ;
  printf "Please select from among the following supported platforms.\n\n" ;

  $opt = 1 ;
  open CONFIGURE_DEFAULTS, "< ./arch/configure.defaults" 
      or die "Cannot open ./arch/configure.defaults for reading" ;
  while ( <CONFIGURE_DEFAULTS> )
  {
    for $paropt ( @platforms ) 
    {
      if ( substr( $_, 0, 5 ) eq "#ARCH" && ( index( $_, $sw{os} ) >= 0 ) && ( index( $_, $sw{mach} ) >= 0 )
	   && ( index($_, $paropt) >= 0 )  )
      {
        $optstr[$opt] = substr($_,6) ;
        $optstr[$opt] =~ s/^[ 	]*// ;
        $optstr[$opt] =~ s/#.*$//g ;
        chomp($optstr[$opt]) ;
        $optstr[$opt] = $optstr[$opt]." (".$paropt.")" ;
        if ( substr( $optstr[$opt], 0,4 ) ne "NULL" )
        {
          printf "  %2d.  %s\n",$opt,$optstr[$opt] ;
          $opt++ ;
        }
      }
    }
  }
  close CONFIGURE_DEFAULTS ;

  $opt -- ;

  printf "\nEnter selection [%d-%d] : ",1,$opt ;
  $response = <STDIN> ;

  if ( $response == -1 ) { exit ; }

  if ( $response >= 1 && $response <= $opt ) 
  { $validresponse = 1 ; }
  else
  { printf("\nInvalid response (%d)\n",$response);}
}
printf "------------------------------------------------------------------------\n" ;

$optchoice = $response ;

open CONFIGURE_DEFAULTS, "< ./arch/configure.defaults" 
      or die "Cannot open ./arch/configure.defaults for reading" ;
$latchon = 1 ;
while ( <CONFIGURE_DEFAULTS> )
{
  if ( substr( $_, 0, 5 ) eq "#ARCH" && $latchon == 1 )
  {
    $latchon = 0 ;
  }
  if ( $latchon == 1 )
  {
    $_=fixup_configure_lines($_);
    push @machopts, $_;
  }
  for $paropt ( @platforms )
  {
    if ( substr( $_, 0, 5 ) eq "#ARCH" && $latchon == 0
          && ( index( $_, $sw{os} ) >= 0 ) && ( index( $_, $sw{mach} ) >= 0 )
          && ( index($_, $paropt) >= 0 ) )
    {

    $x=substr($_,6) ;
    $x=~s/^[     ]*// ;
    $x =~ s/#.*$//g ;
    chomp($x) ;
    $x = $x." (".$paropt.")" ;
    if ( $x eq $optstr[$optchoice] )
    {
      $latchon = 1 ;
      $sw{ompparallel} = "" ;
      $sw{dmparallel} = "" ;
      $validresponse = 0 ;

      if ( $paropt eq 'dmpar' ) 
      {
        $sw{dmparallel} = "RSL_LITE" ;
        $sw{dmparallelflag} = "-DDM_PARALLEL" ;
	$sw{fc} = "\$(DM_FC)" ;
	$sw{f90} = "\$(DM_F90)" ;
	$sw{cc} = "\$(DM_CC)" ;
      }
    }
    }

  }
}
close CONFIGURE_DEFAULTS ;


open CONFIGURE_TRK, "> configure.trk" or die "cannot append configure.trk" ;
open ARCH_PREAMBLE, "< arch/preamble" or die "cannot open arch/preamble" ;
my @preamble;
# apply substitutions to the preamble...
while ( defined($_=<ARCH_PREAMBLE>) ) {
    $_=fixup_configure_lines($_);
    print CONFIGURE_TRK $_;
}
close ARCH_PREAMBLE ;
printf CONFIGURE_TRK "# Settings for %s", $optstr[$optchoice] ;
print CONFIGURE_TRK @machopts  ;
open ARCH_POSTAMBLE, "< arch/postamble" or die "cannot open arch/postamble" ;
while ( defined($_=<ARCH_POSTAMBLE>) ) {
    $_=fixup_configure_lines($_);
    print CONFIGURE_TRK $_
}
close ARCH_POSTAMBLE ;
close CONFIGURE_TRK ;

printf "Configuration successful. To build the Vortex Tracker, type: compile \n" ;
printf "------------------------------------------------------------------------\n" ;


