#!/bin/bash
#
# nohup ./update_ic.sh ithaca atmos T255L62 b0ga >& update.log &
#
set -xuve
date

src=/esnas/releases/ic
plt=$1
typ=$2
grd=$3
ver=$4

case $plt in
 ithaca)   dst=/share/data/cfu/ic ;;
 mn-*)     dst=/gpfs/scratch/ecm86/ecm86010/ic ;;
 ecmwf)    dst=ec:/c3m/ic ;;
 ht-*)     dst=/work/pr1u1011/pr1u1011/pr1e1001/ic ;;
 lindgren) dst=/cfs/klemming/nobackup/a/asifsami/ic ;;
 jaguar)   dst= ;;
 ar-*)     dst=/work/pr1u1011/pr1u1011/shared/ic ;;
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
