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

##########################################################################################
#                                                                                        #
#  Project     :  HOMOLpro                                                               #
#  Release     :  1.1                                                                    #
#                                                                                        #
#  File        :  install.pl                                                             #
#  Description :  HOMOLpro 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/pdb_full")){ 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/blastall"){ 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 HOMOLpro bash profile
print "Installing bash profile...\n"; open(IN,"$install_dir/lib/install_template1.sh");
open(OUT,">$install_dir/env/HOMOLpro.sh"); while(my $l=<IN>){ chomp($l); if($l eq "<INSTALL_DIR>"){
print OUT "export HOMOLpro_ROOT_DIR=\"$install_dir\"\n"; } elsif($l eq "<TOOLS_DIR>"){
print OUT "export HOMOLpro_TOOLS_DIR=\"$scratch1D_tools\"\n"; } elsif($l eq "<BLAST_INSTALL_DIR>"){
print OUT "export HOMOLpro_BLAST_INSTALL_DIR=\"$blast_install_dir\"\n"; }
else{ print OUT "$l\n"; } } close(IN); close(OUT); #`chmod 777 $install_dir/env/HOMOLpro.sh`;

# Preparing HOMOLpro launcher
print "Installing HOMOLpro scripts...\n"; open(IN,"$install_dir/lib/install_template2.sh");
open(OUT,">$install_dir/bin/add_homology_predictions.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/add_homology_predictions.sh`;

# Print message to test installation
print "Done installing HOMOLpro...\n\nTo test the installation:\n\n";
print "Change directory to the 'doc' sub-folder\n\n   cd doc\n\n";
print "Run HOMOLpro on the provided test datasets:\n\n";
print "   ../bin/add_homology_predictions.sh test.fa ss,acc ";
print "test.ss.ab,test.acc.ab test.ss,test.acc\n\n";
print "The output files 'test.ss' and 'test.acc' should be identical ";
print "to 'test.ss.hom' and 'test.acc.hom' respectively\n\n\n\n";

