#!/bin/sh
set -x

screenshot=xrdp-screenshot.png
magickout=match-matrix
testdir=$(dirname "$0")

sudo systemctl start xrdp

# Start XRDP and try to connect to it via Freerdp2, the package being tested.
# Take a screenshot of the X virtual framebuffer, which should contain the
# login screen.
xvfb-run -l "$testdir/connect-and-capture" "$screenshot"

# Confirm that the XRDP logo is in the log in screen (so as to confirm that the
# login screen is really there).
# https://stackoverflow.com/questions/31867093/how-to-search-an-image-for-subimages-using-linux-console
# compare creates two output files, $magickout-{0,1}; The latter
# is a grayscale pattern, in which a white pixel represents a match.
# If the images don't even resemble each other in any way, the compare
# will fail but we don't need to check that specifically, because so
# will the if below.
compare -metric RMSE -subimage-search \
  "$screenshot" /usr/share/xrdp/xrdp_logo.bmp png:"$magickout"
# In case of match, output will be like
# "520,347: (255,255,255)  #FFFFFF  gray(255)"
# in which the first field is the coordinates of the match.
if ! convert "$magickout-1" -threshold 95% txt: | grep -qi '#FFFFFF'; then
    2>&1 printf "%s\n" "Imagemagick did not find any occurence of the XRDP" \
                       "logo inside the screenshot of the remote desktop." \
                       "This test therefore failed."
    exit 2
fi
