#!/usr/bin/perl
use strict;
#use Env;
BEGIN{ @INC = ( 'SOFTWARE_PATH/tools/SCRATCH-1D_1.1/perl_lib/', @INC ); }
use Env;

##########################################################################################
#                                                                                        #
#  Project     :  PROFILpro                                                              #
#  Release     :  1.1                                                                    #
#                                                                                        #
#  File        :  install.pl                                                             #
#  Description :  PROFILpro installation script                                          #
#                                                                                        #
#  Author(s)   :  Christophe Magnan (cmagnan@ics.uci.edu)                                #
#  Copyright   :  Institute for Genomics and Bioinformatics                              #
#                 University of California, Irvine                                       #
#                                                                                        #
#  Modified    :  2015/07/02                                                             #
#                                                                                        #
##########################################################################################

# SCRATCH-1D Specific Settings
my $scratch1D_tools="$ARGV[0]";
my $blast_install_dir="$ARGV[1]";

# Retrieving installation path
my $install_dir=`pwd`; chomp($install_dir);
my $locerr="\nThis script must run in its own directory.\n";
if((! -d "$install_dir/bin")||(! -d "$install_dir/env")){ print "$locerr\n"; exit 1; }
if((! -d "$install_dir/data")||(! -d "$install_dir/data/uniref50")){ print "$locerr\n"; exit 1; }
if((! -d "$install_dir/doc")||(! -d "$install_dir/lib")){ print "$locerr\n"; exit 1; }
if((! -d "$install_dir/tmp")||(! -d "$scratch1D_tools")){ print "$locerr\n"; exit 1; }
if(! -f "$blast_install_dir/bin/blastpgp"){ print "$locerr\n"; exit 1; }
if(! -f "$install_dir/lib/install_template1.sh"){ print "$locerr\n"; exit 1; }
if(! -f "$install_dir/lib/install_template2.sh"){ print "$locerr\n"; exit 1; }
print "\n\n\nInstallation folder: $install_dir\n";

# Preparing PROFILpro bash profile
print "Installing bash profile...\n"; open(IN,"$install_dir/lib/install_template1.sh");
open(OUT,">$install_dir/env/PROFILpro.sh"); while(my $l=<IN>){ chomp($l); if($l eq "<INSTALL_DIR>"){
print OUT "export PROFILpro_ROOT_DIR=\"$install_dir\"\n"; } elsif($l eq "<TOOLS_DIR>"){
print OUT "export PROFILpro_TOOLS_DIR=\"$scratch1D_tools\"\n"; } elsif($l eq "<BLAST_INSTALL_DIR>"){
print OUT "export PROFILpro_BLAST_INSTALL_DIR=\"$blast_install_dir\"\n"; }
else{ print OUT "$l\n"; } } close(IN); close(OUT); #`chmod 777 $install_dir/env/PROFILpro.sh`;

# Preparing PROFILpro launcher
print "Installing PROFILpro scripts...\n"; open(IN,"$install_dir/lib/install_template2.sh");
open(OUT,">$install_dir/bin/generate_profiles.sh"); while(my $l=<IN>){ chomp($l);
if($l eq "<INSTALL_DIR>"){ print OUT "install_dir=\"$install_dir\"\n"; } else{
print OUT "$l\n"; } } close(IN); close(OUT); #`chmod 777 $install_dir/bin/generate_profiles.sh`;

# Print message to test installation
print "Done installing PROFILpro...\n\nTo test the installation:\n\n";
print "Change directory to the 'doc' sub-folder\n\n   cd doc\n\n";
print "Run PROFILpro on the provided test dataset:\n\n";
print "  ../bin/generate_profiles.sh test.fasta test.out 3\n\n";
print "The output file 'test.out' should be identical to 'test.profil'\n\n\n\n";

