#!/bin/sh
#
# Copyright (c) 2018 Aetf <aetf@unlimitedcodeworks.xyz>
# Copyright (c) 2018 Fabian Vogt <fvogt@suse.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# configuration path
helperdir=@libexecdir@

# Get a property from org.freedesktop.locale1
queryLocaledbus() {
    dbus-send --system --print-reply=literal --dest=org.freedesktop.locale1 /org/freedesktop/locale1 org.freedesktop.DBus.Properties.Get "string:org.freedesktop.locale1" "string:X11$1" 2>/dev/null | awk '{print $2}'
}

queryLocalectl() {
    localectl status | awk "/X11 $1/ {print \$3}"
}

# Query and setup system locale settings before start kmscon
setupLocale() {
    # Don't override existing values. Also there is no point in setting only some of them
    # as then they would not match anymore.
    if test -n "${XKB_DEFAULT_MODEL}" -o -n "${XKB_DEFAULT_LAYOUT}" -o -n "${XKB_DEFAULT_VARIANT}" -o -n "${XKB_DEFAULT_OPTIONS}"; then
        return
    fi

    if command -v dbus-send >/dev/null 2>/dev/null; then
        querycmd="queryLocaledbus"
    elif command -v localectl >/dev/null 2>/dev/null; then
        querycmd="queryLocalectl"
    else
        return
    fi

    X11MODEL="$(${querycmd} Model)"
    X11LAYOUT="$(${querycmd} Layout)"
    X11VARIANT="$(${querycmd} Variant)"
    X11OPTIONS="$(${querycmd} Options)"
    [ -n "${X11MODEL}" ] && export XKB_DEFAULT_MODEL="${X11MODEL}"
    [ -n "${X11LAYOUT}" ] && export XKB_DEFAULT_LAYOUT="${X11LAYOUT}"
    [ -n "${X11VARIANT}" ] && export XKB_DEFAULT_VARIANT="${X11VARIANT}"
    [ -n "${X11OPTIONS}" ] && export XKB_DEFAULT_OPTIONS="${X11OPTIONS}"
}

setupLocale
exec ${helperdir}/kmscon "$@"
