#!/bin/bash

if [ $# -eq 0 ] || [ $1 == "-help"  ]; then
  echo "$./process_Download [RunNum]"
  echo "    RunNum = run number"
  echo "   * if RunNum = all, sync all"
  exit 1
fi;
RUN=$1

runNum=${RUN#0} #remove zero
RUN=$(printf '%03d' $runNum) ##add back the zero

source $SOLARISANADIR/armory/Process_BasicConfig
source $SOLARISANADIR/working/expName.sh

MacRawDataPath=$rawDataPathParent/$expName/
IP=solarisdaq # defined at ~/.ssh/config
USR=solaris

if [ ${RUN} == "all" ]; then

    if [ ${PCID} -eq 2 ]; then
        #============ Get the raw data
        rsync -avuht --progress $USR@$IP:$rawDataPath/$expName_* $MacRawDataPath/data/.
        
        echo -e "$YELLOW======== rsync RunTimeStamp.dat $NC"
        rsync -avuht --progress $USR@$IP:$rawDataPath/$RunTimeStamp* $MacRawDataPath/data/.
        
        echo -e "$YELLOW======== rsync expName.sh $NC"
        rsync -avuht --progress $USR@$IP:Analysis/working/expName.sh $SOLARISANADIR/working/.

        echo -e "$YELLOW=============================================$NC"
        tail -10 $MacRawDataPath/data/RunTimeStamp.dat
        echo -e "$YELLOW=============================================$NC"
    else
      echo -e "$RED############### Only in SOLARIS MAC can donwload data. skip.$NC"
    fi
    
    exit 1
fi

#just in case people put %03d as RUN
if [ "${RUN:0:2}" == "00" ]; then
    RUN=${RUN:2:1}
elif [ "${RUN:0:1}" == "0" ]; then
    RUN=${RUN:1:2}
else
    RUN=$(printf '%d' $RUN)
fi
RUN=$(printf '%03d' ${RUN})

echo $RUN

#######################################
####################  Download raw data
echo -e "${RED}######################### Download raw data: run ${RUN}${NC}"
if [ ${PCID} -eq 2 ]; then
    #============ Get the raw data
    echo -e "================= RUN $RUN: Get the raw data `date`"
    
    rsync -avuht --progress $USR@$IP:$rawDataPath/$expName_$RUN* $MacRawDataPath/data/.
        
    echo -e "$YELLOW======== rsync RunTimeStamp.dat $NC"
    rsync -avuht --progress $USR@$IP:$rawDataPath/$RunTimeStamp* $MacRawDataPath/data/.
    
    echo -e "$YELLOW======== rsync expName.sh $NC"
    rsync -avuht --progress $USR@$IP:Analysis/working/expName.sh $SOLARISANADIR/working/.

    echo -e "$YELLOW=============================================$NC"
    tail -10 $MacRawDataPath/data/RunTimeStamp.dat
    echo -e "$YELLOW=============================================$NC"
    
else
  echo -e "$RED############### Only in SOLARIS MAC can donwload data. skip.$NC"
fi

count=`ls -1 $SOLARISANADIR/data_raw/${expName}_${RUN}_*.sol 2>/dev/null | wc -l`
echo -e "========== Number of Files : ${count}${NC}"
if [ ${count} -eq 0 ]; then
    echo "============================================"
    echo "====  RAW Files of RUN-${RUN} not found! "
    echo "============================================"

else
    echo -e "${YELLOW}"
    du -hc $SOLARISANADIR/data_raw/${expName}_${RUN}_*.sol
    echo -e "$NC============================================="

fi