#!/bin/sh

###############################################################################
# UExtract v4.16 by JakeSFR (a.k.a. SFR)'2013-2025                            #
# GNU GPL v2 applies                                                          #
###############################################################################

# PPM may have problems with deleting /usr/local/apps/UExtract/* empty (sub)dir(s),
# so let's do it manually (or not?):
#find /usr/local/apps/UExtract/ -type d -empty -delete

# -----------------------------------------------------------------------------
# Dissociate ROX 'SendTo/OpenWith' and/or Thunar with UExtract
# -----------------------------------------------------------------------------

USER_HOME="$(eval echo ~${SUDO_USER})"	# XBPS

APP_NAME="UExtract"
APP_PATH="/usr/local/apps/UExtract"

### ROX ###

for i in "/etc/xdg/rox.sourceforge.net/SendTo" \
		"$USER_HOME/.config/rox.sourceforge.net/SendTo" \
		"$USER_HOME/.config/rox.sourceforge.net/OpenWith" \
		"/root/.config/rox.sourceforge.net/OpenWith"
do
	if [ -d "${i}" ]; then
		find "$(readlink -f "$i")" -type l -name "$APP_NAME" -delete
		# Delete all empty dirs as well (have to rethink it yet)
		#find "$(readlink -f "$i")" -type d -empty -delete
	fi
done

# Remove icon from ROX pinboard and Desktop folder
[ -e "$USER_HOME/Choices/ROX-Filer/PuppyPin" ] ||
[ -e "$USER_HOME/.config/rox.sourceforge.net/ROX-Filer/PuppyPin" ] &&
rox --RPC << EOF
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
	<env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
		<PinboardRemove>
			<Path>${APP_PATH}</Path>
			<Label>${APP_NAME}</Label>
		</PinboardRemove>
	</env:Body>
</env:Envelope>
EOF

rm -f "${USER_HOME}/Desktop/UExtract.desktop"

# -----------------------------------------------------------------------------

### Thunar ###

CONFIG="$USER_HOME/.config/Thunar/uca.xml"
CONFIG_BAK="${CONFIG}.bak"

if [ -f "$CONFIG" ]; then
	cp -af "$CONFIG" "$CONFIG_BAK"
	
	sed -e "/<action>/i\ " -e "/<\/action>/a\ " -e "/<\/actions>/a\ " "$CONFIG_BAK" | \
		sed "/<action>/,/<\/action>/{H;d;};x;/<name>${APP_NAME}<\/name>/d" | \
		sed "/^ *$/d" > "$CONFIG"
fi

# =============================================================================
