#!/bin/bash
MAXMEM=50

if [ -z $1 ]; then
   echo 'Error. No server or node number specified!'
   exit 0
fi
if [ -z $2 ]; then
   echo 'Error. No node number specified!'
   exit 0
else
   SERVER=$1
   NODE=$2
   case $NODE in 
      ''|*[!0-9]*) echo Error. Expected integer as second command line argument ;;
   esac
fi

INITDIR=`pwd`

com="globus-job-run ce."$SERVER".cro-ngi.hr /usr/bin/ssh node"$NODE" ls -d /scratch/*/temp*"
eval $com > jobdirlist

echo 'Number of jobs found: ' `wc -l < jobdirlist`

   while read jobdir; do
      exportdir=$INITDIR/${jobdir##*/}
      echo 'Creating directory: '$exportdir
      mkdir $exportdir
      com="globus-job-run ce."$SERVER".cro-ngi.hr /usr/bin/ssh node"$NODE" find "$jobdir" -type d"
      eval $com > dirlist
#### SKIP
      sed -i '/Work/d' dirlist
#      sed -i '/REST/d' dirlist
#### END SKIP
      while read dir; do
         subdir="$(echo $dir | sed 's@'"$jobdir"'@'"$exportdir"'@g')"
         echo 'Creating subdirectory: '$subdir
         mkdir -p $subdir
         com="globus-job-run ce."$SERVER".cro-ngi.hr /usr/bin/ssh node"$NODE" find "$dir" -maxdepth 1 -type f -size -"$MAXMEM"M" 
         eval $com > filelist
#### SKIP
      sed -i '/CC/d' filelist
#### END SKIP
         while read file; do
            exportfile="$(echo $file | sed 's@'"$jobdir"'@'"$exportdir"'@g')"
            com="globus-job-run ce."$SERVER".cro-ngi.hr /usr/bin/ssh node"$NODE" cat "$file""
            eval $com > $exportfile
         done < filelist
         rm filelist
      done < dirlist
      rm dirlist
   done < jobdirlist
   rm jobdirlist

exit 0
