#!/bin/sh

###
# F.U.L.L.S.T.O.R.Y init script
#
# Copyright: (C) 2007-2008 Kel Modderman <kel@otaku42.de>
# Copyright: (C) 2007 Niall Walsh <niallwalsh@users.berlios.de>
# Copyright: (C) 2008-2018 Stefan Lippers-Hollmann <s.l-h@gmx.de>
# License:   GPLv2
#
# F.U.L.L.S.T.O.R.Y Project Homepage:
# https://github.com/fullstory
###

PATH=/sbin:/usr/sbin:$PATH

# set default umask
umask 0022

# this is only invoked from fll-common-desktop, inherit its environment
NAME="fll-common-desktop"

###
# source distro-defaults, no-op unless in live mode
###
FLL_DISTRO_MODE="installed"

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

if [ "${FLL_DISTRO_MODE}" != "live" ]; then
	exit 0
fi

###
# cheatcode handling
###
for param in $(cat /proc/cmdline); do
	case "${param}" in
		flldebug=*)
			[ "${param#flldebug=}" = "${NAME}" ] && set -x
			;;
		nointro)
			NOINTRO="yes"
			;;
	esac
done

###
# su-to-root -> sudo
###
if which su-to-root >/dev/null; then
	touch "${HOME}/.su-to-rootrc"
	if grep -q '^SU_TO_ROOT_SU=' "${HOME}/.su-to-rootrc"; then
		sed -i 's|^SU_TO_ROOT_SU=.*|SU_TO_ROOT_SU=sudo|' \
			"${HOME}/.su-to-rootrc"
	else
		echo 'SU_TO_ROOT_SU=sudo' >> "${HOME}/.su-to-rootrc"
	fi
fi

###
# kdesu -> sudo
###

# KDE4-kdesu, still needed...
if [ -x /usr/lib/kde4/libexec/kdesu ]; then
	mkdir -p "${HOME}/.kde/share/config"
	if grep -s -q '^super-user-command=' "${HOME}/.kde/share/config/kdesurc"; then
		sed -i 's|^super-user-command=.*|super-user-command=sudo|' \
			"${HOME}/.kde/share/config/kdesurc"
	else
		cat > "${HOME}/.kde/share/config/kdesurc" \
<<EOF
[super-user-command]
super-user-command=sudo
EOF
	fi
fi

# KDE5-kdesu, not complete without its KDE4 counterpart...
if [ -x /usr/lib/*/libexec/kf5/kdesu ]; then	# XXX: nasty-nasty
	mkdir -p "${HOME}/.config"
        if grep -s -q '^super-user-command=' "${HOME}/.config/kdesurc"; then
		sed -i 's|^super-user-command=.*|super-user-command=sudo|' \
			"${HOME}/.config/kdesurc"
	else
		cat > "${HOME}/.config/kdesurc" \
<<EOF
[super-user-command]
super-user-command=sudo
EOF
	fi
fi

###
# let mc use its own editor
###
if which mc >/dev/null; then
	if [ ! -f "${HOME}/.mc/ini" ]; then
		mkdir -p "${HOME}/.mc/"
		cat >"${HOME}/.mc/ini" <<EOF

[Midnight-Commander]
use_internal_edit=1
EOF
	fi
fi

###
## create ~/Desktop if not present
####
if [ ! -d "${HOME}/Desktop" ]; then
	mkdir -p "${HOME}/Desktop"
fi

###
## put a manual icon on the desktop
####
if [ ! -f "${HOME}/Desktop/aptosid-manual.desktop" ] && \
   [ -f /usr/share/applications/aptosid/aptosid-manual.desktop ]; then
	install -o ${FLL_LIVE_USER} -g ${FLL_LIVE_USER} -m 0755 \
		/usr/share/applications/aptosid/aptosid-manual.desktop \
		"${HOME}/Desktop/"
fi

###
## put a aptosid-irc icon on the desktop
####
if [ ! -f "${HOME}/Desktop/aptosid-irc.desktop" ] && \
   [ -f /usr/share/applications/aptosid/aptosid-irc.desktop ]; then
	install -o ${FLL_LIVE_USER} -g ${FLL_LIVE_USER} -m 0755 \
		/usr/share/applications/aptosid/aptosid-irc.desktop \
		"${HOME}/Desktop/"
fi

###
## put a installer-gui-irc icon on the desktop
####
if [ ! -f "${HOME}/Desktop/install-gui.desktop" ] && \
   [ -f /usr/share/applications/aptosid/install-gui.desktop ]; then
	install -o ${FLL_LIVE_USER} -g ${FLL_LIVE_USER} -m 0755 \
		/usr/share/applications/aptosid/install-gui.desktop \
		"${HOME}/Desktop/"
fi

###
# scan for index.html in fll specific live mountpoint directories
###
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 "${HOME}/Desktop/${FLL_DISTRO_NAME}.desktop" ]; then
	cat > "${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=false

Name=${FLL_CDROM_INDEX}
GenericName=${FLL_CDROM_INDEX}
EOF
chmod u+x "${HOME}/Desktop/${FLL_DISTRO_NAME}.desktop"

	if [ "${NOINTRO}" != "yes" ]; then
		mkdir -p "${HOME}/.config/autostart"

		install -o ${FLL_LIVE_USER} -g ${FLL_LIVE_USER} -m 0744 \
			"${HOME}/Desktop/${FLL_DISTRO_NAME}.desktop" \
			"${HOME}/.config/autostart/"
	fi
elif [ -e "${HOME}/.config/autostart/${FLL_DISTRO_NAME}.desktop" ]; then
	# toram + persistency
	rm -f "${HOME}/.config/autostart/${FLL_DISTRO_NAME}.desktop"
fi

