#!/bin/bash

targetdir=$1
shift
exposures=$*

echo "Creating targetdir [$targetdir]"
mkdir -p $targetdir
mkdir -p $targetdir/smf

images=""
for exposure in $exposures; do
	echo "Requesting images for $exposure"
	image=`./postreq $exposure | grep datastore.ipp.ifa.hawaii.edu | sed 's/^.*\///'`
	images="$images $image"
	echo "Requesting smf for $exposure"
	./get_smf.sh $exposure $targetdir
done

todelete=""
for image in $images; do
	status=1
	while [ $status -ne 0 ]; do
		sleep 30
		echo "Copying images for $image"
		./getit $image $targetdir
		status=$?
	done
	todelete="$todelete $image.fits"
done
echo "All images copied locally"

echo "Rsyncing: rsync -av $targetdir ipp@ipp0222:/data/ipp022.0/ps1-outreach/"
rsync -av $targetdir ipp@ipp0222:/data/ipp022.0/ps1-outreach/
rsync -av $targetdir ipp@ipp0222:/data/ipp022.0/ps1-outreach/

echo
echo "Done"
echo "Dataset can be copied from rsync://ps1epo@ipp0222.ifa.hawaii.edu/ps1-outreach/$targetdir"
echo "Command line:"
echo "  rsync -avz rsync://ps1epo@ipp0222.ifa.hawaii.edu/ps1-outreach/$targetdir ."
echo
echo "Command to run to cleanup local directory:"
echo "  rm -rf $targetdir $todelete"
echo

