#!/bin/csh 
#   PIRUPDATE,   Version  2/13/2003
#csh script to download PIR CODATA files


# Example: {uses 'at' command to run a delayed batch job}
#  at 1am
#  at>gbupdate filelist &
#  at>ctrl-D  {ends the command }

#  This assumes you are in the PIR directory.  The existing files will be 
#  replaced with the new ones, and so must be writeable.
#  Remember to include the index and documentation files in filelist.
#  When disk space is tight, edit the 'filelist' file to download files in
#  order of decreasing size.  In general, you need to have at least twice as
#  much empty space as is necessary to hold the largest file to be downloaded.
#  This can be circumvented by setting $tmpdir to /tmp, rather than ".".

#-----------------------  Set environment variables -------------
#Check to see if $MAILID is set
if (${?MAILID} == 0) then
  echo Environment variable MAILID must be set to your full Internet address
  echo in the form     userid@hostname
  echo This is best done in your .cshrc file.
  exit
endif

# directory to store temporary files
set tmpdir = .
#set tmpdir = /tmp
set RM = 'rm -f'
set PIRUSERID = anonymous
set PIRPASSWD = $MAILID


# PIR download site, and directory at that site.
# Mirror sites and directories are commented out.

#- - - - - - - Natl. Biomed. Res. Found., Georgetown Univ.
set PIRHOST = ftp.pir.georgetown.edu
set PIRDIR =  pir_databases/psd/codata
set PIRUSERID = anonymous
set PIRPASSWD = $MAILID

# - - - - - - - - - - - - - - -
# Use 'ls -l' to write a list of files on remote server,
# Send output to ls.out
  # generate FTP command file
  echo user $PIRUSERID $PIRPASSWD > ftp.input
  echo cd $PIRDIR >>  ftp.input
  echo bin >> ftp.input
  echo ls -l ls.out >> ftp.input 
  echo bye >> ftp.input
  # run FTP
  nice ftp -i -n $PIRHOST < ftp.input


#-----------------------  MAIN LOOP -------------
foreach file (`cat $1`)

     set name = $file:r

     echo $file

  # Create a temporary list of all files for a
  # PIR division. Each PIR division has a .dat.Z and a .nam file
  #egrep -e $file'[0-9]*\.dat\.Z'  -e $file'[0-9]*\.nam' ls.out > $$.temp
  egrep -e $file'[0-9]*' ls.out > $$.temp
  cat $$.temp
  if ( -z $$.temp) then # division is in a single file
    echo $file > $$.filelist
  else # division is split among several files
    tr -s ' ' '	' < $$.temp | cut -f9 > $$.filelist 
  endif 
  cat $$.filelist
  $RM $$.temp
  

  foreach file (`cat $$.filelist`)

     #Nomenclature:
     # $file - original gzipped file eg. gbest1.dat.Z
     # $name - $file minus .Z extension eg. gbest1.dat
     # $base - raw file name eg. gbest1
     if ($file:e == Z) then
        set name = $file:r
        if ($name:e == dat) then
           set base = $name:r
           # Make some space by deleting the current PIR division, if
           # it exists.
           echo "Removing file(s) for $base, if they exist"
           $RM $name.*
        endif
     endif


     # Create input file for ftp command. Logs in, moves to correct directory,
     # and downloads the data. Then logs out.
     echo user $PIRUSERID $PIRPASSWD > ftp.input
     echo cd $PIRDIR >>  ftp.input
     echo bin >> ftp.input 
     echo get $file $tmpdir/$file  >>  ftp.input
     echo bye >> ftp.input

     #   Get the file from PIR
     nice ftp -i -n $PIRHOST < ftp.input
     #cat ftp.input >> ftp.test
    
     #   Make sure that the file received is the same length as the original
     #file.
     set ORIGINAL = `grep $file ls.out |tr -s ' ' '	' |cut -f5`
     set RECIEVED = `ls -l $tmpdir/$file |tr -s ' ' '	' |cut -f5`
     echo 'ORIGINAL=  '$ORIGINAL
     echo 'RECEIVED=  '$RECIEVED
     if ($ORIGINAL == $RECIEVED) then 
        ls -l $tmpdir/$file >> files_received
        if ($file:e == Z) then
           #Uncompress the file
           if ($name:e == dat) then
              nice +10 uncompress -f $tmpdir/$file 
              #Run splitdb to create the database
              nice +10 splitdb -c -p $tmpdir/$name $base.ano $base.wrp $base.ind
              set success = $status
              chmod a+r $base.*

              #Remove the .dat file if splitdb exited with a return code of 0.
              if ($success == 0) then 
                 $RM $tmpdir/$name
              # Otherwise, remove the partially split files to make room.
              # These will have to be split manually.
              else
                 $RM $base.ano $base.wrp $base.ind
              endif # success == 0
           else
              nice +10 zcat -c -f $tmpdir/$file > $name
              $RM $tmpdir/$file
              chmod a+r $name

           endif # $name:e == dat
         endif # $file:e == Z
      else
        echo $file >> files.missed
      endif # $ORIGINAL == RECEIVED
  end #foreach
  
  $RM $$.filelist
  

end #foreach


  # move documentation files, whose names begin with 0
  # to documentation directory
  chmod a+r 0*
  mv 0* $doc/PIR