#!/bin/bash
# 
# (c) S. Parkin 13/09/21:  xpdraw
# 
# A script that automates drawing of plot files created by George Sheldrick's XP 
# program.  It uses the companion 'expect' script named 'plt2ps' to call XP once 
# for every '.plt' file present in the directory from which it is run. The '.ps' 
# file colours are then edited using the unix/linux utility 'sed' to make subtle 
# changes to the colours to enhance readability.  For example, yellows (e.g. for 
# sulphur atoms) are made a bit darker.  The script then writes a *.PDF file for 
# each of the PostScript files present in the directory.  This script supercedes 
# 'xpfix' and 'ps2' (also still available). 
# Note:  For use on linux, the '.tmp' should be removed from the sed commands.

echo
echo " ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
echo " +  XP - Interactive molecular geometry. V.2008/1 for UNIX/X-Windows  + "
echo " +  COPYRIGHT(c) 2008 Bruker-AXS, Madison, WI -- All Rights Reserved  + "
echo " +  R u n n i n g   i n   n o n -- i n t e r a c t i v e   m o d e !  + "
echo " +  Automated creation/modification of PostScript files from XP done  + "
echo " +  with expect/bash scripts, plt2ps & xpdraw -- (c) S. Parkin, 2013  + "
echo " ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
echo

draw_ps() 
{ 
 new=$(basename $1 .plt) 
 plt2ps $new 
}

for file in $(ls *.plt)
do
 draw_ps $file
done

sed -i .tmp 's/Bruker Nonius Inc./XP (G.M. Sheldrick), xpdraw \& plt2ps (S. Parkin)/' *.ps
sed -i .tmp 's/C0 { 0 0 0 setrgbcolor }/C0 { 0 0 0 setrgbcolor }/' *.ps
sed -i .tmp 's/C1 { 0 1 0 setrgbcolor }/C1 { 0.0 0.7 0.0 setrgbcolor }/' *.ps
sed -i .tmp 's/C2 { 0.9 0 0 setrgbcolor }/C2 { 0.7 0.0 0.0 setrgbcolor }/' *.ps
sed -i .tmp 's/C3 { 0 0 0.9 setrgbcolor }/C3 { 0.0 0.0 0.7 setrgbcolor }/' *.ps
sed -i .tmp 's/C4 { 1 0.8 0 setrgbcolor }/C4 { 0.8 0.7 0.0 setrgbcolor }/' *.ps
sed -i .tmp 's/C5 { 1 0 1 setrgbcolor }/C5 { 0.4 0.0 0.4 setrgbcolor }/' *.ps
sed -i .tmp 's/C6 { 0.4 1 0.4 setrgbcolor }/C6 { 0.1 0.5 0.1 setrgbcolor }/' *.ps
sed -i .tmp 's/C7 { 0.6 0.6 0.6 setrgbcolor }/C7 { 0.3 0.3 0.3 setrgbcolor }/' *.ps
sed -i .tmp 's/C8 { 0 0.3 1 setrgbcolor }/C8 { 0.0 0.2 0.8 setrgbcolor }/' *.ps
sed -i .tmp 's/C9 { 0 0.7 0 setrgbcolor }/C9 { 0.0 0.4 0.0 setrgbcolor }/' *.ps
sed -i .tmp 's/C10 { 0.6 0 1 setrgbcolor }/C10 { 0.5 0.0 0.7 setrgbcolor }/' *.ps
sed -i .tmp 's/C11 { 1 0.6 0 setrgbcolor }/C11 { 0.5 0.5 0.0 setrgbcolor }/' *.ps
sed -i .tmp 's/C12 { 0 1 1 setrgbcolor }/C12 { 0.0 0.5 0.7 setrgbcolor }/' *.ps
sed -i .tmp 's/C13 { 0.7 0.3 0 setrgbcolor }/C13 { 0.6 0.2 0.0 setrgbcolor }/' *.ps
sed -i .tmp 's/C14 { 0.4 0.4 0.4 setrgbcolor }/C14 { 0.2 0.2 0.2 setrgbcolor }/' *.ps
sed -i .tmp 's/C15 { 1 0.2 0.2 setrgbcolor }/C15 { 0.7 0.1 0.1 setrgbcolor }/' *.ps

# Remove the .tmp file made by sed:
# Note - this next line not needed for linux version.

rm *.tmp

# If you do not want .pdf files of your drawings, remove or comment out the next 
# nine non-blank lines.

draw_pdf() 
{
 new=$(basename $1 .ps)
 ps2pdf $1 $new.pdf
}

for file in $(ls *.ps)
do
 draw_pdf $file
done


