#!/bin/bash 

# (c) Sean Parkin 130228 - 251001
# A launch script for SHELXL-20XY that follows the refinement with a call to the 
# CIF editing script CIFFIX to modify the standard SHELXL-written CIF to include 
# data specific to the UK X-ray lab. If $1.cif doesn't exist, it exits after the 
# SHELXL20XY job. It first adds CONF and HTAB commands to the *.ins file if they 
# are not already present.  Following the SHELXL refinement it runs the R-tensor 
# program, RT, but only if there was a LIST 4 instruction given in the INS file. 
# The script assumes that the SHELXL-20XY executable has been named 'shelxlXY.Z' 
# and that it resides in /usr/local/bin/ or some other location in the PATH.  If 
# this script is saved with the name 'shelxl', then refinement, CIF editing with 
# CIFFIX and checking by RT are all done automatically, without further typing.

if [ ! -f $1.ins ];
then
  echo " "
  echo " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  echo " +  SHELXL -  CRYSTAL STRUCTURE REFINEMENT  -   MULTI-CPU VERSION  +"
  echo " +  Copyright(C) George M. Sheldrick 1993-2025     Version 2025/1  +"
  echo " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
if [ "$1" == "" ]; then
  echo " +                      < NO *.ins FILE GIVEN >                     +"
else
  echo " +                 < CANNOT OPEN FILE $1.ins >                 +"
fi
  echo " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  echo " "
  exit
fi

# The next few lines insert CONF and/or HTAB commands to the *.ins file prior to 
# running the SHELXL job, but only if they aren't there already.  The insertions 
# are made before the FMAP command.  Note: gsed is gnu sed on MacOS, so on linux 
# just use sed instead.
#
keyword=$(grep -i -m1 '^CONF' $1.ins)
if [ "$keyword" == "" ]; then
  gsed -i 's/.*FMAP.*/CONF\n&/' $1.ins
fi
keyword=$(grep -i -m1 '^HTAB' $1.ins)
if [ "$keyword" == "" ]; then
  gsed -i 's/.*FMAP.*/HTAB\n&/' $1.ins
fi

# Replace any BOND line with BOND $H
#
gsed -i 's/^BOND $H/BOND/;s/^BOND/BOND $H/' $1.ins

# The next section allows the use of a (non-shelxl) keyword ISOT, which sets all 
# anisotropic displacement parameters to a fair approximation of their Uiso.  It 
# calls a script named 'isot', which needs to be present in /usr/local/bin.
#
keyword=$(grep -i -m1 '^ISOT' $1.ins)
if [ "$keyword" == "ISOT" ]; then
  gsed -i 's/ISOT//' $1.ins
  isot $1
  mv $1.ueq $1.ins
fi

# Strip the extra CIF junk from the end of the *.hkl file because it can mess up 
# the operation of CIFFIX. This uses csplit to take junk off the hkl file. It is 
# re-appended as the last task of this launch script.
#
csplit -n1 -k -s $1.hkl '/   0   0   0/+1'
mv xx1 .hkl-appendage
cp xx0 .hkl-main
mv xx0 $1.hkl

#shelxl14.7 $1 -t4 -b10000
#shelxl16.4 $1 -t4 -b10000
#shelxl16.5 $1 -t4 -b10000
#shelxl16.6 $1 -t4 -b10000
#shelxl17.1 $1 -t4 -b10000
#shelxl18.1 $1 -t4 -b10000
#shelxl18.3 $1 -t4 -b10000
#shelxl19.2 $1 -t4 -b10000
#shelxl19.3 $1 -t4 -b10000
# 240613: on m3max 14" MBP, 11 threads appears fastest, though not by much.
shelxl25.1 $1 -t11 -b10000

if [ ! -f $1.cif ];
then
  echo " +  No CIF could be found.  Please add the ACTA command to your *.ins file!  + "
  echo " +  R-tensor program, RT, only runs automatically if a CIF has been written  + "
  echo " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
  echo " "
  cat .hkl-main .hkl-appendage > $1.hkl
  rm .hkl-main .hkl-appendage
  exit
else
# The & at the end of the next line allows the rest of this script to run at the 
# same time as CIFFIX. In effect, this appears to make CIFFIX instantaneous. The 
# same should not be done for RT because the RT run would not be complete by the 
# time that the *.rtn file is removed. Dammit - it's sometimes a bad idea to use 
# & at all as it means that ciftab might end up working with a file that has not 
# been fully edited - not good. Be prepared to occasionally have to remove the &
#  ciffix $1 2>/dev/null
# 180205:  For data from the old kappaCCD or the X8 Proteum, use the old version 
# of ciffix. Test the first character of the file name for if it begins with 'k' 
# or 'x'.  If should be self explanatory.
#
machine=${1:0:1}
if [[ $machine == "k" ]] || [[ $machine == "x" ]]; then
  ciffix-last-kappa-x8-version $1 2>/dev/null &
else
  ciffix $1 2>/dev/null &
fi

# 170406: Only run RT if there was a LIST 4 instruction in the *.ins file.
#
  keyword=$(grep -i -m1 'LIST' $1.ins)
  if [[ $keyword == *"4"* ]]; then 
    rt $1 2>/dev/null &
  fi

  echo " +  SHELXL-2025/1 structure refinement ------ (c) G.M. Sheldrick, 1993-2025  + "
  echo " +  SHELXL run using script shelxl-launcher ------ (c) S. Parkin, 2000-2025  + "
  echo " +  CIFFIX 'on-the-fly' instantaneous CIF editing  (c) S. Parkin, 2000-2025  + "  
  echo " +  SHELXL ------------------ Sheldrick, G.M. (2015). Acta Cryst. C71, 3--8  + "
  echo " +  Platon ------------------- Spek, A.L. (2009). Acta Cryst. D65, 148--155  + "
  if [[ $keyword == *"4"* ]]; then 
    echo " +  RT ----------------------- Parkin, S. (2000). Acta Cryst. A56, 157--162  + "
  fi
  echo " +  CIFFIX -------------- http://xray.uky.edu/people/parkin/programs/ciffix  + "
fi

# For automatic run after shelxl, delete $1.rtn. If there is anything that looks 
# suspicious in the $1.rto file, then perhaps re-run the RT standalone, and look 
# for problems.
#
if [ -f "$1.rtn" ]; then
   rm $1.rtn 2>/dev/null
fi

# Automatically run ciftab_automator script to create formatted tables, but wait 
# until the ciffix or platon jobs have finished. 
#
pgrep ciffix > /dev/null && echo ''
wait
pgrep platon > /dev/null && echo ''
wait

ciftab $1 

cat .hkl-main .hkl-appendage > $1.hkl
rm .hkl-main .hkl-appendage $1.rtn

exit
