#!/bin/csh -f

set filename = $1
set workdir = $2
set outfile = $3
set MSROOT = $4
set CASENAME = $5
set type = $6

setenv MSP /${MSROOT}/${CASENAME}/ocn/hist
setenv MSPY /${MSROOT}/${CASENAME}/ocn/proc/tavg/annual/

echo "TYPE: "$type

set current = $PWD
cd $workdir
setenv complete 0

if ($filename !=  "Blank") then
  if ($type == "year") then
    hsi -P "ls ${MSPY}/$filename"
    if ($status == 0) then
      set filesize = `hsi -P ls -l ${MSPY}/$filename | awk '{print $5}'`
      hsi get ${MSPY}/$filename
    endif 
  else
    hsi -P "ls ${MSP}/$filename"
    if ($status == 0) then
      set filesize = `hsi -P ls -l ${MSP}/$filename | awk '{print $5}'`
      hsi get ${MSP}/$filename 
    endif
  endif

  set localsize = `ls -l $filename | awk '{print $5}'`

  if ($localsize == $filesize) then
    cd $current
    printf "retrieved " $filename > $outfile
  else
    echo "### ERROR ### Problem copying file over from hsi.  Problem file was "$filename
    echo "### ERROR ### It is recommended that you check all file sizes and delete any partial files before continuing."
  endif 
else
  cd $current
  printf "retrieved " $filename > $outfile
endif

