#!/bin/sh
# Simple script which configure Xfce environment for a fresh user
# 2012 Author: tpg@mandriva.org
# 2014 Author: tpgxyz@gmail.com
# 2021 Author: mikhailnov <m.novosyolov@rosalinux.ru>
# Licensed under GPL terms

set -efu

# https://specifications.freedesktop.org/basedir-spec/latest/ar01s03.html
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
AUTOSTART_DIR="${XDG_CONFIG_HOME}/autostart"
DESKTOP_FILE="${AUTOSTART_DIR}/xfce4-firstrun.desktop"

[ ! -e "$DESKTOP_FILE" ] || exit 0

# This will:
# - start flameshot if it is not running (and it is not on the first start up)
# - create ~/.config/autostart/Flameshot.desktop
# Started flameshot will be kept in tray and will apeear there automatically on the next start
if [ -x /usr/bin/flameshot ]; then
	# dbus-send would start flameshot, but the problem is that it will be started via DBus
	# and will not inherit environmental variables of this desktop session — Qt will show
	# its own file dialog when saving a screenshot instead of automatically using GTK+3 dialog,
	# so let's start flameshot by ourselves
	if ! pgrep --ns $$ --uid "$(id -u)" flameshot >/dev/null; then
		flameshot &
		sleep 2
	fi
	dbus-send --dest=org.flameshot.Flameshot / org.flameshot.Flameshot.autostartEnabled boolean:true
	# For some reasons dbus interface creates the desktop file not always,
	# workaround it by creating the same file manually
	if [ ! -f "${AUTOSTART_DIR}/Flameshot.desktop" ]; then
		cat > "${AUTOSTART_DIR}/Flameshot.desktop" << EOF
[Desktop Entry]
Name=flameshot
Icon=flameshot
Exec=flameshot
Terminal=false
Type=Application
X-GNOME-Autostart-enabled=true
EOF
	fi
fi

mkdir -p "$AUTOSTART_DIR"

cat > "$DESKTOP_FILE" << EOF
[Desktop Entry]
Hidden=true
EOF
