#!/bin/bash
# This script runs the shell script fragments from a batch directory

. /usr/local/lib/gce-funcs
. /root/test-config

KEEP=
GCE_DIR=ltm-batch

while [ "$1" != "" ]; do
    case $1 in
	--keep)
	    DO_KEEP=yes
	    ;;
	--gce-dir)
	    shift
	    GCE_DIR="$1"
	    ;;
	*)
	    echo "Unknown option: $1"
	    exit 1
	    ;;
    esac
    shift
done


BATCH_DIR=/run/batch-cmds

mkdir -p $BATCH_DIR
gsutil rsync gs://$GS_BUCKET/$GCE_DIR $BATCH_DIR

for i in $(find $BATCH_DIR -type f -print | sort) ; do
    echo "Running $i..."
    if test -z "$DO_KEEP" ; then
	gsutil rm gs://$GS_BUCKET/$GCE_DIR/$(basename $i)
    fi
    script -a -c "/bin/bash -vx $i" /var/log/gce-run-batch.log
    rm -f $i
done
