#!/bin/bash

if [ ! $# -ge 2 ]; then {
  echo "1. Log in to ra.psi.ch as yourself"
  echo "2. cd to /sls/x12sa/data/<pgroup>/raw/bin"
  echo "3. Run this script there"
  echo "Usage: $0 <pgroup> <0-skip nexus data files, 1-copy nexus data files><*optional* source directory><*optional* target directory>"
  echo "Example: $0 p16000 0"
  echo "Example: $0 p16000 0 /sls/x12sa/data/p16000/raw/ /das/work/p16/p16000"
  echo "The script runs into a timeout if there have not been any changes for 3 days."
  exit
} fi

# make sure we are on DaaS
host_full=`hostname`
host=`echo $host_full | awk -F'-' '{print $1}'`
if [ $host != 'ra' ]; then
   echo 'Unknown host. Please login to ra.psi.ch as yourself and run the script again.'
   exit 
fi

eaccount=$1

# source directory
sourceDir="/sls/x12sa/data/$1/raw"


subDir="p${eaccount:1:2}"
destDir="/das/work/$subDir/$eaccount/online_data"


if [ ! -z "$3" ]; then
   sourceDir=$3
fi
if [ ! -z "$4" ]; then
   destDir=$4
fi

cd /sls/x12sa/data/$1/raw/bin
 
# create destination directory

if [ ! -d "$destDir" ]; then {
  mkdir "$destDir"
}; fi

if [ $2 == 0 ]; then {
  echo skipping data nexus directory
} else {
  echo copying data nexus directory 
}; fi

# endless loop to be stopped by CTRL-C
while (true); do {

  if [ $2 == 0 ]; then {
      echo "rsync all data except the nexus data"
      rsync --chmod=ug=rwx -p --verbose --modify-window=1 --times --recursive --exclude='data' $sourceDir/* "$destDir"    
  } else { 
      echo "rsync all files from raw"
      rsync --chmod=ug=rwx -p --verbose --modify-window=1 --times --recursive $sourceDir/* "$destDir"
  }; fi
  df -h "$1"
  date
  # sleep half an hour
  #chmod -R ug=r "$destDir"
  old_time="$(find $destDir -type f -exec stat \{} --printf="%y\n" \; | sort -n -r | head -n 1)"
  old_time="$(date -d "$old_time" "+%s")"
  current_time="$(date -d '3 days ago' '+%s')"
  if [ "$current_time" -ge "$old_time" ]; then
    echo "Could not find any changes in the last few days."
    exit
  fi
  sleep 1800

}; done

