#!/bin/bash
# -*- coding: utf-8 -*-

# tiny script to show current keyboard layout
# including second and third level assignments.
#
# Written 2/2024 by Robin for antiX community
# GPL v.3
#
# dependencies: xkblayout-state gkbd-capplet
# expects to be started from within antiX desktop.
#
# usage: antiX-current-kb-layout

TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=antiX-current-kb-layout
export TEXTDOMAINDIR TEXTDOMAIN
source gettext.sh

acc_install_lock="/dev/shm/ackl-install-lock-$$04"

# do some housekeeping on leaving
function cleanup() {
    rm -f "$acc_install_lock" >/dev/null
}
trap cleanup EXIT

# user info dialog popup from antiX-samba-mgr, expects positionals:
# width (int), height (int), timeout (int)/buttons (-1),
# header (str), text (str)
function infobox() {
    TEXTDOMAIN=antiX-samba-mgr
    case $3 in
      '-1') yad --skip-taskbar --on-top \
             --borders=10 \
             --center --width=$1 --height=$2 --fixed \
             --button="$(eval_gettext "Will do so later.")" \
             --window-icon="dialog-warning" \
             --title="! CAUTION !" \
             --text="<big><big><b>$4</b></big>\n\n$5</big>" >/dev/null;;
      *) yad --skip-taskbar --on-top \
             --undecorated --borders=15 \
             --center --width=$1 --height=$2 --fixed \
             --no-buttons --timeout=$3 \
             --window-icon="dialog-warning" \
             --title="! CAUTION !" \
             --text="<big><big><b>$4</b></big>\n\n$5</big>" >/dev/null;;
    esac
    TEXTDOMAIN=antiX-current-kb-layout
}


# Check whether prerequisites are present.
# make sure gkbd-keyboard-display and xkblayout-state commands are installed.
# function originates from antiXcc
prerequisites() {
TEXTDOMAIN=antixcc
    # this convenience function provides a full install dialog and expects all packages meant to be installed as positional parameters and cares for creation of an install dialog to be applied in entries which are in need of some additional helper tools not installed by default on some antiX flavours. Don't forget to set the $prerequesite_icon and $prerequisites_msg variables before calling.
    # if prerequisites is used in an entry please make sure to wait with reload of acc main window until you find this lockfile removed, so changed button functionality is processed properly at reload:

    # prepare textstring for output
    if [ $# -gt 1 ]; then
        for i in $@; do
            text="$text, $i"
        done
        text="$(sed 's/^\(.*\), \(.*\)$/\1 '$(eval_gettext "and")' \2/' <<<"${text:2}")"
    else
        text="$1"
    fi
    yad --center --fixed --undecorated --borders=10 \
        --window-icon=antiX-current-kb-layout \
        --image=antiX-current-kb-layout \
        --title="$(eval_gettext "Install \$text")" \
        --text="$prerequisites_msg\n\n""$(eval_gettext "Please install \$text packages first.\nDo you want to install the packages now?")""\n" \
        --button="$(eval_gettext "No thanks")":1 \
        --button="$(eval_gettext "Install")":0
        if [ $? -eq 0 ]; then
        #set -x
            urxvt -e bash -c '{ echo $$ > "'$acc_install_lock'"; success=false; sudo apt-get update; if [ $? = 0 ]; then sudo apt-get install '"$*"'; if [ $? = 0 ]; then echo -e \\\n"'$"Done."'"; success=true; fi; fi; if ! $success; then echo -e \\\n\"'$"Error. Please install $text packages yourself."'\"; fi; rm '$acc_install_lock'; sleep 4; }'
        #set +x
        fi

}

# check prerequisites
if ! which gkbd-keyboard-display  >/dev/null || ! which xkblayout-state >/dev/null; then
    prerequisites_icon="antiX-current-kb-layout"
    prerequisites_msg="$(eval_gettext "Prerequisites not found.")"
    prerequisites 'xkblayout-state' 'gkbd-capplet'
    TEXTDOMAIN=antiX-current-kb-layout # reset textdomain.
    sleep 1
    p='undefined'; [ -f $acc_install_lock ] && p=$(<$acc_install_lock)
    while [ -f $acc_install_lock ]; do sleep .1; if ! ps -p $p >/dev/null 2>&1; then break; fi; done
    if ! which gkbd-keyboard-display  >/dev/null || ! which xkblayout-state >/dev/null; then
        infobox 300 160 5 "$(eval_gettext "Install-error:")" "$(eval_gettext "Prerequisites still not installed.\nCan’t proceed.")"
        exit 1
    fi
fi

# this is what actually brings the current keyboard layout to screen.
gkbd-keyboard-display -g "$((1+$(xkblayout-state print %c)))"
