#!/bin/csh -f
# 
# Ludi_Run
#    Script for running the inhibitor background job.
#
# Command line input:
#    argv[1] = control file
#    argv[2] = log file
#    argv[3] = receptor or active-analogs pdb file
#    argv[4] = run name
#    argv[5] = active analog mode flag (T or F)
#    argv[6] = link mode flag (T or F)
#    argv[7] = ligand pdb file
#    argv[8] = fragment library location file
#
# Environment: 
#    If the image is running as a remote background job, the following 
#    environment variable will be set:
#	
#    BIOSYM_REMOTE "local_host local_dir username status_file scatch_dir"
# 
# Product: ludi
#
#$Log:	Ludi_Run,v $
#	Revision 1.1  1999/02/01  15:28:58  qbuild
#	Initial revision
#	
#	Revision 1.1  1999/01/28  14:49:59  agiammon
#	Initial revision
#	
# Revision 960.5  1997/05/09  21:30:45  agiammon
# allow a local version of inhibitor to be used when running
# in the regression testing system.
# 
#Revision 960.4  1997/03/03 22:33:37  agiammon
#Library type is now determined within inhibitor.
#
# Revision 960.3  1996/12/05  19:28:09  agiammon
# ludi_genfra project.
#
# Revision 960.2  1996/04/25  21:57:20  agiammon
# Bug fix 24857/25589 make "which" command more robust.
#
# Revision 960.1  1996/04/17  18:17:07  agiammon
# @>previous_revision:pathname:ludi/v950/bin/Ludi_Run:revision:950.16: Initial entry for new version
#
# Revision 950.16  1995/07/14  07:31:51  agiammon
# add diagnostic return values for missing library files.
#
# Revision 950.15  1995/06/04  20:13:48  agiammon
# bugfix 22916.
#
# Revision 950.14  1995/02/24  22:53:05  stroz
# Got rid of \here\ file usage to simplify things and also to get this script to
# run on AIX 4.1.1
#
# Revision 950.13  1995/02/03  01:37:22  stroz
# Made file checking more robust.
#
# Revision 950.12  1995/02/01  22:29:39  stroz
# Got rid of verbose and echo flags.
#
# Revision 950.11  1995/01/26  20:35:37  stroz
# Made fil existance checking even more robust.
#
# Revision 950.10  1995/01/25  01:20:55  stroz
# Made more robust by checking for file existence. If files are not found a
# non-zero exit is made.
#
# Revision 950.9  1995/01/16  23:40:01  agiammon
# bugfix 21468
#
# Revision 950.8  1995/01/11  20:31:07  lifeadm
# (marvin for agiammon):Fix bug 21372 in which library formats are misidentified.
# ( username: marvin  machine: iris51 )
#
# Revision 950.7  1995/01/05  17:26:08  lifeadm
# (marvin for agiammon):Bug fixes 20874 and 21002:  modifies handling of remote
# jobs and library filename expansion.
# ( username: marvin  machine: iris51 )
#
# Revision 950.6  1994/12/15  00:26:11  agiammon
# turn off diagnostic output.
#
# Revision 950.5  1994/12/14  04:17:25  agiammon
# Fix remote job execution when there is no NFS (network file system).
# bug 20874.
#
# Revision 950.4  1994/10/18  19:02:22  agiammon
# rename unexpected files with the ludi_data extension.
#
# Revision 950.3  1994/10/12  21:58:29  agiammon
# New method for renaming frag files.
# This is made necessary by the new inhibitor feature
# which deletes duplicate fragment hits.
#
# Revision 950.2  1994/10/04  17:21:37  agiammon
# Don't delete receptor and ligand pdb files when run completes.
# Receptor file is now much smaller since only search sphere atoms written out.
#
# Revision 950.1  1994/09/28  01:23:45  agiammon
# @>previous_revision:pathname:ludi/v230/bin/Ludi_Run:revision:230.4: Initial entry for new version
#
# Revision 230.4  1993/06/18  20:05:54  agiammon
# add comments
#
# Revision 230.3  93/06/15  16:36:49  agiammon
# Fix remote job execution that was broken by re-implementation of 
# background job facility.
# 
#
# Biosym Technologies, Inc. 1993
#-------------------------------------------------------------------------

set control       = $argv[1]
set log           = $argv[2]
set struct_input  = $argv[3]
set run_name      = $argv[4]
set active_analog = $argv[5]
set link          = $argv[6]
set ligand_inp    = $argv[7]
set env_file      = $argv[8]
set PLATFORM	  = `uname -a | awk '{print $1}'`
set PROG = $0

set send_input_files   = "$run_name/$control $run_name/$struct_input $run_name/$env_file"
set remove_input_files = "$control $struct_input $env_file"

if ($link == T) then
   set send_input_files = "$send_input_files $run_name/$ligand_inp"
   set remove_input_files = "$remove_input_files $ligand_inp" 
endif

#
# The following is done to detect the presence 
# of a development version of inhibitor before 
# moving into the run directory
#
set platform = `uname -a | /bin/awk '{print $1}'`
unalias which
if ($platform == AIX) then
  set command = `which inhibitor`
  set found = $status
else
  set command = `which -f inhibitor`
  set found = $status
endif
if ($found != 0) then
   echo "inhibitor executable not found."
   echo "Check your environment for proper set up for Ludi."
   echo "inhibitor executable not found." >> $run_name/$run_name.ludi_status
   echo "Check your environment for proper set up for Ludi." >> $run_name/$run_name.ludi_status
   set e_status = 1
   goto done
endif

if ($?BIOSYM_PLATFORM) then
  if ($BIOSYM_PLATFORM == $command:gh) then
    set command = `pwd`/$command
  endif
else
  if ($command:gh == ".") then
    set command = ../$command:gt
  endif
endif

# If this is a remote background job, do the neccessary setup.

if ($?BIOSYM_REMOTE) then

    biosym_remote_init -f $send_input_files
    set init_stat = $status
    if ($init_stat != 0) then 
	set e_status = init_stat
	goto done
    endif

    set rem_params = ($BIOSYM_REMOTE)
    cd $rem_params[5]

endif 

#
# Move into the run directory 
# This only happens for local jobs and for remote jobs under NFS
#
if (-e $run_name) then
   set cd_to_run_dir = 1
   cd $run_name
else
   set cd_to_run_dir = 0
endif

#
#  Link to the Ludi library
#
source $env_file
if ($status != 0) then
   echo "Can't find Ludi library."
   exit 10
endif

if (! -r ${lib_struct}) then
    echo "${PROG}: Unable to open <${lib_struct}>"
    if ($link == T) then
       exit 11
    else
       exit 12
    endif
endif

if (! -r ${lib_inp}) then
    echo "${PROG}: Unable to open <${lib_inp}>"
    if ($link == T) then
       exit 11
    else
       exit 12
    endif
endif

ln -s $lib_struct fort.9
ln -s $lib_inp fort.4

#
# Link to the receptor or active analogs structure
#
if ($active_analog == T) then
   if (-r ${struct_input}) then
      ln -s $struct_input fort.3
   else
      echo "${PROG}: Unable to open <${struct_input}>"
      exit -1
   endif
else if ($active_analog == F) then
   if (-r ${struct_input}) then
      ln -s $struct_input fort.10
   else
      echo "${PROG}: Unable to open <${struct_input}>"
      exit -1
   endif
endif

#
# Link to the ligand structure
#
if ($link == T) then
   if (-r ${ligand_inp}) then
      ln -s $ligand_inp fort.7
   else
      echo "${PROG}: Unable to open <${ligand_inp}>"
      exit -1
   endif
endif

# Run the executable and save its exit status.

$command <$control >$log
set e_status = $status

#
# Clean up 
#
/bin/rm -f fort.4
/bin/rm -f fort.9

if ($active_analog == T) then
  /bin/rm -f fort.3
else if ($active_analog == F) then
  /bin/rm -f fort.10
endif

if ($link == T) then
   /bin/rm -f fort.7
endif

# The following lines remove the protein pdb file and 
# any inhib pdb file
#/bin/rm $struct_input
#if ($link == T) then
#   /bin/rm -f $ligand_inp
#endif

#
# Rename fort.* files with names expected in Insight
#
if (-e fort.8) then
   mv fort.8 $run_name.ludi_targ_sites
endif

if (-e fort.11) then
   mv fort.11 $run_name.ludi_int_sites
endif

if (-e fort.12) then 
   mv fort.12 $run_name.ludi_pseudo_protein
endif

if (-e fort.46) then 
   mv fort.46 $run_name.ludi_status
endif

if (-e fort.47) then
   mv fort.47 $run_name.ludi_scores
endif

if (-e fort.48) then
   mv fort.48 $run_name.ludi_sum1
endif

if (-e fort.49) then
   unalias cat
   cat fort.49 >> $run_name.ludi_status
   mv fort.49 $run_name.ludi_err
endif

if (-e fort.50) then
   mv fort.50 $run_name.ludi_sum2
endif

# The next section renames the frag files.
# The frag file unit numbering starts at 51
# and goes up but not consecutively.
# Unexpected files are renamed with the ludi_data ext.
set tmp_awk = ${run_name}_tmp.awk

echo '{'							> ${tmp_awk}
echo 'arr_size = split ($1, arr, ".")' 				>> ${tmp_awk}
echo 'nb = arr[arr_size] - 50' 					>> ${tmp_awk}
echo 'nb_str = nb " "' 						>> ${tmp_awk}
echo 'if      (nb < 1)   ext = ".ludi_data" arr[arr_size]' 	>> ${tmp_awk}
echo 'if      (nb < 10)  ext = ".ludi_frag00" nb_str' 		>> ${tmp_awk}
echo 'else if (nb < 100) ext = ".ludi_frag0"  nb_str' 		>> ${tmp_awk}
echo 'else               ext = ".ludi_frag"   nb_str' 		>> ${tmp_awk}

#-------------------------------------------------------------#
# Do this check 'cause AIX doesn't want the new line escaped. #
#-------------------------------------------------------------#

if (${PLATFORM} == AIX) then
    echo 'printf("mv %s %s%s\n", $1, "'${run_name}'", ext)' 	>> ${tmp_awk}
else
    echo 'printf("mv %s %s%s\\n", $1, "'${run_name}'", ext)' 	>> ${tmp_awk}
endif

echo '}' 							>> ${tmp_awk}

(/bin/ls fort.* | /bin/awk -f ${tmp_awk} > $run_name.tmp)  >& /dev/null

/bin/chmod 700 $run_name.tmp
./$run_name.tmp
/bin/rm -f ${run_name}.tmp
/bin/rm -f ${tmp_awk}

if ($cd_to_run_dir == 1) then
   cd ../	  
endif

# If this is a remote background job, do the neccessary cleanup.

if ($?BIOSYM_REMOTE) then

    set remote_params = ($BIOSYM_REMOTE)
    set l_host  = $remote_params[1]
    set l_dir   = $remote_params[2]
    set run_dir = $remote_params[2]/$run_name
    set user    = $remote_params[3]
    set s_file  = $remote_params[4]
    set scr_dir = $remote_params[5]

# Update $BIOSYM_REMOTE to take the run directory into account

    setenv BIOSYM_REMOTE "$l_host $run_dir $user $s_file $scr_dir"

    cd ~
    biosym_remote_end $e_status -rm $remove_input_files
    set e_status = $status

# The following is needed for remote jobs without NFS

    rsh $l_host -n "if (-e $run_dir/fort.46) /bin/rm $run_dir/fort.46; if (-e $run_dir/$s_file) /bin/mv $run_dir/$s_file $l_dir/$s_file"


# reset BIOSYM_REMOTE to its original state

    setenv BIOSYM_REMOTE "$l_host $l_dir $user $s_file $scr_dir"

endif

# Exit with the appropriate status

done:
 exit($e_status)
