#!/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/30-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.SetVConsoleKeyboard \
      "'fr'" "'euro2'" true true
cmp scratch/subdir/subsubdir/vconsole.conf << EOF
KEYMAP='fr'
KEYMAP_TOGGLE='euro2'
EOF
RES=$?

if [ $RES = 0 ]; then
    echo PASS: write-KEYMAP
    cmp scratch/subdir/xorg.conf.d/30-keyboard.conf <<- EOF
	# Automatically generated by blocaled
	# Minimal xorg.xonf for keyboard layout

	Section "InputClass"
	        Identifier "Blocaled Keyboard"
	        MatchIsKeyboard "on"
	        Option "XkbLayout" "fr"
	        Option "XkbModel" "pc105"
	        Option "XkbOptions" "terminate:ctrl_alt_bksp"
	EndSection
	EOF
    RES=$?
fi

if [ $RES = 0 ]; then
    echo PASS: convert
    LANG=C gdbus call \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --method org.freedesktop.locale1.SetVConsoleKeyboard \
      "'de-latin1'" "'euro2'" false false |& cmp ${srcdir}/scratch/keyboard-write-result1
    RES=$?
fi
if [ $RES = 0 ]; then
    echo PASS: Not authorized
    . ${srcdir}/unref-localed.sh
    rm -r scratch/subdir
    gdbus call \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --method org.freedesktop.locale1.SetVConsoleKeyboard \
      "'oh-mg'" "''" true true 2> scratch/error
    cmp scratch/error scratch/keyboard-write-result2
    RES=$?
fi
if [ $RES = 0 ]; then
    echo PASS: bogus keymap convert
    . ${srcdir}/unref-localed.sh
    rm -rf scratch/subdir
    rm -f scratch/error
    sleep 0.1
    . ${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.SetVConsoleKeyboard \
      "'fr'" "'euro2'" true true 2>scratch/error
    sleep 0.1
    cmp scratch/error << EOF
Error: GDBus.Error:org.gtk.GDBus.UnmappedGError.Quark._g_2dfile_2derror_2dquark.Code2: Could not create directory '$(pwd)/scratch/subdir/subsubdir': Permission denied
EOF
    RES=$?
fi
if [ $RES = 0 ]; then
    echo PASS: keyboard create dir non writable
    rm -rf scratch/subdir
    install -dm 555 scratch/subdir/subsubdir
    gdbus call \
      --system \
      --dest org.freedesktop.locale1 \
      --object-path /org/freedesktop/locale1 \
      --method org.freedesktop.locale1.SetVConsoleKeyboard \
      "'fr'" "'euro2'" true true 2>scratch/error
    sleep 0.1
    cmp scratch/error << EOF
Error: GDBus.Error:org.gtk.GDBus.UnmappedGError.Quark._g_2dio_2derror_2dquark.Code14: Unable to save '$(pwd)/scratch/subdir/subsubdir/vconsole.conf': Error opening file “$(pwd)/scratch/subdir/subsubdir/vconsole.conf”: Permission denied
EOF
    RES=$?
fi

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