#!/bin/bash

REF_IMAGE=$1

if [ -z ${REF_IMAGE} ]; then
	echo "No reference image passed"
	exit 125
fi

if [ ! -f ${REF_IMAGE} ]; then
	echo "Reference image is not found"
	exit 125
fi

AGL_SCREENSHOOTER=`whereis agl-screenshooter | awk -F ': ' '{print $2}'`

if [ -z $AGL_SCREENSHOOTER ]; then
	echo "Failed to find agl-screenshooter. Compositor too old?"
	exit 127
fi

#echo "Found agl-screenshoooter in $AGL_SCREENSHOOTER"
rm -rf agl-screenshot-*.png

if $AGL_SCREENSHOOTER; then
	echo "Screenshot taken"
else
	exit 127
fi

REF_IMAGE_SHA1SUM=`sha1sum ${REF_IMAGE} | awk -F ' ' '{print $1}'`
IMAGE_SHA1SUM=`sha1sum agl-screenshot-*.png | awk -F ' ' '{print $1}'`

if [ "${REF_IMAGE_SHA1SUM}" == "${IMAGE_SHA1SUM}" ]; then
	echo "Screenshot matches the reference image" 
	exit 0
else
	echo "Screenshot does not match the reference image" 
	exit 127
fi
