#! /usr/bin/env bash

#- in order to save disc space, take *.bin files
#- from this dir:
fromDir="../../tutorial_global_oce_latlon/input"

fileList=`( cd $fromDir ; echo *.bin )`

#echo 'fileList=' $fileList

#- and do a symbolic link in the current directory
#   (if the file does not already exist)
if test -d $fromDir ; then
  lnkList='files:'
  for xx in $fileList
  do
    if test -r ${fromDir}/$xx ; then
      if test ! -r $xx ; then
        lnkList=${lnkList}" "$xx
        ln -sf ${fromDir}/$xx .
      fi
    fi
  done
  echo ' link' $lnkList "from dir:" $fromDir
  #- SST file: copy last month (=Dec) and append to the beginning
  xx='lev_sst' ; inpFile=${fromDir}/${xx}.bin ; tmpFile=${xx}_startdec.tmp
  if test -f $inpFile -a $tmpFile -ot $inpFile ; then
    dd if=$inpFile bs=14400 count=1 skip=11 of=${xx}_dec.tmp 2> /dev/null
    cat ${xx}_dec.tmp $inpFile > $tmpFile
    #rm -rf ${xx}_dec.tmp
    echo ' + add 1 record to' $inpFile '-->' $tmpFile
  fi
else
  echo " Error:" $fromDir "not a directory"
fi

