#
# 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 h20v7 h20v8 h20v9 h20v10 h21v7 h21v8 h21v9 h21v10 h22v7 h22v8 h22v9 h22v10 h23v7 h23v8 h23v9 h23v10;
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"

