#!/usr/bin/sh

# Numlock FallBack what resolves for Numlock switch bug of KDE/Plasma desktop environment 
# Written by Charles K Barcza - kbarcza@blackpanther.hu

if [ -n "$DEBUG" ];then
    ECHO=echo
else 
    ECHO='logger -t NumLockFB'
fi

if [ ! -x "$(which xset 2>/dev/null)" ];then
    $ECHO "xset binary is missing, please install first..."
    exit 1
fi
if [ ! -x "$(which kreadconfig5 2>/dev/null)" ];then
    $ECHO "kreadconfig5 binary is missing, please install first..."
    exit 1
fi
if [ ! -x "$(which numlockx 2>/dev/null)" ];then
    $ECHO "numlockx binary is missing, please install first..."
    exit 1
fi

STATE=$(xset q | grep 'Num Lock' | sed 's|.*01: ||' | sed 's|02:.*||' | awk '{print $3}')
SET=$(kreadconfig5 --file kcminputrc --group Keyboard --key NumLock)

if [ -n "$DEBUG" ];then
    $ECHO "Numlock Now: $STATE"
    $ECHO "Numlock Set: $SET"
fi

if [ "$SET" = "0" ];then
    # enable numlock after start kde
    if [ "$STATE" = off ];then
	$ECHO "The Numlock SET to ON"
	numlockx on
    else
	$ECHO "Numlock is already on, skip switch..."
    fi
elif [ "$SET" = "1" ];then
    # disabled numlock after start kde
    if [ "$STATE" = on ];then
	$ECHO "The Numlock SET to OFF"
        numlockx off
    else
	$ECHO "Numlock is already off, skip switch..."
    fi
        
elif [ "$SET" = "" ];then
    $ECHO "Don't changed Numlock State, cause not set in blackPanther System Settings"
    $ECHO "You can find: Systemsettings > Input devices > Keyboard > Numlock"
fi

