#
# 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 h20v07 h20v08 h20v09 h21v07 h21v08 h21v09 h22v07 h22v08 h22v09 h23v07 h23v08 h23v09;
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"

