#!/usr/bin/env perl

# Configuration script for the TEclass 2.0 tool
# Gyorgy Abrusan, 1st Feb 2010 

use warnings;
use Cwd;
use Getopt::Std;
use File::Find

getopts('h');
our ($opt_h);


# Pints help
if ($opt_h) {
    print STDERR "\n"
       . "Usage:\n" 
       . " ./Configure.pl \n\n";
    exit;
}


my $current_dir = getcwd();
my $exit = 0;

# Opens the configuration file
open (CONFIG, ">", "Configuration.pm") or die "can't open CONFIG $!";
print CONFIG  "#!/usr/bin/env perl\n\n"
            . "package Configuration;\n"
            . "require Exporter;\n\n"
            . "use warnings;\n\n"
            . 'our @ISA    = qw / Exporter /;' . "\n"
            . 'our @EXPORT = qw / $path_to_TEclass' . "\n"
            . '                   $path_to_classifiers' . "\n"            
            . '                   $path_to_svm' . "\n"         
            . '                   $path_to_scripts' . "\n"                 
            . '                   $path_to_glimmer' . "\n"
            . '                   $path_to_librf' . "\n"
            . '                   $path_to_LVQ' . "\n"   
            . '                   $path_to_blastclust' . "\n"     
            . '                   $path_to_RepBase' . "\n"
            . '                   $path_to_RepeatMaskerLib /;' . "\n"            
            . "\n";

print CONFIG '$path_to_TEclass     = "', $current_dir, '";', "\n"; 

# Default classifier dir
if (! -d "./classifiers" ) {
    `mkdir classifiers`;
}
print CONFIG '$path_to_classifiers = "', $current_dir, '/classifiers";', "\n";

# Locates the libsvm folder
my $path_to_svm = '';   
find(sub { if (m/svm.cpp/) { $path_to_svm = getcwd(), "\n"; } }, '.');
# libsvm
if (-s "$path_to_svm/svm-predict") {
    print CONFIG '$path_to_svm         = "', $path_to_svm, '";', "\n"; 
    print CONFIG '$path_to_scripts     = "', $path_to_svm, '/tools";', "\n";        
}
else {
    print CONFIG '$path_to_svm         = "";', "\n"; 
    print CONFIG '$path_to_scripts     = path_to_svm/tools;', "\n"; 
    print STDERR "libsvm was not found.\n";
    $exit = 1;
}
    
# Glimmer
if (-s "$current_dir/glimmer3.02/bin/build-icm") {
    print CONFIG '$path_to_glimmer     = "', $current_dir, '/glimmer3.02/bin";', "\n";
}
else {
    print CONFIG '$path_to_glimmer     = "";', "\n";
    print STDERR "Glimmer was not found.\n";
    $exit = 1;
}

# librf
if (-s "$current_dir/librf/examples/rftrain") { 
    print CONFIG '$path_to_librf       = "', $current_dir, '/librf/examples";', "\n";
}
else {
    print CONFIG '$path_to_librf       = "";', "\n";
    print STDERR "librf was not found.\n";
    $exit = 1;
}
 
# LVQ
if (-s "$current_dir/lvq_pak-3.1/classify") { 
    print CONFIG '$path_to_LVQ         = "', $current_dir, '/lvq_pak-3.1";', "\n";
}
else {
    print CONFIG '$path_to_LVQ         = "";', "\n";
    print STDERR "LVQ was not found. \n";
    $exit = 1;    
}

# Blastclust
if (-s "$current_dir/blast-2.2.26/bin/blastclust") {
    print CONFIG '$path_to_blastclust  = "', $current_dir, '/blast-2.2.26/bin";', "\n\n";          
}
elsif ( -s "/cygwin/c/Program\ Files/blast-2.2.26/bin/blastclust.exe") {
    print CONFIG '$path_to_blastclust  = "/cygwin/c/Program Files/blast-2.2.26/bin";', "\n\n";
}
elsif ( -s "/cygwin64/c/Program\ Files/blast-2.2.26/bin/blastclust.exe") {
    print CONFIG '$path_to_blastclust  = "/cygwin64/c/Program Files/blast-2.2.26/bin";', "\n\n";
}
else {
    print CONFIG '$path_to_blastclust  = "";', "\n\n";
    print STDERR "Blastclust was not found.\n";
    $exit = 1;
}


# GIRI version of RepBase
print STDERR "\nPlease specify the path to RepBase, EMBL version :\n",
             "(press enter if you want to specify it later)\n";
print CONFIG '$path_to_RepBase = "', set_path(1), '";', "\n";

print STDERR "\nPlease specify the path to RepeatMaskerLib.embl :\n",
             "(press enter if you want to specify it later)\n";
print CONFIG '$path_to_RepeatMaskerLib = "', set_path(2), '";', "\n\n";    
 

print CONFIG "1;"; 

close CONFIG; 


#---------------------- Configuring the TEclasc scripts  ------------------------ 

# Sets  "use lib" in scripts.pl
$from = 'use lib ';
$to   = 'use lib "' . $current_dir . '"; #';  
`perl -p -i -e 's{$from}{$to}' TEclassTest.pl`;
`perl -p -i -e 's{$from}{$to}' TEclassBuild.pl`;
`perl -p -i -e 's{$from}{$to}' Build_ICM_model.pl`;
`perl -p -i -e 's{$from}{$to}' Build_LVQ_sets.pl`;
`perl -p -i -e 's{$from}{$to}' Build_Random_Forests.pl`;
`perl -p -i -e 's{$from}{$to}' Build_SVM_models.pl`;
`perl -p -i -e 's{$from}{$to}' Preprocessing.pl`;
unlink <*.bak>;
 
system("chmod +x *.pl");
print STDERR "\nConfiguration ready.\n",
         "You can modify the paths manually in the Configuration.pm module.\n";

if ($exit == 1) {
    print STDERR "\nSome executables were not found, please set their paths\n",
                 "manually in the Configuration.pm file\n";
}


#--------------------------------- Subs ----------------------------------------

sub set_path {
    my $lib = $_[0]; 
    my $path;       
    while (1) {
        $path = <>;
        chomp $path;
        if ($path =~ m/\// || $path =~ m/\w/) {
            if ($path !~ /^\//) {
                print STDERR "Please provide the full path.\n";
                next;
            }
            elsif ($lib == 1 && ! -s "$path/humrep.ref") {
                print STDERR "Repbase was not found, please provide the correct path.\n";
                next; 
            }
            elsif ($lib == 2 && ! -s "$path/RepeatMaskerLib.embl") {
                print STDERR "RepeatMaskerLib.embl was not found, please provide the correct path.\n";
                next; 
            }            
            last;    
        }
        last;    
    }
    return $path;
}

