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

src=/cfu/releases/nudging
plt=$1
typ=$2
grd=$3
ver=$4
set +e; mem=$5; set -e

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

case $typ in
ocean)
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
;;

atmos)
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
;;
esac

date
