#
# Copy tiles from hsm while checking results.  Run as follows so it can
# run even if the terminal session is lost (e.g.: due to PC maintenance).
#
# nohup ./copy_tiles.sh > log &
# 
# log should have the success/failure status for all rsync attempts.
#

for tile in h18v13 h18v14 h18v15 h18v16 h18v17 h19v13 h19v14 h19v15 h19v16 h19v17 h20v13 h20v14 h20v15 h20v16 h20v17 h21v13 h21v14 h21v15 h21v16 h21v17 h22v13 h22v14 h22v15 h22v16 h22v17;
do
    for instrument in tm etm oli_tirs;
    do
        echo "###############################################"
        date
        echo ""
        echo "Starting copy for" $instrument $tile

        # Copy the tile for the current instrument.
        rsync -aq --password-file ~/etc/tile.secret tile_ST@10.0.84.17::lsat_ST_c1_in/$instrument/$tile ./

        # Check the copy status. 
        status=$?
        if [ $status -eq 0 ]
        then
            echo "tile" $tile $instrument "passed with status" $status
        else
            echo "tile" $tile $instrument "failed with status" $status
        fi
        echo ""
    done


    # Build the manifest (inventory).
    echo "###############################################"
    date
    echo ""
    echo "Starting manifest for" $tile

    ./build_manifest.sh $tile
    status=$?

    # Check the manifest status. 
    if [ $status -eq 0 ]
    then
        echo "tile" $tile "manifest passed with status" $status
    else
        echo "tile" $tile "manifest failed with status" $status
    fi
    echo ""

    # Change protections for the tile's files. 
    echo "###############################################"
    date
    echo ""
    echo "Changing protections for" $tile
    chmod 775 $tile/*
    echo ""

    # Print a completion message for the tile. 
    echo "###############################################"
    date
    echo ""
    echo "Done copy for" $tile
done

# Print a completion message for whole procedure. 
echo "###############################################"
date
echo ""
echo "Done copies"

