#!/bin/bash

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

cp ${srcdir}/../data/kbd-model-map scratch
chmod u+w scratch/kbd-model-map
cat >scratch/blocaled.conf << EOF
[settings]
keymapfile = $(pwd)/scratch/subdir/subsubdir/vconsole.conf
xkbdlayoutfile = $(pwd)/scratch/subdir/xorg.conf.d/00-keyboard.conf
EOF
rm -rf scratch/subdir
. ${srcdir}/ref-dbus.sh
. ${srcdir}/ref-polkit.sh
gdbus call \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --method org.freedesktop.locale1.SetX11Keyboard \
      "'fr'" "''" "'latin9'" "''" true true
cmp scratch/subdir/xorg.conf.d/00-keyboard.conf << EOF
# Automatically generated by blocaled
# Minimal xorg.xonf for keyboard layout

Section "InputClass"
        Identifier "Blocaled Keyboard"
        MatchIsKeyboard "on"
        Option "XkbLayout" "fr"
        Option "XkbVariant" "latin9"
EndSection
EOF
RES=$?

if [ $RES = 0 ]; then
    echo PASS: write-X11-no-dir
    cmp scratch/subdir/subsubdir/vconsole.conf <<- EOF
	KEYMAP='fr-latin9'
	EOF
RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS: xkbd convert
    ls -l scratch/subdir/xorg.conf.d/00-keyboard.conf >scratch/error
    sed -i 's/ .*//' scratch/error
    cmp scratch/error << EOF
-rw-r--r--
EOF
RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS: xkbd permissions
    LANG=C gdbus call \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --method org.freedesktop.locale1.SetX11Keyboard \
      "'de'" "''" "'euro2'" "''" false false |& cmp ${srcdir}/scratch/keyboard-write-result1
    RES=$?
fi
if [ $RES = 0 ]; then
    echo PASS: xkbd Not authorized
    rm -r scratch/subdir
    . ${srcdir}/unref-localed.sh
    sleep 0.1
    . ${srcdir}/ref-localed.sh --foreground 2>scratch/error
    gdbus call \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --method org.freedesktop.locale1.SetX11Keyboard \
      "'oh'" "''" "''" "''" true true
    sleep 0.1
    cmp scratch/error << EOF
Failed to find conversion entry for x11 layout 'oh' in '$(pwd)/scratch/kbd-model-map'
EOF
    RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS: bogus X11 layout convert
    . ${srcdir}/unref-localed.sh
    rm -rf scratch/subdir
    rm -f scratch/error
    . ${srcdir}/ref-localed.sh --foreground
    install -dm 555 scratch/subdir
    gdbus call \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --method org.freedesktop.locale1.SetX11Keyboard \
      "'fr'" "''" "'latin9'" "''" true true 2>scratch/error
    cmp scratch/error <<EOF
Error: GDBus.Error:org.gtk.GDBus.UnmappedGError.Quark._g_2dfile_2derror_2dquark.Code2: Could not create directory '$(pwd)/scratch/subdir/xorg.conf.d': Permission denied
EOF
    RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS: xkbd create dir unwritable
    rm -rf scratch/subdir
    install -dm 555 scratch/subdir/xorg.conf.d
    gdbus call \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --method org.freedesktop.locale1.SetX11Keyboard \
      "'fr'" "''" "'latin9'" "''" true true 2>scratch/error
    cmp scratch/error <<EOF
Error: GDBus.Error:org.gtk.GDBus.UnmappedGError.Quark._g_2dio_2derror_2dquark.Code14: Unable to save '$(pwd)/scratch/subdir/xorg.conf.d/00-keyboard.conf': Error opening file “$(pwd)/scratch/subdir/xorg.conf.d/00-keyboard.conf”: Permission denied
EOF
    RES=$?
fi

. ${srcdir}/unref-localed.sh
. ${srcdir}/unref-polkit.sh
. ${srcdir}/unref-dbus.sh
if [ $RES = 0 ]; then
    echo PASS: xkbd create file unwritable
    rm -rf scratch/subdir
    rm -f scratch/error
fi
rm scratch/kbd-model-map
rm scratch/blocaled.conf
exit $RES
