#!/bin/sh


PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME="common-cheatcodes"

###
# source distro-defaults
###

if [ -r /etc/default/distro ]; then
	. /etc/default/distro
fi

###
# source lsb functions
###
. /lib/lsb/init-functions

###
# source fll functions
###
. /lib/init/fll

###
# read in variables from /etc/default/fll-locales
###
if [ -r /etc/default/fll-locales ]; then
	. /etc/default/fll-locales
fi

FLL_LIVE_USER_HOME=$(getent passwd ${FLL_LIVE_USER} | cut -d \: -f 6)

###
# cheatcode handling
###

for param in $(cat /proc/cmdline); do
	case "${param}" in
		flldebug=*)
			if [ "${param#flldebug=}" = "${NAME}" ] || [ "${param#flldebug=}" = "all" ]; then
				fll_redirect
			fi
			;;
		nointro)
			NOINTRO="nointro"
			;;
	esac
done

no_intro()
{
for mntpnt in /fll/*; do
	if [ -d "${mntpnt}" ] && [ -f "${mntpnt}/index.html" ]; then
		INDEXFILE="${mntpnt}/index.html"
		for ext in svg png gif ico; do
			if [ -f "${mntpnt}/${FLL_CDROM_INDEX_ICON}.${ext}" ]; then
				INDEXICON="${mntpnt}/${FLL_CDROM_INDEX_ICON}.${ext}"
				break
			fi
		done
		break
	fi
done


###
# create desktop shortcut to index.html
###
if [ -f "${INDEXFILE}" ] && [ ! -f "${FLL_LIVE_USER_HOME}/Desktop/${FLL_DISTRO_NAME}.desktop" ]; then
    [ ! -d ${FLL_LIVE_USER_HOME}/Desktop ] && sudo -u ${FLL_LIVE_USER} mkdir ${FLL_LIVE_USER_HOME}/Desktop
    cat > "${FLL_LIVE_USER_HOME}/Desktop/${FLL_DISTRO_NAME}.desktop" <<EOF
#!/usr/bin/env xdg-open
[Desktop Entry]
Encoding=UTF-8
Type=Application
Exec=x-www-browser file:${INDEXFILE}
Icon=${INDEXICON}
Terminal=0

Name=${FLL_CDROM_INDEX}
GenericName=${FLL_CDROM_INDEX}
EOF
    chmod 755 "${FLL_LIVE_USER_HOME}/Desktop/${FLL_DISTRO_NAME}.desktop"
    chown ${FLL_LIVE_USER} "${FLL_LIVE_USER_HOME}/Desktop/${FLL_DISTRO_NAME}.desktop"
    chgrp ${FLL_LIVE_USER} "${FLL_LIVE_USER_HOME}/Desktop/${FLL_DISTRO_NAME}.desktop"
echo "Release Notes scheduled for start." 
fi
}

set_hidpi() {
    RESOLUTION=$(fbset -s | awk -F '"' 'FNR == 2 {print $2}'| cut -c -4)
    echo Resolution: $RESOLUTION > /tmp/livesettings.txt
    if [ "$RESOLUTION" -ge 3440 ]; then
for file in /home/${FLL_LIVE_USER}/.config/kdeglobals /etc/skel/.config/kdeglobals
do
cat <<'EOF' > "$file"
[KScreen]
ScaleFactor=2
ScreenScaleFactors=-1eDP-1=2;HDMI-1=2;DP-1=2;HDMI-2=2;DP-2=2;HDMI-3=2;DP-3=2;HDMI-4=2;DP-4=2;Virtual-1=2;
EOF
done

for file in /home/${FLL_LIVE_USER}/.config/kcmfonts /etc/skel/.config/kcmfonts
do
cat <<'EOF' > "$file"
[General]
forceFontDPI=192
EOF
done

for file in /home/${FLL_LIVE_USER}/.config/kcminputrc /etc/skel/.config/kcminputrc
do
cat <<'EOF' > "$file"
[Mouse]
cursorSize=48
EOF
done

cat <<'EOF' >> /etc/sddm.conf.d/kde_settings.conf

[X11]
EnableHiDPI=true
ServerArguments=-dpi 192
EOF
    else
        if [ "$RESOLUTION" -ge 2040 ]; then
for file in /home/${FLL_LIVE_USER}/.config/kdeglobals /etc/skel/.config/kdeglobals
do
cat <<'EOF' > "$file"
[KScreen]
ScaleFactor=1.5
ScreenScaleFactors=eDP-1=1.5;HDMI-1=1.5;DP-1=1.5;HDMI-2=1.5;DP-2=1.5;HDMI-3=2;DP-3=1.5;HDMI-4=1.5;DP-4=1.5;Virtual-1=1.5;
EOF
done

for file in /home/${FLL_LIVE_USER}/.config/kcmfonts /etc/skel/.config/kcmfonts
do
cat <<'EOF' > "$file"
[General]
forceFontDPI=144
EOF
done

for file in /home/${FLL_LIVE_USER}/.config/kcminputrc /etc/skel/.config/kcminputrc
do
cat <<'EOF' > "$file"
[Mouse]
cursorSize=36
EOF
done

cat <<'EOF' >> /etc/sddm.conf.d/kde_settings.conf

[X11]
EnableHiDPI=true
ServerArguments=-dpi 144
EOF
        else
            echo kein HiDPI > /tmp/livesettings-2-txt
        fi
    fi
    chown -R ${FLL_LIVE_USER}.${FLL_LIVE_USER} /home/${FLL_LIVE_USER}
}

hack_kde()
{
        if [ $LANG = "de_DE.UTF-8" ] || [ $LANG = "de_DE.utf8" ]; then
            cat > /home/tux/.config/plasma-localerc <<EOF
[Formats]
LANG=de_DE.UTF-8

[Translations]
LANGUAGE=de:en_US
EOF
        fi
	mkdir -p /home/${FLL_LIVE_USER}/.config/autostart/
	cp -a /usr/share/siduction/user-dirs.desktop /home/${FLL_LIVE_USER}/.config/autostart/
	chown -R ${FLL_LIVE_USER}.${FLL_LIVE_USER} /home/${FLL_LIVE_USER}
}

do_start()
{
if [ -z "${NOINTRO}" ]; then
	no_intro
fi
}

case "${1}" in
    start)
        log_daemon_msg "${NAME}"
	set_hidpi
	hack_kde
        do_start
        log_end_msg "${?}"
        ;;
    stop)
        ;;
    restart|force-reload)
        echo "Error: argument '${1}' not supported" >&2
        exit 3
        ;;
    *)
        echo "Usage: ${NAME} {start|stop}" >&2
        exit 3
        ;;
esac
