#!/usr/bin/env perl
##---------------------------------------------------------------------------##
##  File:
##      @(#) configure
##  Author:
##      Robert Hubley <rhubley@systemsbiology.org>
##  Description:
##      A configuration utility for the RepeatMasker distribution.
##
#******************************************************************************
#* Copyright (C) Institute for Systems Biology 2003-2004 Developed by 
#* Arian Smit and Robert Hubley.
#*
#* This work is licensed under the Open Source License v2.1.  To view a copy
#* of this license, visit http://www.opensource.org/licenses/osl-2.1.php or
#* see the license.txt file contained in this distribution.
#*
#******************************************************************************
#
# ChangeLog
#
#   $Log: configure,v $
#   Revision 1.12  2004/09/09 22:31:58  rhubley
#    Mostly license changes.
#
#   Revision 1.11  2004/08/12 23:20:27  rhubley
#    - Modifications for use with CSV format
#
#   Revision 1.10  2004/08/04 23:55:50  rhubley
#     - Updated configure
#
#   Revision 1.9  2004/08/04 23:36:18  rhubley
#     - Several important changes:
#          1. A major bug was found by Warren Gish.  The CrossmatchSearchEngine
#             was not able to detect that crossmatch had run out of memory.
#             This is now fixed....also fixed WUBlastSearchEngine.
#          2. Warren located a csh syntax for redirection in the setdb
#             system call.  System calls always use bourne shell syntax.
#          3. Taxonomy was built using a newer version of Freeze than
#             most people use.
#
#   Revision 1.8  2004/07/15 04:28:06  rhubley
#     - Working toward a bug fix
#
#   Revision 1.7  2004/06/30 17:10:16  rhubley
#    - Some doc changes
#
#
#
###############################################################################
#
# To Do:
#

=head1 NAME

configure - Configure the RepeatMasker distribution

=head1 SYNOPSIS

  perl ./configure 

=head1 DESCRIPTION

  An installation script for the RepeatMasker distribution

=head1 SEE ALSO

=over 4

RepeatMasker

=back

=head1 COPYRIGHT

Copyright 2004 Robert Hubley, Institute for Systems Biology

=head1 AUTHOR

Robert Hubley <rhubley@systemsbiology.org>

=cut

#
# Module Dependence
#
use strict;
use FindBin;
use lib $FindBin::Bin;

##
## Check for RepeatMasker dependencies
##
BEGIN {
my @modDependencies = (
  "Tie::File",  "Getopt::Long", "POSIX", "File::Copy",
  "File::Path", "Data::Dumper", "Cwd"
);

my @missingModules = ();
foreach my $module (@modDependencies) {
  unless ( eval "require " . $module . ";" ) {
    push @missingModules, $module;
  }
}
if (@missingModules) {
  print "\nThe following perl modules required by RepeatMasker "
    . "are missing from\nyour system.  Please install these or "
    . "or upgrade your perl version\nto 5.8.x first:\n    "
    . join( "\n    ", @missingModules ) . "\n";
  exit;
}
}

##
## Check for perl version
##
if ( $] && $] < 5.008 ) {
  system("clear");
  print "RepeatMasker should be used with perl 5.008 or higher.\n"
        . "Perl $] is being used to run configure.";
  print "\n\n\n<PRESS ENTER TO CONTINUE>\n";
  my $answer = <STDIN>;
}


##
## Introduce ourselves
##
system("clear");
print "\n\n\nRepeatMasker Configuration Program\n\n";
print "This program assists with the configuration of the\n";
print "RepeatMasker program.  The next set of screens will ask\n";
print "you to enter information pertaining to your system\n";
print "configuration.  At the end of the program your RepeatMasker\n";
print "installation will be ready to use.\n";
print "\n\n\n<PRESS ENTER TO CONTINUE>\n";
my $answer = <STDIN>;



##
## Perl interpreter location
##
my $goodParam;
my $perlLocation = $^X;
do { 
  $goodParam = 1;
  $perlLocation = &promptScreen(
  "**PERL INSTALLATION PATH**\n\n  This is the full path to the Perl interpreter.\n  ie. /usr/local/bin/perl\n",
  "Enter path", $perlLocation );
  if ( ! ($perlLocation =~ /^.*perl$/) ) {
    $perlLocation .= "/" if ( ! ($perlLocation =~ /^.*\/$/) );
    $perlLocation .= "perl";
  }
  if ( ! -f $perlLocation || 
       ! -x $perlLocation ) {
    print "Perl not found at \"$perlLocation\"\n";
    print "<PRESS ENTER TO CONTINUE>\n";
    $answer = <STDIN>;
    $goodParam = 0;
  }
}while ( $goodParam != 1 );

##
## RepeatMasker location
##
my $rmLocation = "$FindBin::Bin";
$rmLocation = &promptScreen(
"**REPEATMASKER INSTALLATION PATH**\n\n  This is the path to the location where\n  the RepeatMasker program has been installed.\n",
  "Enter path",
  $rmLocation
);

##
## Cross_match location
##
my $cmLocation = "/usr/local/bin/cross_match";
do {
  $goodParam  = 1;
  $cmLocation = &promptScreen(
"**CROSS_MATCH INSTALLATION PATH**\n\n  This is the path to the location where\n  the cross_match program has been installed.\n",
    "Enter path",
    $cmLocation
  );
  if ( ! ($cmLocation =~ /^.*cross_match$/) ) {
    $cmLocation .= "/" if ( ! ($cmLocation =~ /^.*\/$/) );
    $cmLocation .= "cross_match";
  }
  if ( !-x $cmLocation ||
       !-f $cmLocation ) {
    print "$cmLocation not found.\n";
    print "<PRESS ENTER TO CONTINUE>\n";
    $answer = <STDIN>;
    $goodParam = 0;
  }
} while ( $goodParam != 1 );

##
## WUBlast Location
##
my $wuLocation;
do {
  $goodParam  = 1;
  $wuLocation = &promptScreen(
"**WUBLAST BLASTP INSTALLATION PATH**\n\n  Optional.  This is the path to the location where\n  the wublast blastp and setdb programs can be found.\n",
    "Enter path",
    ""
  );
  if ( $wuLocation ne "" ) {
    unless ( -d "$wuLocation" ) {
      print "$wuLocation does not exist\n";
      $goodParam = 0;
    }
    unless ( -s "$wuLocation/blastp" ) {
      print "$wuLocation/blastp does not exist\n";
      $goodParam = 0;
    }
    unless ( -s "$wuLocation/setdb" ) {
      print "$wuLocation/setdb does not exist\n";
      $goodParam = 0;
    }
    if ( $goodParam == 0 ) {
      print "<PRESS ENTER TO CONTINUE>\n";
      my $answer = <STDIN>;
    }
  }
} while ( $wuLocation ne "" && $goodParam == 0 );
$wuLocation =~ s/(.*)\/$/$1/;

##
## Alter perl invocation headers
##
print " -- Setting perl interpreter...\n";
my @progFiles = ( "RepeatMasker", "DateRepeats", "ProcessRepeats",
                  "util/queryRepeatDatabase.pl", 
                  "util/queryTaxonomyDatabase.pl" );
my $perlLocEsc = $perlLocation;
$perlLocEsc =~ s/\//\\\//g;
foreach my $file (@progFiles) {
  system(
"$perlLocation -i -0pe \'s/^#\\!.*perl.*/#\\!$perlLocEsc/g\;' $file"
  );
}

##
## Alter program parameters
##
print " -- Setting RepeatMasker parameters...\n";
my $cmLocEsc = $cmLocation;
$cmLocEsc =~ s/\//\\\//g;
system(
"$perlLocation -i -0pe \'s/my\\s+\\\$crossmatch\\s*=\\s*\\S+/my \\\$crossmatch = \"$cmLocEsc\";/\;' RepeatMasker"
);

if ( $wuLocation ne "" ) {
  my $wuLocEsc = $wuLocation;
  $wuLocEsc =~ s/\//\\\//g;
  system(
"$perlLocation -i -0pe \'s/my\\s+\\\$WUBLAST\\s*=\\s*\\S+/my \\\$WUBLAST = \"$wuLocEsc\\\/blastp\";/\;' RepeatMasker"
  );
  system(
"$perlLocation -i -0pe \'s/my\\s+\\\$SETDB\\s*=\\s*\\S+/my \\\$SETDB = \"$wuLocEsc\\\/setdb\";/\;' RepeatMasker"
  );
}

print "\nCongratulations!  RepeatMasker is now ready to use.\n";
print "Simply place $rmLocation in your user's path and review\n";
print "the RepeatMasker documentation here: $rmLocation/repeatmasker.help\n";

####################### S U B R O U T I N E S ##############################

##-------------------------------------------------------------------------##

=over 4
                                                                                
=item Use: my $retVal =  &promptScreen( $screenText, $promptText, 
                                        $defaultAnswer, [$validResponses] );
                                                                                
Clear the screen and prompt the user for input
                                                                                
=back
                                                                                
=cut

##-------------------------------------------------------------------------##
sub promptScreen {
  my $screenText     = shift;
  my $promptText     = shift;
  my $defaultAnswer  = shift;
  my $validResponses = shift;

  my $answer = undef;

  # Clear the screen
  system("clear");

  print "\n\n\n";
  print $screenText;
  my $numLines = ( $screenText =~ s/(\n)/$1/g );
  for ( my $i = 0 ; $i < 15 - $numLines ; $i++ ) {
    print "\n";
  }
  if ( defined $validResponses ) {
    $promptText .= " ( " . $validResponses . " )";
  }
  if ( defined $defaultAnswer ) {
    $promptText .= " [ " . $defaultAnswer . " ]: ";
  }
  print $promptText;
  $answer = <STDIN>;

  if ( $answer =~ /^\s*$/ ) {
    $answer = $defaultAnswer;
  }
  else {
    $answer =~ s/[\n\r]+//g;
  }

  return ($answer);
}

1;
