#!/bin/bash

# This scripts helps monitor how the transmission-cli download is
# going
#
# It does this by looking for the following in the nohup.log file
#
#     egrep "^Processed:" 

# By design 'transmission-cli' automatically starts serving up the
# JSON files for others in the P2P network so the process never
# finished
#
# It does support a -f option that gets run when a download is
# finished When the transmission-cli command was run, a
# -f argument was supplied to run a script that terminates the
# process

if [ "x$MDQ_HOME" = "x" ] ; then
    source ./SETUP.bash
    if [ $? != 0 ] ; then
	exit 1
    fi
fi

set -euo pipefail

if [ $# != 1 ] ; then
    echo "----" 1>&2
    echo "Usage: $0 <year>" 1>&2
    echo "----" 1>&2
    exit 1
fi

year=$1

year_nohup_log="dataset-download/nohup-torrent-download-$year.out"

if [ -f "$year_nohup_log" ] ; then

    echo "----"
    dataset-download/FIX-CRs.sh "$year_nohup_log" | egrep "^Progress:"
    echo "----"
    
#    echo "----"
#    echo "Checking for 'complete' in nohup log"
#    dataset-download/FIX-CRs.sh "$year_nohup_log" | fgrep -i complete
#    echo "----"

else
    echo "----" 1>&2
    echo "Error: Failed to find nohup log for $year: " 1>&2 
    echo "  $year_nohup_log" 1>&2
    echo "----" 1>&2
    exit 1
fi    
					      

echo ""
