#!/bin/bash

exec >"$(basename $0)".log 2>&1

cat > scratch/xkeyboard << EOF
Section "InputClass"
        Identifier "libinput keyboard catchall"
        MatchIsKeyboard "on"
        MatchDevicePath "/dev/input/event*"
        Driver "libinput"
        Option "XkbLayout" "fr"
        Option "Xkbvariant" "latin9"
        Option "xkbModel" "pc105"
        Option "xkboptions" "terminate:ctrl_alt_bksp"
EndSection
EOF
. ${srcdir}/ref-dbus.sh
gdbus introspect \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --only-properties | grep X11 | cmp ${srcdir}/scratch/xkeyboard-read-result1
RES=$?

if [ $RES = 0 ]; then
    echo PASS read-xkeyboard
    cat > scratch/xkeyboard <<- EOF
	Section "InputClass"
	        Driver "libinput"
	Section "InputClass"
	        Identifier "libinput keyboard catchall"
	        MatchIsKeyboard "on"
	        MatchDevicePath "/dev/input/event*"
	# Section "InputClass"
	        Option "XkbLayout" "fr"
	        Option "Xkbvariant" "latin9"
	# EndSection
	        Option "xkbModel" "pc105"
	        Option "xkboptions" "terminate:ctrl_alt_bksp"
	EndSection
	EOF
    . ${srcdir}/ref-localed.sh
    sleep 0.1
    gdbus introspect \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --only-properties | grep X11 | cmp ${srcdir}/scratch/xkeyboard-read-result1
    RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS Comments
    cat > scratch/xkeyboard <<- EOF
	Section "InputClass"
	        Option "XkbLayout" "fr"
	EOF
    . ${srcdir}/ref-localed.sh
    sleep 0.1
    gdbus introspect \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --only-properties | grep X11 | cmp ${srcdir}/scratch/xkeyboard-read-result2
    RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS No End Section
    cat > scratch/xkeyboard <<- EOF
	Section "Screen"
	        MatchIsKeyboard "on"
	        Option "XkbLayout" "fr"
	EndSection
	EOF
    . ${srcdir}/ref-localed.sh
    sleep 0.1
    gdbus introspect \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --only-properties | grep X11 | cmp ${srcdir}/scratch/xkeyboard-read-result2
    RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS No InputClassSection
    cat > scratch/xkeyboard <<- EOF
	Section "InputClass"
	        Option "XkbLayout" "fr"
	EndSection
	EOF
    . ${srcdir}/ref-localed.sh
    sleep 0.1
    gdbus introspect \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --only-properties | grep X11 | cmp ${srcdir}/scratch/xkeyboard-read-result2
    RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS No MatchIsKeyboard
    cat > scratch/xkeyboard <<- EOF
	Section "InputClass"
	        Option "XkbLayout" "fr"
	Section "InputClass"
	        MatchIsKeyboard "on"
	EndSection
	EOF
    . ${srcdir}/ref-localed.sh
    sleep 0.1
    gdbus introspect \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --only-properties | grep X11 | cmp ${srcdir}/scratch/xkeyboard-read-result2
    RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS InputClass after xkblayout
    cat > scratch/xkeyboard <<- EOF
	Section "InputClass"
	        Option "XkbLayout" "fr"
	        Option "XkbModel" "pc105"
	        MatchIsKeyboard "on"
	        Option "XkbVariant" "latin9"
	        Option "xkbOptions" "terminate:ctrl_alt_bksp"
	EndSection
	EOF
    . ${srcdir}/ref-localed.sh
    sleep 0.1
    gdbus introspect \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --only-properties | grep X11 | cmp ${srcdir}/scratch/xkeyboard-read-result1
    RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS MatchIsKeyboard after XkbLayout
    cat > scratch/xkeyboard <<- EOF
	Section "InputClass"
	        MatchIsKeyboard "on"
	Section "InputClass"
	EndSection
	EOF
    . ${srcdir}/ref-localed.sh --foreground --debug 2> scratch/error
    sleep 0.1
    gdbus introspect \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --only-properties | grep X11 | cmp ${srcdir}/scratch/xkeyboard-read-result2
    RES=$?
    if [ $RES = 0 ]; then
        sed -i -n '/parse/s/[^]]*]//p' scratch/error
        cmp scratch/error << EOF
: Debug: Unable to parse '$(pwd)/scratch/xkeyboard'
EOF
    fi
    RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS InputClass after MatchIsKeyboard
    cat > scratch/xkeyboard <<- EOF
	Section "InputClass"
	        MatchIsKeyboard "on"
	        Option "XkbLayout" "fr"
	EOF
    . ${srcdir}/ref-localed.sh --foreground --debug 2> scratch/error
    sleep 0.1
    gdbus introspect \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --only-properties | grep X11 | cmp ${srcdir}/scratch/xkeyboard-read-result2
    RES=$?
    if [ $RES = 0 ]; then
        sed -i -n '/parse/s/[^]]*]//p' scratch/error
        cmp scratch/error << EOF
: Debug: Unable to parse '$(pwd)/scratch/xkeyboard'
EOF
    fi
    RES=$?
fi

if [ $RES = 0 ]; then rm -f scratch/{xkeyboard,error}; fi
. ${srcdir}/unref-localed.sh
. ${srcdir}/unref-dbus.sh
exit $RES
