#!/bin/sh

#  DownloadArtifactoryItems.sh
#  myverizon
#
#  Created by Kyle on 3/2/20.
#  Copyright © 2020 Verizon Wireless Inc. All rights reserved.

ARTIFACTORYITEMS=./ArtifactoryItems.txt
ARTIFACTORY=Artifactory.sh
    
update_artifactory_item () {
    #echo "Run Artifactory for ${1} from ${2}"
    ./${ARTIFACTORY} "${1}" "${2}"
}

#Loop through items needed to download and download them.
PIDARRAY=()
LOGFILEARRAY=()
while read -r LOCALFILE REMOTEFILE; do

    #for if the directory has a parameter, such as PROJECT_DIR
    FILE=$(eval echo ${LOCALFILE})
    LOGFILE="/tmp/$(basename ${FILE}).txt"
    rm $LOGFILE 2> /dev/null
    touch $LOGFILE

    #download them in parallel and store the PIDS.
    update_artifactory_item "${FILE}" $REMOTEFILE $LOGFILE &
    PIDARRAY+=($!)
    LASTPID=${PIDARRAY[${#PIDARRAY[@]}-1]}
    LOGFILEARRAY+=($LOGFILE)
    echo "Process ${LASTPID} spawned for ${REMOTEFILE##*/}"
    
done < "${ARTIFACTORYITEMS}"

#wait for all processes to finish and fail if one of them fails.
INDEX=-1
TOTAL=${#PIDARRAY[@]}
for i in "${PIDARRAY[@]}"; do

    INDEX=$((INDEX + 1))
    LOGFILE=${LOGFILEARRAY[$((INDEX))]}

    echo "\n\n$((INDEX + 1)) / ${TOTAL} (PID: ${i}) "

    # tail the subprocess log
    tail -n +1 -f $LOGFILE &
    READ_PID=$!

    # wait for subprocess to finish
    sleep 0.05 # Allow tail -n +1 to print
    wait $i

    # catch any subprocess non-zero status
    status=$?
    if [[ $status -gt 0 ]]; then
        FAILED_PID_STATUS=$status
        FAILED_PID=$i
    fi

    # kill the running tail, consume the kill output
    kill $READ_PID
    wait $READ_PID > /dev/null 2>&1
    
    # Need proper way to terminate children.
    #if [[ -n $FAILED_PID ]] && [[ -n $FAIL_EARLY ]]; then
    #   echo "\n\nProcess ${FAILED_PID} failed with exit code ${FAILED_PID_STATUS}"
    #   echo $( ps -o pgid $FAILED_PID | grep [0-9] | tr -d ' ' )
    #   kill -9 $(printf '%s ' "${PIDARRAY[@]}")
    #   exit $FAILED_PID_STATUS
    #fi

done

if [[ -n $FAILED_PID_STATUS ]]; then
    echo "\n\nProcess ${FAILED_PID} failed with exit code ${FAILED_PID_STATUS}"
    exit $FAILED_PID_STATUS
fi
