#!/bin/sh

if ! $TDEDIR/bin/kreadconfig --file "q4osrc" --group "General" --key "first_login" --default "true" --type "bool" ; then
  exit
fi
if ! $TDEDIR/bin/kreadconfig --file "kpersonalizerrc" --group "General" --key "FirstLogin" --default "true" --type "bool" ; then
  #only needed for transiting current installations, remove this condition for future q4os/quark releases as not need
  $TDEDIR/bin/kwriteconfig --file "q4osrc" --group "General" --key "first_login" "false"
  exit
fi

SRCFNAME1="qtdeinit"
LOGT_FILE="$HOME/.appsetup2/log/${SRCFNAME1}_$( date +%Y-%m-%d-%H-%M-%S ).log"
mkdir -p "$( dirname "$LOGT_FILE" )"
echo "Creation time: $( date +%Y-%m-%d-%H-%M-%S )" > "$LOGT_FILE"

( (

PATH=/opt/trinity/bin:$PATH
ACTIVE_USER="$( whoami )"
echo "Active user: $ACTIVE_USER"
echo "Running qtdeinit ..."

if [ -f "$HOME/.addlangenv.sh" ] ; then
  . /usr/share/apps/q4os_system/bin/exportlang.sh
  # dbus-update-activation-environment --systemd LANG
fi
xdg-user-dirs-update

#todo: create tde desktop shortcuts
#todo: create tde autostart shortcuts

echo "disabling khotkeys daemon"
$TDEDIR/bin/kwriteconfig --file "khotkeysrc" --group "Main" --key "Autostart" "false"
$TDEDIR/bin/kwriteconfig --file "khotkeysrc" --group "Main" --key "Disabled" "true"
pkill khotkeys

if [ "$( $TDEDIR/bin/kreadconfig --file "/etc/q4os/q4base.conf" --group "OnInstall" --key "soundcard" )" = "0" ] ; then
  echo "disabling sound for user: $ACTIVE_USER"
  # $TDEDIR/bin/kwriteconfig --file "kmixrc" --group "" --key "Autostart" "false"
  $TDEDIR/bin/kwriteconfig --file "kcmartsrc" --group "Arts" --key "StartServer" "false"
  $TDEDIR/bin/kwriteconfig --file "knotifyrc" --group "StartProgress" --key "Arts Init" "false"
  $TDEDIR/bin/kwriteconfig --file "knotifyrc" --group "StartProgress" --key "Use Arts" "false"
else
  echo "setting sound volume levels"
  dash /usr/share/apps/q4os_system/bin/set_kmix_levels #unmute/set pcm and master volume after install
fi
q4hw-info --thinkpad-r51
if [ "$?" = "0" ] ; then
  echo "disable numlock on start for user: $ACTIVE_USER"
  $TDEDIR/bin/kwriteconfig --file "kcminputrc" --group "Keyboard" --key "NumLock" "1"
fi
q4hw-info --eeepc
if [ "$?" = "0" ] ; then
  echo "disable numlock on start for user: $ACTIVE_USER"
  $TDEDIR/bin/kwriteconfig --file "kcminputrc" --group "Keyboard" --key "NumLock" "1"
fi

# if [ "$MEMTOTAL" -gt "600" ] ; then
#   ctrl-compmgr --enable --conf-only
#   echo "Compositing enabled"
#   kdialog --icon "message" --title "Q4OS" --caption "setup" --msgbox "<p>Desktop effects will be enabled.</p><p>It is possible to fine tune or completely turn off the effects in Control Panel > Desktop > Window Behaviour > <b>Translucency</b> section.</p>"
# else
#   echo "Compositing not enabled"
# fi
# 
# if [ "$MEMTOTAL" -lt "600" ] ; then
#   $TDEDIR/bin/kwriteconfig --file "kickerrc" --group "General" --key "LegacyKMenu" "true"
#   echo "Classic menu set"
# fi

# if findmnt -n "/run/live/medium" ; then
if [ -f "/etc/q4oslivemedia" ] ; then
  echo "Disable screen locking for live session ..."
  $TDEDIR/bin/kwriteconfig --file "kdesktoprc" --group "ScreenSaver" --key "Lock" "false"
  # chmod a+r /home/$ACTIVE_USER/.trinity/share/config/kdesktoprc
fi

$TDEDIR/bin/kwriteconfig --file "kpersonalizerrc" --group "General" --key "FirstLogin" "false" #disable any possible kpersonalizer first boot actions
$TDEDIR/bin/kwriteconfig --file "q4osrc" --group "General" --key "first_login" "false"

) 2>&1 ) >> "$LOGT_FILE"
