#!/bin/bash

echo -e "${RED}##################### Loading Process_Download.sh ${NC}"

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

if [ -z $expName ]; then
    source $SOLARISANADIR/working/expName.sh
fi

IP=solarisdaq # defined at ~/.ssh/config
USR=solaris

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

    if [ ${PCID} -eq 2 ]; then

        #=========== Ping to check the connectivity
        echo "Checking $IP connetivity, max wait for 3 sec...."
        ping -c 3 -W 3 $IP #> /dev/null
        if [ $? -ne 0 ]; then
            echo -e "$RED !!!!!!! $IP is not alive $NC"
        else
            #============ 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/.
        fi
    else
      echo -e "$RED############### Only in SOLARIS MAC can donwload data. skip.$NC"
    fi

    echo -e "$YELLOW=============================================$NC"
    tail -10 $MacRawDataPath/raw_data/RunTimeStamp.dat
    echo -e "$YELLOW=============================================$NC"
    
    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})

#######################################
####################  Download raw data
echo -e "${CYAN}>>>>>>>>>>>>>>> Download raw data: run ${RUN}${NC}"
if [ ${PCID} -eq 2 ]; then

    #=========== Ping to check the connectivity
    echo "Checking $IP connetivity, max wait for 3 sec...."
    ping -c 3 $IP -W 3 #> /dev/null
    if [ $? -ne 0 ]; then
        echo -e "$RED !!!!!!! $IP is not alive $NC"
    else
        #============ 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/.
    fi    
else
  echo -e "$CYAN############### Only in SOLARIS MAC can donwload data. skip.$NC"
fi

echo -e "$YELLOW=============================================$NC"
tail -10 ${rawDataPath}/RunTimeStamp.dat
echo -e "$YELLOW=============================================$NC"

count=`ls -1 ${rawDataPath}/${expName}_${RUN}_*.sol 2>/dev/null | wc -l`
echo -e "========================= Number of Files : ${count}${YELLOW}"
if [ ${count} -eq 0 ]; then
    echo "${NC}============================================"
    echo "====  RAW Files of RUN-${RUN} not found! "
    echo "============================================"
    isRunDataExist=false
    exit 1
else
    du -hc ${rawDataPath}//${expName}_${RUN}_*.sol
    echo -e "$NC============================================="
    isRunDataExist=true
fi
