#!/bin/bash

if test -f /run/gce-xfstests-bld
then
    exit 0
fi

. /usr/local/lib/gce-funcs

if test -f /run/self-shutdown-requested
then
    /sbin/shutdown -h -P now "Self shutdown requested"
fi

if ! mkdir /run/gce-shutdown.singleton
then
    exit 0
fi

SYSLOG_RE_FAILURE="kernel: .*(WARNING)|(BUG): "

REGEXP="( Linux version )|(^CMDLINE)|(^FSTEST)|(^MNTOPTS)|(^CPUS:)|(^MEM:)|(^BEGIN)|(^MOUNT_OPTIONS)|(^MKFS_OPTIONS)|(^END)|(^EXT4-fs error)|(WARNING)|(^Ran: )|(^Failures: )|(^Passed)|(^_check_)|(inconsistent)|(^Shutdown reason)"
REGEXP_FAILURE="(^CMDLINE)|(^FSTEST)|(^MNTOPTS)|(^CPUS:)|(^MEM:)|(^BEGIN)|(^Failures: )|(^Passed)|(^Shutdown reason)"

function gen_xfstests_summary()
{
    grep -E "$REGEXP" < /results/runtests.log > /results/summary

    grep -E "$REGEXP_FAILURE" < /results/runtests.log > /results/failures
    printf "\n" >> /results/failures
    sed -n -f /usr/local/lib/get-check-failures.sed < /results/runtests.log >> /results/failures
    printf "\n" >> /results/failures
    grep -E "$SYSLOG_RE_FAILURE" < /results/syslog >> /results/failures
}

BLK_REGEXP="( Linux version )|(^CMDLINE)|(^FSTEST)|(^MNTOPTS)|(^CPUS:)|(^MEM:)|(^BEGIN)|(^MOUNT_OPTIONS)|(^MKFS_OPTIONS)|(^EXT4-fs error)|(WARNING)|(^_check_)|(inconsistent)|(^Shutdown reason)"
BLK_REGEXP_FAILURE="( Linux version )|(^CMDLINE)|(^FSTEST)|(^MNTOPTS)|(^CPUS:)|(^MEM:)|(^BEGIN)|(^MOUNT_OPTIONS)|(^MKFS_OPTIONS)|(^EXT4-fs error)|(WARNING)|(\[failed\])|(^_check_)|(inconsistent)|(^Shutdown reason)"

function gen_blktests_summary()
{
    run_tests=$(grep -E "(\[failed\])|(\[passed\])" < /results/runtests.log | awk '{print $1}')
    failed_tests=$(grep -E "\[failed\]" < /results/runtests.log | awk '{print $1}')

    nr_run="$(echo $run_tests | wc -w)"
    nr_failed="$(echo $failed_tests | wc -w)"

    grep -E "$BLK_REGEXP" < /results/runtests.log > /results/summary
    echo "Run: $(echo $run_tests)" >> /results/summary
    echo "Failures: $(echo $failed_tests)" >> /results/summary
    echo "Failed $nr_failed of $nr_run tests" >> /results/summary
    grep ^END < /results/runtests.log >> /results/summary

    grep -E "$BLK_REGEXP_FAILURE" < /results/runtests.log >> /results/failures
    echo "Run: $(echo $run_tests)" >> /results/failures
    echo "Failures: $(echo $failed_tests)" >> /results/failures
    echo "Failed $nr_failed of $nr_run tests" >> /results/failures
    grep ^END < /results/runtests.log >> /results/failures
    grep -E "$SYSLOG_RE_FAILURE" < /results/syslog >> /results/failures
}

reason=$(gce_attribute shutdown_reason)

if test "$reason" != "abort" -a -f /results/runtests.log -a \
    $(stat -c %s /results/runtests.log) -gt 0
then
    systemctl stop kvm-xfstests
    if test -n "$reason"
    then
	echo "$reason" > /results/shutdown_reason
    fi
    if test -f /results/shutdown_reason
    then
	echo "Shutdown reason: $(cat /results/shutdown_reason)" \
	     >> /results/runtests.log
    fi

    if test -f /run/do_blktests ; then
	gen_blktests_summary
	suite=blktests
    else
	gen_xfstests_summary
	suite=xfstests
    fi

    if test -n "$GCE_UPLOAD_SUMMARY"
    then
	gsutil cp /results/summary \
	    "gs://$GS_BUCKET/${BUCKET_SUBDIR:-results}/summary.$TESTRUNID.$(uname -r).txt"
    fi

    REPORT_EMAIL=$(gce_attribute report_email)
    JUNIT_EMAIL=$(gce_attribute junit_email)
    run_hooks send-email
    if test -n "$REPORT_EMAIL" -a -n "$SENDGRID_API_KEY"
    then
	if test -s /results/report
	then
	    RESULTS_REPORT=/results/report
	else
	    RESULTS_REPORT=/results/runtests.log
	fi
	subj="$suite results $TESTRUNID - $(uname -r)"
	if test -f /run/powerbtn
	then
	    subj="$subj - test run aborted"
	fi
	if test -z "$GCE_REPORT_SENDER"
	then
	    GCE_REPORT_SENDER="$USER@$HOSTNAME"
	fi
	export SENDGRID_API_KEY
	/usr/local/sbin/send-mail.py --sender "$GCE_REPORT_SENDER" \
		-s "$subj" "$REPORT_EMAIL" < "$RESULTS_REPORT"
    fi
    if test -n "$JUNIT_EMAIL" -a -n "$SENDGRID_API_KEY" -a -f /tmp/results.xml
    then
	subj="$suite junit results $TESTRUNID - $(uname -r)"
	if test -f /run/powerbtn
	then
	    subj="$subj - test run aborted"
	fi
	if test -z "$GCE_REPORT_SENDER"
	then
	    GCE_REPORT_SENDER="$USER@$HOSTNAME"
	fi
	export SENDGRID_API_KEY
	/usr/local/sbin/send-mail.py --sender "$GCE_REPORT_SENDER" \
		-s "$subj" "$JUNIT_EMAIL" < /tmp/results.xml
    fi
    tar -C /results -cf - . | xz -6e > /tmp/results.tar.xz
    gsutil cp /tmp/results.tar.xz \
	"gs://$GS_BUCKET/${BUCKET_SUBDIR:-results}/results.$TESTRUNID.$(uname -r).tar.xz"
    gsutil cp /tmp/results.xml \
	"gs://$GS_BUCKET/${BUCKET_SUBDIR:-results}/results.$TESTRUNID.$(uname -r).xml"
fi

if test -f /run/filestore-param ; then
    . /run/filestore-param
    if test -n "$FILESTORE_TOP" ; then
	mount -t nfs "$FILESTORE_TOP" /mnt
	rm -rf /mnt/$FILESTORE_SUBDIR /mnt/busy-$FILESTORE_SUBDIR
	busy=
	if ls /mnt/busy-* > /dev/null 2>&1 ; then
	    busy=t
	fi
	umount /mnt
	if test -z "$busy" ; then
	    gcloud filestore instances delete "$FILESTORE_NAME" \
		   --quiet --async --location="$FILESTORE_LOCATION"
	fi
    fi
fi

run_hooks shutdown
sleep 60
/usr/local/lib/gce-logger shutting down
if test -f /run/powerbtn
then
    /sbin/shutdown -h -P now "Power button pressed"
else
    touch /run/self-shutdown-requested
    gcloud compute -q instances delete $instance --zone $ZONE --delete-disks=boot
fi
