#!/bin/bash
#
# nohup ./update_model.sh ithaca ecearth v3.0.1 setup >& update.log &
#                           OR
# nohup ./update_model.sh ithaca ecearth v3.0.1 inidata >& update.log &
#
set -xuve
date

src=/cfu/models
plt=$1
mod=$2
ver=$3
cat=$4

case $plt in
 ithaca)   dst=/scratch/cfu/models ;;
 mn-*)     dst=/gpfs/projects/ecm86/models ;;
 ecmwf)    dst=c2a:/perm/ms/spesiccf/c3m/models ;;
 ht-*)   dst=/work/pr1u1011/pr1u1011/pr1e1001/models ;;
 lindgren) dst=/cfs/klemming/nobackup/a/asifsami/models ;;
 jaguar)   dst= ;;
 ar-*)   dst=/work/pr1u1011/pr1u1011/shared/models ;;
esac

if [[ $plt != ecmwf ]]; then
 rsync -acrv $src/$mod/$ver/$cat ${plt}:${dst}/$mod/$ver
else
 cd $src/$mod/$ver
 dirs_list=$(find setup/ -type d)
 for diR in $dirs_list; do
  set +e
  ecaccess-file-mkdir $dst/$mod/$ver/$diR
  set -e
 done
 list=/tmp/${mod}-${ver}-${cat}
 if [[ ! -a $list ]]; then
  find $cat/ -type f > $list
 fi
 for l in $(cat $list); do
  ecaccess-file-put $src/$mod/$ver/$l $dst/$mod/$ver/$l
  cat $list | sed -e '1d' > ${list}.tmp
  mv ${list}.tmp $list
 done
 cat $list; rm $list
fi

date
