#!/bin/bash

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

cp ${srcdir}/../data/kbd-model-map scratch
chmod u+w scratch/kbd-model-map
rm -r scratch/keyboard
rm -r scratch/xkeyboard
. ${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/keyboard << EOF
KEYMAP='fr'
KEYMAP_TOGGLE='euro2'
EOF
RES=$?

if [ $RES = 0 ]; then
    echo PASS: write-KEYMAP
    cmp scratch/xkeyboard <<- 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
    rm scratch/keyboard
    rm scratch/xkeyboard
    . ${srcdir}/unref-localed.sh
    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
    rm -f scratch/keyboard
    rm -f scratch/xkeyboard
fi
rm scratch/kbd-model-map
. ${srcdir}/unref-localed.sh
. ${srcdir}/unref-polkit.sh
. ${srcdir}/unref-dbus.sh
exit $RES
