#!/bin/sh      
#!/usr/bin/ksh 
#           file: dirdif < ddexec.unix   [GBK Feb.97]  Updt 14 July 1999
#                 ------ < -----------
# /bin/sh :Bourne shell for HP, IBM Risc, Silicon Gr., and Sun machines
# /usr/bin/ksh :  Korn shell for DEC machines
#
# |--------------------------------------------------------------------|
# |--------------------------------------------------------------------|
# |                          DIRDIF-99                                 |
# |                                                                    |
# |    Crystal Structure Determination by Patterson Methods and        |
# |    Direct Methods  applied to  Difference Structure Factors        |
# |                                                                    |
# |    Paul T. Beurskens, Gezina Beurskens, Rene de Gelder,            |
# |    S. Garcia-Granda, R.O. Gould, Randy Israel, and Jan M.M. Smits  |
# |                                                                    |
# |    Crystallography Laboratory,  University of Nijmegen,            |
# |    Toernooiveld 1,  6525 ED  Nijmegen,  The Netherlands            |
# |                                                                    |
# |--------------------------------------------------------------------|
# |--------------------------------------------------------------------|
# |                                                                    |
# |  Script for EXECUTING the program system DIRDIF-99                 |
# |                                                                    |
# |  Specifications of this script:   Bourne shell  or  Korn shell     |
# |  Tested and used on computers:                                     | 
# |     DEC Alpha (with Bourne and Korn shell)                         |
# |     DEC 5000 under ultrix (Korn shell, problems with Bourne shell) |
# |     IBM Risc 6000/320 (aix, Bourne shell)                          |
# |     Silicon Graphics Indigo and Irix (Bourne shell)                |
# |     SUN under unix (Bourne shell)                                  |
# |        We thank H.J. Bruins Slot, A.L. Spek, J.P. Declerq,         |
# |        G. Baudoux and R. Driessen  for their kind  help in         |
# |        setting up DIRDIF under Unix, Ultrix, AIX, in 1992.         |
# |     HP computers (unix, Bourne shell)                              |
# |        Thanks are due to A. Meetsma and R.A. de Graaff             |
# |                                                                    |
# |--------------------------------------------------------------------|
# |                                                                    |
# |  In case of problems please contact one of the following persons:  |
# |        Paul T. Beurskens,  E-mail: ptb@sci.kun.nl                  |
# |        Jan M.M. Smits,     E-mail: smits@sci.kun.nl                |
# |        Rene de Gelder,     E-mail: rdg@sci.kun.nl                  |
# |--------------------------------------------------------------------|
#
# NOTE: The execution of dirdif has to be from the directory with the 
#       structural data ! And no parallel runs from the same directory ! 
#
# EDITING INSTRUCTIONS for this script file :
#
# First: check the definition of the shell to be used for this script:
# ====>  for some DEC machines it may be necessary to remove the first
#        line of this script, to change from Bourne to Korn shell.
#
# Next: define DDROOTPATH and ORUSERPATH as described here.
# The dirdif-system files are stored in the directory  xxxx/ddroot
# and the path to this directory must be defined like:
#   DDROOTPATH=/root/users/local/ddroot
#
# or for instance in the situation in Leiden:
#   DDROOTPATH=/users/rag/dirdifhp/ddroot
#
# or as in the actual situation in Nijmegen:
    DDROOTPATH=/nsr/ptb/ddroot
#
# ===>  IMPLEMENTATION: adapt DDROOTPATH=.... for your situation !
#
# In addition we must define the path to the file oruser!
# Oruser also is a dirdif-system file and has been stored 
# in the directory  ..../ddroot
# So, in general, ORUSERPATH=$DDROOTPATH would be correct.
#
# However, the file oruser should be used as a 'private' supplement 
# (with 'private' models appended by the individual user) to the file
# orbase (wich contains molecular models for program ORIENT);
# to enable the individual user access to his 'private' file oruser 
# during execution of dirdif, the path to oruser is defined as 
# the path to the user's home directory, for instance as follows:
#   
#   NAME=`whoami`
#   ORUSERPATH=/root/users/$NAME
#
# or more direct, as in Leiden:
#   ORUSERPATH=/users/rag
#
# and the actual situation in Nijmegen is:
    NAME=`whoami`
    ORUSERPATH=/nsr/$NAME
#
# The individual user is urged to add new structural models to oruser!
#
# In any dirdif run the presence of a 'private' oruser 
# (in the directory given by ORUSERPATH) is checked;
# if not present, $DDROOTPATH/oruser is copied to $ORUSERPATH/oruser.
# Then -when asked for by ORIENT- dirdif will search for a model
# in oruser and in orbase in this order.
#
# ===>  IMPLEMENTATION: adapt ORUSERPATH=.... for your situation !!
# ===>  then: exit ,  and proceed with the implementation instructions.
#
#   Store and check user environment
 if [ `pwd` = $DDROOTPATH -o `pwd` = $ORUSERPATH ]; then
   echo "ATTENTION !!! Your working directory is `pwd`,   however,"
   echo "dirdif has to be called from a structure data directory."
   echo "Please, change your working directory to the appropriate"
   echo "structure data directory and call dirdif again."
   exit 1
 fi
  echo "Execute DIRDIF-99.2 from pwd:  `pwd` "
#   Initialize keys ? abort not yet,  loop later?
 INDEX=$#             # number of parameters given in the call
#   test first parameter (CCODE) and write file DDJOB
 if [ $INDEX -lt 1 ]; then
    CCODE=END
 else
    CCODE=$1
    fi
#     to enforce the use of lower case in CCODE, capitals are converted:
 echo $CCODE > ddtemp1
 tr [A-Z] [a-z] < ddtemp1 > ddtemp2
 read CCODE < ddtemp2
 rm ddtemp1 ddtemp2
#   write DDJOB file
 if [ -f ddjob ]; then rm ddjob; fi
 echo $CCODE $2 $3 $4 > ddjob
#   Link files and check oruser
 ln -s $DDROOTPATH/ddhelp ddhelp
 ln -s $DDROOTPATH/orbase orbase
#   temp:
 if [ -f $DDROOTPATH/ddconfig ]; then
                             ln -s $DDROOTPATH/ddconfig ddconfig; fi
 if [ -f $DDROOTPATH/ddcon ]; then ln -s $DDROOTPATH/ddcon ddcon; fi
 if [ -f $DDROOTPATH/ddlic ]; then ln -s $DDROOTPATH/ddlic ddlic; fi
#
#   file oruser should also be available for personal use:
 if [ -f $ORUSERPATH/oruser ]; then
    ORU=0        # private oruser was present
 echo 'private oruser is present'
  else
 echo 'private oruser is initially not present'
    cp $DDROOTPATH/oruser $ORUSERPATH/oruser
    ORU=1        # private oruser made: print later
    fi
 ln -s $ORUSERPATH/oruser oruser
#   clean up and backup old output listing files
 if [ -f ddsyst ]; then rm ddsyst; fi
 if [ -f $CCODE.lis1 ]; then
    mv $CCODE.lis1 $CCODE.lis1.old
    echo "$CCODE.lis1 moved to $CCODE.lis1.old"
    fi
 if [ -f $CCODE.lis2 ]; then
    mv $CCODE.lis2 $CCODE.lis2.old
    echo "$CCODE.lis2 moved to $CCODE.lis2.old"
    fi
#      Start DIRDIF if everything is OK        mind the capital names !
 EXPROG=DDSTART
 loop=0
#   Working loop
 while [ $loop -eq 0 ]
 do
    ln -s $DDROOTPATH/$EXPROG $EXPROG.EXE
    $EXPROG.EXE 2>dirdif.log
    retcod=$?
 if [ $retcod -ne 0 ]; then
    echo "   Return code = $retcod : stop execution."
    echo "   ERROR returning from  $EXPROG"
    echo "   Contents of dirdif.log:"
    more dirdif.log
    echo " "
    loop=1
    fi
    if [ -f dirdif.log ]; then rm dirdif.log; fi
# next program ?
    rm $EXPROG.EXE
    read EXPROG < ddsyst
    if [ $EXPROG = STOP ]; then loop=1; fi
    done
#   Remove linked and remove unnecessary files
 if [ -f ddhelp ]; then rm ddhelp; fi
 if [ -f orbase ]; then rm orbase; fi
 if [ -f oruser -a `pwd` != $ORUSERPATH ]; then rm oruser; fi
 if [ -f ddconfig ]; then rm ddconfig; fi
 if [ -f ddcon ]; then rm ddcon; fi
 if [ -f ddlic ]; then rm ddlic; fi
 if [ -f ddsyst ]; then rm ddsyst; fi
 if [ -f ddjob ]; then rm ddjob; fi
#      Finished
 echo "dirdif finished at " `date`.
 if [ $ORU -eq 1 ]; then
    echo " oruser has been copied to directory $ORUSERPATH , "
    echo " keep this file as your 'private' oruser and extend it with"
    echo " your own models (mol. fragments, e.g. ccode.atmod files)."
    echo " Next run dirdif will -when asked for by ORIENT- search"
    echo " oruser and orbase in this order for the requested model. "
    echo "   "
 fi
 echo "Bye bye from dirdif  ---  DIRDIF-99.2  ---"
 echo "Current directory:  `pwd` "
#   End of dirdif script -------------------------------------------
