#!/bin/bash
#
# nohup ./update_nudging.sh ithaca glorys2v1 ORCA025L46 fc0 >& update.log &
#
set -xuve
date

src=/cfu/releases/nudging
plt=$1
ver=$2
grd=$3
mem=$4

case $plt in
 ithaca)   dst=/share/data/cfu/nudging ;;
 mn-*)     dst=/gpfs/scratch/ecm86/ecm86010/nudging ;;
 ecmwf)    dst=ec:/c3m/nudging ;;
 hector)   dst= ;;
 lindgren) dst=/cfs/klemming/nobackup/a/asifsami/nudging ;;
 jaguar)   dst= ;;
esac

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

date
