#!/bin/bash
#
# nohup ./update_perturb.sh ithaca atmos T255L91 e00l >& update.log &
#
set -xuve
date

src=/cfu/releases/perturb
plt=$1
typ=$2
grd=$3
ver=$4

case $plt in
 ithaca)   dst=/share/data/cfu/perturb ;;
 mn-*)     dst=/gpfs/scratch/ecm86/ecm86010/perturb ;;
 ecmwf)    dst=ec:/c3m/perturb ;;
 ht-*)   dst=/work/pr1u1011/pr1u1011/pr1e1001/perturb ;;
 lindgren) dst=/cfs/klemming/nobackup/a/asifsami/perturb ;;
 jaguar)   dst= ;;
 ar-*)   dst=/work/pr1u1011/pr1u1011/shared/perturb ;;
esac

if [[ $plt != ecmwf ]]; then
 ssh $plt mkdir -p $dst/$typ/$grd
 rsync -acrv $src/$typ/$grd/$ver ${plt}:${dst}/$typ/$grd
else
 set +e
 ecaccess-file-mkdir $dst/$typ
 ecaccess-file-mkdir $dst/$typ/$grd
 ecaccess-file-mkdir $dst/$typ/$grd/$ver
 set -e
 list=/tmp/${typ}-${grd}-${ver}
 if [[ ! -a $list ]]; then
  ls -1 $src/$typ/$grd/$ver > $list
 fi
 for l in $(cat $list); do
  ecaccess-file-put $src/$typ/$grd/$ver/$l $dst/$typ/$grd/$ver/$l
  cat $list | sed -e '1d' > ${list}.tmp
  mv ${list}.tmp $list
 done
 cat $list; rm $list
fi

date
