#!/bin/bash

# (c) Sean Parkin 12/08/12.  xp_launcher
# A launch script for XP that enables it to work with SHELXL2014. All it does is 
# 'REM' the statements that XP does not understand, launch XP using the modified 
# RES file, and restore the RES file upon exiting from XP.  If a new INS file is 
# made, all the REM'ed out lines will need to be hand edited in order to restore 
# their proper function. The script assumes that the XP executable file has been 
# renamed 'xpx'.  On OSX 10.9 (and perhaps OSX 10.7.x and 10.8.x), XP can now be 
# launched from a regular OSX Terminal.app window. This results in a short delay 
# unless an xterm window is already open. It is perhaps better to just use xterm 
# to launch XP in any case.
#
# 2013/10/07:  Converted to use perl to do search and replace so that the search 
# strings can be made case insensitive more easily on OSX.  The original version 
# used 'sed', but the OS X version of sed is crummy. There is probably a smarter 
# way to include perl than using a series of one liners, feel free to tell me.
#
# 2013/10/23:  Modified so that it can read files with either .ins or .res or no 
# extension (default). If there is no extension then it REAPs a .res file, as is 
# expected for normal XP behaviour. This all seems to work properly.  
#
# 2013/12/10: Open a dummy xterm window and run a nonsense script within the new
# xterm window. For some unknown reason, this tricks the XP graphics window into
# doing smooth rotation. The "xp-proj-enabler" script just runs an infinite loop 
# that continually writes some text onto the dummy xterm window.  So long as you
# have sensible window placement (see below in this script) this dummy xterm job 
# will be hidden by the XP graphics window, so it will be invisible.  When XP is 
# exited (properly, i.e. by typing "exit"), the dummy xterm job is killed by the 
# last line of this script. Note: the -iconic flag sends the dummy xterm down to 
# the dock, making it even less conspicuous.
#
# To use:
# i)   Rename the xp executable to xpx (i.e. mv xp xpx). 
# ii)  Move this script to a suitable place (e.g. mv xp /usr/local/bin/xp) 
# iii) Make it executable (e.g. chmod 775 xp).
#
# With this script in /usr/local/bin you can start up XP for any valid SHELXL-14 
# model without worrying about whether XP understands the new SHELXL14 commands. 
# If you have the 'xprun' (expect) script to call XP, then you can automatically 
# set things like atom types using ATYP that can't be set via the sxtl.ini file.
#
# Here is what it does:
#
# 1) Start a dummy xterm window with the xp-proj-enabler running in it. Note: it
# can be sent to the dock automatically with '-iconic', otherwise it will simply 
# hide behind the XP graphics window.  Also get the pid number of the process so 
# that xp-proj-enabler can be automatically killed on exit from XP.
#
xterm -iconic +sb -bg black -fg yellow -geometry 66x4 -e xp-proj-enabler &
#xterm +sb -bg black -fg yellow -geometry 66x4 -e xp-proj-enabler &
xp_proj_enabler_pid=`echo $!` 
#echo $xp_proj_enabler_pid

# 2) Check to see whether the filename to be input has an extension .ins or .res 
# so that the right file is backed up, and then make a backup copy.  If the file 
# you request is not available, it informs you and quits rather than starting XP 
# with a dead filename.

filename=$1
extension=$"`echo $filename | rev `"
extension="${extension:0:4}"
extension=$"`echo $extension | rev `"

if [ "$extension" == ".ins" ]; then
  cp $filename $filename.back
elif [ "$extension" == ".res" ]; then
  cp $filename $filename.back
else 
  filename=$filename.res
  cp $filename $filename.back
fi

if [ ! -f "$filename" ]; then
  echo
  echo "File $filename not found!"
  echo
# If there is no file, then kill the dummy xterm window. 
  kill $xp_proj_enabler_pid
  exit 
fi

cp $filename $filename.back
backup=$filename.back

# 3) Put REM in front of all the new SHELXL2014 commands so XP will ignore them. 
# If new commands are introduced in later releases of SHELXL, they can easily be 
# added here. 

perl -pi -w -e "s/ABIN/REM ABIN/gi;s/ANSC/REM ANSC/gi;s/ANSR/REM ANSR/gi;s/NEUT/REM NEUT/gi;s/PRIG/REM PRIG/gi;s/RIGU/REM RIGU/gi;s/TWST/REM TWST/gi;s/WIGL/REM WIGL/gi;s/XNPD/REM XNPD/gi;" $filename

# 4) Launch XP. This script (and also xprun, see below) expects that the real XP 
# executable has been renamed to 'xpx'.  You could launch XP with a call to xpx, 
# but this script now launches XP using an 'expect' script called 'xprun'.  This 
# enables a few defaults to be set, including those that cannot be altered using 
# the regular 'XP' sxtl.ini file. This may sound more complicated than it really 
# is, but (for me) it works like a charm.

screen_height=$(xdpyinfo | grep dimension | awk '{print $2}' | cut -d 'x' -f2)
if [ "$screen_height" == "1178" ]; then 
  xp_width=1408
else
  xp_width=1166
fi




#xpx $filename
xprun $filename $xp_width 

# 5) Upon exit from XP, restore the backed up .res (or .ins) file and dispose of 
# left over .tmp files. This clean up may not be needed, but it does no harm.

mv $backup $filename
if [ -f tg*.tmp ]; then
   rm tg*.tmp
fi

# 6) Kill the dummy xterm window.
kill $xp_proj_enabler_pid
