#!/bin/csh -f
#                               -*- Mode: Sh -*- 
# rasmol.csh --- rasmol interface with caching
# Author          : Marcel Turcotte
# Created On      : Tue Mar 17 14:52:51 1998
# Last Modified By: Marcel Turcotte
# Last Modified On: Tue Mar 17 16:03:39 1998

# Based on http://scop.mrc-lmb.cam.ac.uk/scop/std/rs/rasmolscript.sh.txt

#
# Local copy of PDB database
#
if ( ! $?RASMOLPDBPATH ) then
    # set $RASMOLPDBPATH './'
    set $RASMOLPDBPATH $HOME/data/pdb
endif
#
# PDB database server
#
# set ftpserver='pdb.pdb.bnl.gov'
set ftpserver='ftp.ebi.ac.uk'
#
# Directory on server
# 
# set ftpserverdir='all_entries/compressed_files'
set ftpserverdir='pub/databases/pdb/all_entries/compressed_files'

set cmpext='.Z'
set script=/usr/tmp/rasmolscript0.$$
cat $* > $script

#
# extract name and check for existence:
#
set entry=`grep '^load pdb' $1 | sed 's/^load pdb//'| sed 's/"//g'`

#
# lookup for file
# 
if ( -f $RASMOLPDBPATH/$entry ) then
  xterm -e rasmol -script $1
else 
    sed "s/\.ent/.ent$cmpext/g" $script > /usr/tmp/rasmolscript1.$$
    set script=/usr/tmp/rasmolscript1.$$

    if ( -f $RASMOLPDBPATH/$entry$cmpext ) then
	xterm -e rasmol -script $script
    else
	echo "Fetching file $entry$cmpext via FTP"
	cd $RASMOLPDBPATH
	ftp -n $ftpserver <<FTPEND
	    user ftp $USER@`hostname`
	    binary
	    cd $ftpserverdir
	    get $entry$cmpext
	    quit
FTPEND

	if (-f $entry$cmpext) then
	    xterm -e rasmol -script $script
	endif
    endif
    /bin/rm $script
endif

exit 0
