#!/bin/sh

###
# F.U.L.L.S.T.O.R.Y init script
#
# Copyright: (C) 2007-2008 Kel Modderman <kel@otaku42.de>
# Copyright: (C) 2008-2016 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-kde-desktop, inherit its environment
NAME="fll-xfce-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

###
# read in variables from our default conffile
###
if [ -s "/etc/default/${NAME}" ]; then
	. "/etc/default/${NAME}"
fi

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

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

###
# set wallpaper defaults in desktop.xml
###
mkdir -p "${HOME}/.config/xfce4/xfconf/xfce-perchannel-xml"
XFCE_SETTINGS_XML="${HOME}/.config/xfce4/xfconf/xfce-perchannel-xml/xsettings.xml"
XFCE_COLOURS="808080"

mkdir -p "${HOME}/.config/xfce4/panel/"
if [ -d /usr/share/siduction-settings-xfce ]; then
	cp -r /usr/share/siduction-settings-xfce/xfce4 ${HOME}/.config
	sed -i -e s/FLL_LIVE_USER/${FLL_LIVE_USER}/ ${HOME}/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-panel.xml
fi

mkdir -p "${HOME}/.config/xfce4/xfconf/xfce-perchannel-xml"
XFCE_DESKTOP_XML="${HOME}/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-desktop.xml"

# handle case where FLL_WALLPAPER is defined in kde4 directory syntax
# siduction-art-wallpaper-* provides a symlink in /usr/share/wallpapers/ to common res wallpaper
if [ -d ${FLL_WALLPAPER} ]; then
	FLL_WALLPAPER=$(find $(dirname ${FLL_WALLPAPER}) -maxdepth 1 -type l -iname $(basename ${FLL_WALLPAPER}).*)
fi

if [ "${FLL_WALLPAPER}" ] && [ ! -f "${XFCE_DESKTOP_XML}" ]; then

	cat > "${XFCE_DESKTOP_XML}" << EOF
<?xml version="1.0" encoding="UTF-8"?>

<channel name="xfce4-desktop" version="1.0">
  <property name="backdrop" type="empty">
    <property name="screen0" type="empty">
      <property name="monitor0" type="empty">
        <property name="image-path" type="string" value="${FLL_WALLPAPER}"/>
        <property name="image-style" type="int" value="3"/>
      </property>
    </property>
  </property>
</channel>
EOF
fi

###
# create .config/autostart dir
###
mkdir -p "${HOME}/.config/autostart"

###
# disable tips on startup
###
FLL_CONF_PRESEED="${HOME}/.config/autostart/xfce4-tips-autostart.desktop"
if grep -s -q '^Hidden=' "${FLL_CONF_PRESEED}"; then
	sed -i 's|^Hidden=.*|Hidden=true|' "${FLL_CONF_PRESEED}"
else
	cat > "${FLL_CONF_PRESEED}" <<EOF

[Desktop Entry]
Hidden=true

EOF
fi

###
# set xfce splash login theme
###
FLL_CONF_PRESEED="${HOME}/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-session.xml"
if [ "${XFCE_SPLASH_THEME}" ]; then
	mkdir -p "${HOME}/.config/xfce4/xfconf/xfce-perchannel-xml"

	if [ ! -f "${FLL_CONF_PRESEED}" ]; then
	        cat > "${FLL_CONF_PRESEED}" << EOF
<?xml version="1.0" encoding="UTF-8"?>
 
<channel name="xfce4-session" version="1.0">
  <property name="splash" type="empty">
    <property name="Engine" type="string" value="${XFCE_SPLASH_ENGINE}"/>
    <property name="engines" type="empty">
      <property name="balou" type="empty">
        <property name="Theme" type="string" value="${XFCE_SPLASH_THEME}"/>
      </property>
    </property>
  </property>
</channel>
EOF
	fi
fi

###
# take care of the trash can
###
mkdir -p "${HOME}/.local/share/Trash/info/" "${HOME}/.local/share/Trash/files/"

###
# create ~/Desktop if not present
###
mkdir -p "${HOME}/Desktop"
