#!/bin/csh -f
# 
# Ludi_Add_Fragment
#    Script for running the genfra background job.
#
# Command line input:
#    argv[1] = fragment label
#    argv[2] = fragment file - pdb format
#    argv[3] = fragment file - sd format
#    argv[4] = control file
#    argv[5] = library location file
#    argv[6] = output file
#    argv[7] = run name
#    argv[8] = new_library flag
#
# 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_Add_Fragment,v $
#	Revision 1.3  1999/02/24  10:43:38  agiammon
#	Submission Number: 9463
#	fix bug found by qa
#	For genfra 5.0 use auto-append mode
#	rather than the append commands in this script.
#	
#	Revision 1.2  1999/02/23  15:37:48  agiammon
#	Submission Number: 9463
#	Fix bug found by QA
#	LUDI_APPEND undefined.
#	
#	Revision 1.1  1999/02/01  15:28:38  qbuild
#	Initial revision
#	
#	Revision 1.1  1999/01/28  14:51:15  agiammon
#	Initial revision
#	
#Revision 980.4  1998/11/05 00:35:11  agiammon
#Fix error reporting when Add_Fragment fails.
#
#Revision 980.3  1998/10/27 19:13:39  agiammon
#initialize LUDI_IREAD
#
#Revision 980.2  1998/10/21 22:56:49  agiammon
#bugfix 98272t1hq03  - can't append to binary libraries.
#
#Revision 960.12  1997/08/29 16:30:10  agiammon
#Bugfix 97148a1gq02 - Need user error when add_fragment to old lib with new link specs
#
#Revision 960.11  1997/06/03 23:44:01  agiammon
#Turn off debugging flags.
#
#Revision 960.10  1997/03/14 19:42:01  agiammon
#Recognize new lib formats and handle them appropriately.
#
# Revision 960.9  1997/03/13  16:19:32  agiammon
# Fix bug in determining which version of Genfra to use
# when adding to existing lib.
#
# Revision 960.8  1997/01/21  21:38:20  agiammon
# lower and upper case labels are unique.
#
# Revision 960.7  1997/01/16  22:34:55  agiammon
# bugfix for back compat with genfra v4.4.
#
# Revision 960.6  1997/01/16  17:56:06  agiammon
# bugfix.
#
# Revision 960.4  1996/12/04  22:55:34  agiammon
# fix logical error that causes header line to be repeated
# throughout the targets file.
#
# Revision 960.2  1996/11/21  00:20:29  agiammon
# ludi_genfra project.
#
# Revision 960.1  1996/10/14  23:09:03  agiammon
# @>previous_revision:pathname:ludi/v950/bin/Ludi_Add_Fragment:revision:950.3: Initial entry for new version
#
# Revision 950.3  1995/01/11  20:31:57  lifeadm
# (marvin for agiammon):Fix bug 21372 in which library formats are misidentified.
# ( username: marvin  machine: iris51 )
#
# Revision 950.2  1995/01/05  17:25:37  lifeadm
# (marvin for agiammon):Bug fixes 20874 and 21002:  modifies handling of 
# remote jobs and library filename expansion.
# ( username: marvin  machine: iris51 )
#
# Revision 950.1  1994/11/03  06:44:36  agiammon
# Initial implementation of the Add_Fragment Ludi command.
#
#
#
# Biosym Technologies, Inc. 1993
#-------------------------------------------------------------------------

set input_files   = "$argv[2] $argv[3] $argv[4] $argv[5]"
set frag_label = $argv[1]
set pdb_entry = $argv[2]
set sd_entry = $argv[3]
set control = $argv[4]
set env_file = $argv[5]
set out_file = $argv[6]
set run_name = $argv[7]
set new_lib = $argv[8]

set host = `hostname`

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

if ($?BIOSYM_REMOTE) then

    biosym_remote_init -f $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 

source $env_file

if ($new_lib == "NEW") then
  if (-e $lib_struct) then
    echo "" >>! $out_file
    echo "$lib_struct already exists." >> $out_file
    echo "Please turn the New_Library parameter OFF if you wish to append." >> $out_file
    set e_status = 1
    goto done
  endif

  if (-e $lib_inp) then
    echo "" >>! $out_file
    echo "$lib_inp already exists." >> $out_file
    echo "Please turn the New_Library parameter OFF if you wish to append." >> $out_file  
    set e_status = 1
    goto done
  endif

    
else
  if (! -e $lib_struct) then
    echo "" >>! $out_file
    echo "$lib_struct does not exist." >> $out_file
    echo "Please turn the New_Library parameter ON" >> $out_file
    echo "if you wish to create a new library." >> $out_file
    set e_status = 1
    goto done
  endif

  if (! -e $lib_inp) then
    echo "" >>! $out_file 
    echo "$lib_inp does not exist." >> $out_file
    echo "Please turn the New_Library parameter ON" >> $out_file
    echo "if you wish to create a new library." >> $out_file
    set e_status = 1
    goto done
  endif

endif

#
#  Determine targets format for the library
#  and decide whether to use the new genfra.
#
if ($new_lib == "NEW") then
   set new_targs = 1
   set LUDI_IREAD = 0
else
   unalias head
   set targ_form = `strings $lib_inp |head -1 `
   if ("$targ_form" == "Genfra 5.00") then
       set new_targs = 1
       setenv LUDI_APPEND 1
   else
       set new_targs = 0
       set link_type = `/bin/grep -c SLINK $control`
       if ($link_type == 0) then
         set link_type = `/bin/grep -c TLINK $control`
       endif
       if ($link_type != 0) then
         echo "" >>! $out_file 
	 echo "Link Type and Link Site Specifications cannot be used" >> $out_file
         echo "when adding to libraries with this targets file format:" >> $out_file
	 echo "$lib_inp " >> $out_file
         echo "If you wish to add link entries to this library, please" >> $out_file
         echo "turn the Use_Methyls flag on and do not use Link Type" >> $out_file
	 echo "or Link Site Specs." >> $out_file
         set e_status = 1
	 goto done
       endif
   endif
endif

#
#  Determine structure format for the library
#  and decide whether to use the entry's pdb
#  structure file or sd structure file.
#

set format_id = `/bin/grep IREAD $control`
if ($format_id[2] == -1) then
# -1 indicates existing library format should be figured out.
# default is PDB
  setenv LUDI_IREAD 2
  set fragment_file = $pdb_entry
  set unused_file = $sd_entry

  set format = `strings $lib_struct |head -1 | /bin/grep FORMAT `

  if ($#format == 1) then
###
### Unformatted Libraries
###
    if ($format[1] == BIN_FORMAT) then
      set format = `strings $lib_struct |head -2 | /bin/grep MOLTOBIN`
      if ($#format > 0) then
	 echo "Can't add to Ludi/ACD release 95.0."
	 set e_status = 1
	 goto done
      else
        setenv LUDI_APPEND 1
        set format =  "`strings $lib_struct |head -2 | /bin/grep Genfra`"
	set format2 = ($format:x)
	if ($format2[4] == GenfraACD) then
	  setenv LUDI_IREAD 3
          setenv LUDI_IWRITE 1
	  setenv LUDI_COMPRESS 1
        else
          if ($format2[5] == SD) then
	    setenv LUDI_IREAD 3
	    setenv LUDI_IWRITE 1
	    ln -s $lib_struct fort.7
	    ln -s $lib_inp fort.3
          endif
        endif
      endif
    endif
  else 

###
### Formatted Libraries
###
    if ($#format >= 2) then
      if ($format[2] == MOL_FORMAT) then
	setenv LUDI_IREAD 3
      else
        if ($format[3] == PDB_FORMAT) then
	  setenv LUDI_IREAD 2
	endif
      endif
    endif
  endif

  if ($LUDI_IREAD == 3) then
    set fragment_file = $sd_entry
    set unused_file = $pdb_entry
  endif	    
  
  /bin/awk \
  "BEGIN {\
      format = $LUDI_IREAD\
  }"' \
  /IREAD   -1/       {print "IREAD    " format}\
  $0 !~ /IREAD   -1/ {print } \
  ' $control > $run_name.tmp

  if ($?LUDI_IWRITE != 0) then
    echo "IWRITE $LUDI_IWRITE" >> $run_name.tmp 
  endif

  if ($?LUDI_COMPRESS != 0) then
    echo "COMPRESS $LUDI_COMPRESS" >> $run_name.tmp 
  endif

  if ($?LUDI_APPEND != 0) then
    echo "APPEND $LUDI_APPEND" >> $run_name.tmp 
  endif

  /bin/mv $run_name.tmp $control

else
# new library so format was set by user
  if ($format_id[2] == 2) then
    set fragment_file = $pdb_entry
    set unused_file = $sd_entry
  else
    if ($format_id[2] == 3) then 
      set fragment_file = $sd_entry
      set unused_file = $pdb_entry
    endif
  endif
endif

######################################################
# Check whether identifier is unique within library  #
######################################################

if (-e $lib_inp) then 
   set unique = `grep -s ^$frag_label $lib_inp`
   set unique = $status
else
   set unique = 1
endif

if ($unique == 0) then
# Identifier is not unique
# Issue error message
   echo "" >>! $out_file
   echo " The Label $frag_label is not unique within the library." >> $out_file
   echo " Please check the library identifiers and enter a unique Label." >> $out_file
   echo " Library identifiers are the first field of the file" >> $out_file
   echo " $lib_inp" >> $out_file
   set e_status = 1
else
  if ($unique == 1) then

##############
# Run Genfra #
##############


    if ($new_targs == 0) then
      ln -s $fragment_file fort.41
      genfra_v4.4  < $control >>$out_file
      set e_status = $status
    else
      genfra -p $control -s $lib_struct  -t $lib_inp $fragment_file >>! $out_file
      set e_status = $status
    endif

############################################
# If new entries are ok add to the library #
############################################

    if ($e_status != 0) then
      echo "Error in background job." >> $out_file
      echo "Exiting script without touching library." >> $out_file
    else
      if ($?LUDI_APPEND == 1) then
	goto no_append
      endif	
      if (-e fort.7) then
        unalias wc
        set nb_lines = `wc -l fort.7`
	if ($nb_lines[1] != 0) then
          echo " " >> $out_file
          echo "Appending new entries to the library files" >> $out_file
          echo "$lib_struct" >> $out_file
          echo "$lib_inp." >> $out_file
          if (! -e $lib_struct) then
	    echo " " >> $out_file
            echo "$lib_struct doesn't exist." >> $out_file
            echo "Creating $lib_struct." >> $out_file
            touch $lib_struct
	    if ($status != 0) then
             echo "Can't create $lib_struct." >> $out_file
            endif
          endif

          if (! -e $lib_inp) then
	    echo " " >> $out_file
            echo "$lib_inp doesn't exist." >> $out_file
            echo "Creating $lib_inp." >> $out_file
            touch $lib_inp
	    if ($status != 0) then
             echo "Can't create $lib_inp." >> $out_file
            endif
          endif

          cat fort.7 >> $lib_struct
          if ($status == 0) then
	    /bin/rm fort.7
            set struct_added = 0
          else
            echo "Can't add to $lib_struct."  >> $out_file
	    set struct_added = 1
          endif

          if ($struct_added == 0) then

            cat fort.3 >> $lib_inp 

            if ($status == 0) then
              /bin/rm fort.3
            else
              echo "Can't add to $lib_inp." >> $out_file
            endif
          endif
	else
	  echo "Entry not created." >> $out_file
	  echo "Exiting script without touching library." >> $out_file
	endif
      else
        if ($new_targs == 0) then
	  echo "Entry not created." >> $out_file
          echo "Exiting script without touching library." >> $out_file
        endif
      endif
    endif
  endif
endif

no_append:

#set l = `date`
#mkdir genfra_$l[4]
#cp GENFRA_RUN* genfra_$l[4]/

############
# Clean up #
############

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

if ($?BIOSYM_REMOTE) then

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

endif

# Exit with the appropriate status

done:
 exit($e_status)
