#!/bin/bash
# -*- ENCODING: UTF-8 -*-

# rese
#
# Recover desktop session.
#
# Copyright (c) 2013-2026: Alexis Puente Montiel   < pica (a) picalibre.org >
#
# Licensed according to GNU AGPL version 3.0.
#
# It is libre/free software; you can use, redistribute and/or modify it according to the terms of GNU AGPL as published by GNU, version 3.0, 19 November 2007.
#
# It is distributed in the hope that it will be useful, but without any warranty. Read GNU AGPL version 3.0 for additional details.
#
# A copy of GNU AGPL version 3.0 is available at /usr/share/doc/<software-package-name>/agpl-3.0.txt (additionally on Internet as text at https://www.gnu.org/licenses/agpl-3.0.txt and as HTML at https://www.gnu.org/licenses/agpl-3.0-standalone.html ).
#
# Note: Additionally to the official e-mails, picalibre.org is strictly the only official site for this software project, please consider using it to download, report bugs and contribute.
#
# Depends: bash, coreutils, findutils, grep, hostname, sed, xfce4-session, xfwm4
# Recommends: x11-xkb-utils


### SCRIPT VARIABLES ########################################

CNAME="rese"
VERSION="1.3.1"
TITLE="'rese'"
ICON="/usr/share/icons/rese.png"

# Translations
if [ "$LANG" = "" ] ; then export $(cat /etc/default/locale | grep -a 'LANG=') ; fi
TEXTDOMAIN=pica-skel
TEXTDOMAINDIR=/usr/share/locale/

# Write errors to log
ERRORLOG="$HOME/.${CNAME}.log"
if [ -e "$ERRORLOG" ] ; then
	mv -f $ERRORLOG ${ERRORLOG}.ant
fi
if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi

for i in /etc/pica-global.dist /etc/pica-global.orig /etc/pica-global /etc/pica-global.local ~/.pica-global ~/pica-global ; do
	if [ -f "$i" ] ; then
	cat "$i"
	source "$i"
	source <(cat $i | sed -e "s/=\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)/=Y/g" -e "s/=\(No\|no\|n\)/=N/g" -e "s/=\"\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)\"/=Y/g" -e "s/=\"\(No\|no\|n\)\"/=N/g")
	fi
done
if [ "$DEBUG" = "Y" ] ; then
	set -xv
	DEBUG="Y"
else
	ERRORLOG="/tmp/.${CNAME}_$(id -nu).log"
	if [ -e "$ERRORLOG" ] ; then mv -f $ERRORLOG ${ERRORLOG}.ant ; fi
	if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi
fi
if [ "$DEBUG" = "" ] ; then DEBUG="N" ; fi
if [ "$DEBUG" != "N" ] ; then
exec > >(tee -a "$ERRORLOG") 2>&1
echo "$0" "$*" >> "$ERRORLOG"
echo "$CNAME" "$VERSION" >> "$ERRORLOG"
echo $(date +%Y-%m-%d_%H:%M:%S) $"Start" >> "$ERRORLOG"
echo "env:" >> "$ERRORLOG"
env >> "$ERRORLOG"
echo "set:" >> "$ERRORLOG"
set >> "$ERRORLOG"
#else
#exec 2>>"$ERRORLOG"
fi

# Description:
BDESCRIP=$"Recover desktop session."
LDESCRIP=$"$TITLE is a tool to recover the previous desktop session."

# Documentation:
docu_info () {
echo "$CNAME ($VERSION) - $BDESCRIP"
echo 
echo $"Usage:" $CNAME [$"OPTIONS"]
echo 
echo $"Options:"
echo -e "$ODESCRIP"
echo 
echo $"'man $CNAME' for more information."
echo 
}
ODESCRIP=" -x""\t"$"Show help documentation."

while getopts x OPTION ; do
	case $OPTION in
		x )   docu_info ; exit 0 ;;
	esac
done


### SCRIPT ########################################

	if [ "$(env | grep -aEi "^DESKTOP_SESSION=|^XDG_SESSION_DESKTOP=|^XDG_CURRENT_DESKTOP=" | grep -aEi xfce)" != "" ] || [ "$(set | grep -aEi "^DESKTOP_SESSION=|^XDG_SESSION_DESKTOP=|^XDG_CURRENT_DESKTOP=" | grep -aEi xfce)" != "" ] ; then

		echo $"$TITLE is a tool to recover the previous desktop session. $TITLE does not work if there is a XFCE desktop session running. You must close any XFCE desktop session."
		
		echo $"Maybe you want to reopen the 'Thunar' file manager with the last tabs? "
		echo 0 $"No"
		printf "\r%-77.77s" $"Please wait..." ; printf "\r"
#		N=0 ; for f in $(find $HOME/.cache/ -type f | grep -aEi sessions | grep -aEi thunar ) ; do N=$(expr $N + 1) ; export N=$N ; declare THUNAR$N="$(cat "$f" | grep -aE URI | sed 's|URI=||g' | sed 's|;| |g')" ; export THUNAR$N="$(eval echo "\$THUNAR$N")" ; echo $N $(eval echo "\$THUNAR$N") ; done  # With replications
#		N=0 ; rm /tmp/rese ; for f in $(find $HOME/.cache/ -type f | grep -aEi sessions | grep -aEi thunar ) ; do N=$(expr $N + 1) ; export N=$N ; prop="$(cat "$f" | grep -aE URI | sed 's|URI=||g' | sed 's|;| |g')" ; md5prop="$(echo "$prop" | md5sum)" ; if [ "$(cat /tmp/rese | grep -a "$md5prop")" = "" ] ; then declare THUNAR$N="$(cat "$f" | grep -aE URI | sed 's|URI=||g' | sed 's|;| |g')" ; export THUNAR$N="$(eval echo "\$THUNAR$N")" ; echo $N $(eval echo "\$THUNAR$N") ; echo "$md5prop" >> /tmp/rese ; fi ; done ; rm /tmp/rese  # Using write to file
		N=0 ; unset arraymd5 ; for f in $(find $HOME/.cache/ -type f | grep -aEi sessions | grep -aEi thunar ) ; do N=$(expr $N + 1) ; export N=$N ; prop="$(cat "$f" | grep -aE URI | sed 's|URI=||g' | sed 's|;| |g')" ; md5prop="$(echo "$prop" | md5sum)" ; if [ "$(echo ${arraymd5[@]} | grep -a "$(echo $md5prop)")" = "" ] ; then declare THUNAR$N="$(cat "$f" | grep -aE URI | sed 's|URI=||g' | sed 's|;| |g')" ; export THUNAR$N="$(eval echo "\$THUNAR$N")" ; echo $N $(eval echo "\$THUNAR$N") ; arraymd5+=("$md5prop") ; fi ; done ; unset arraymd5  # Using array
		read -p $"Select a number: " SEL
		if [ "$SEL" -gt 0 2>/dev/null ] ; then
			nohup thunar $(eval echo "\$THUNAR$SEL") 1>&2 2>>"$ERRORLOG" >> "$ERRORLOG" &
			sleep 0.2
		else
			exit 1
		fi

	else

XFCESE="$(ls "$HOME/.cache/sessions/" | grep -aEi "^xfce4-session-.*:0$")"

if [ "$(cat "$HOME/.cache/sessions/$XFCESE" | grep -aEi xfwm4)" != "" ] ; then

cd "$HOME/.cache/sessions/" ; ls -FAtr | grep -aEi "\.bak$" | while read i ; do cp -a --remove-destination "$i" "$(echo $i | sed "s|\.bak||g")" ; done

else

cd "$HOME/.cache/sessions/"

cp -a --remove-destination "$XFCESE" "$XFCESE".bak2

cp -a --remove-destination "$XFCESE" "$XFCESE"_pica

cat > "$XFCESE" <<EOF
[Session: Default]
Client0_ClientId=$(ls -FAtr | grep -aEi "^xfwm4-" | tail -n 1 | sed "s|^xfwm4-||g" | sed "s|.state$||g")
Client0_Hostname=local/$(hostname)
Client0_CloneCommand=xfwm4
Client0_DiscardCommand=rm,-rf,$HOME/.cache/sessions/xfwm4-$(ls -FAtr | grep -aEi "^xfwm4-" | tail -n 1 | sed "s|^xfwm4-||g" | sed "s|.state$||g").state
Client0_RestartCommand=xfwm4,--display,:0.0,--sm-client-id,$(ls -FAtr | grep -aEi "^xfwm4-" | tail -n 1 | sed "s|^xfwm4-||g" | sed "s|.state$||g")
Client0_CurrentDirectory=$HOME
Client0_Program=xfwm4
Client0_UserId=$(echo $HOME | sed 's|/home/||g')
Client0_Priority=15
Client0_RestartStyleHint=2
EOF

NN=$(cat "$XFCESE"_pica | grep -aEi "^Client" | sed "s|_.*||g" | sort -V | tail -n 1 | sed "s|Client||g")

for i in $(seq $NN -1 0) ; do sed -i "s|^Client${i}_|Client$(expr $i + 1)_|g" "$XFCESE"_pica ; done

cat "$XFCESE"_pica | grep -aEiv "^\[Session: Default\]" >> "$XFCESE"

rm -f "$XFCESE"_pica

fi

		echo $"Done. If it continues without working, you will have to completely delete the hidden folder/directory $HOME/.cache/sessions/ "

	fi

exit 0
