#!/bin/sh

set -e

HOSTNAME=$(hostname)

# prepare configuration
cat >/etc/csync2.cfg <<EOF
group test
{
        host 127.0.0.1;
        host 127.0.0.2;
        host $HOSTNAME;
        key /etc/csync2.key;
        include /etc/shells;
}
EOF

# prepare keys
csync2 -k /etc/csync2.key

openssl req -x509 -newkey rsa:2048 \
  -days 300 -subj '/CN=csync2' -nodes \
  -keyout /etc/csync2_ssl_key.pem \
  -out    /etc/csync2_ssl_cert.pem 2>&1

# start daemon socket
sed -i 's/disable.*/disable = no/' /etc/xinetd.d/csync2
if [ -d /run/systemd/system ] ; then
    systemctl restart xinetd.service
else
    service xinetd restart
fi

# sync with inetd daemon on this host
if ! csync2 -x -N 127.0.0.2 -P 127.0.0.1 -v 2>&1 ; then
    r=$?
    if [ -d /run/systemd/system ] ; then
	systemctl status xinetd.service
	journalctl -u xinetd.service
    fi
    echo XFAIL this test case is racy especially on armhf
    exit 0
fi
