pre_install() {

	if pgrep -f '/opt/aimp/AIMP' > /dev/null; then
		echo "Terminating app instance..."
		pkill -f '/opt/aimp/AIMP' || true
		sleep 1
	fi

}

post_install() {
	gtk-update-icon-cache -q -t -f usr/share/icons/hicolor
	chmod 666 /opt/aimp/AIMP.ini
	chmod 777 /opt/aimp/Plugins/

	xdg-mime install --novendor /opt/aimp/System/FileFormats.xml
	#xdg-desktop-icon install --novendor "/usr/share/applications/aimp.desktop"
	update-desktop-database -q
}

pre_upgrade() {
	pre_install
}

post_upgrade() {
	post_install
}

pre_remove() {

	if pgrep -f '/opt/aimp/AIMP' > /dev/null; then
		echo "Terminating app instance..."
		pkill -f '/opt/aimp/AIMP' || true
		sleep 1
	fi

	if [ -f "/usr/share/applications/aimp.desktop" ]; then
		xdg-desktop-icon uninstall --novendor "/usr/share/applications/aimp.desktop"
	fi

	if [ -f "/opt/aimp/System/FileFormats.xml" ]; then
		xdg-mime uninstall "/opt/aimp/System/FileFormats.xml"
	fi

}

post_remove() {
	gtk-update-icon-cache -q -t -f usr/share/icons/hicolor
	echo "postrm: $1"

	# DEB: remove, RPM: 0
	if [ "$1" = "remove" ] || [ "$1" = "0" ];
	then
		echo "> removing..."
		rm -f /usr/bin/aimp
		rm -f /usr/lib64/libbass-aimp.so
		rm -f /usr/lib64/libav*-aimp.so*
		rm -f /usr/lib64/libsw*-aimp.so*
		rm -f /usr/lib/x86_64-linux-gnu/libbass-aimp.so
		rm -f /usr/lib/x86_64-linux-gnu/libav*-aimp.so*
		rm -f /usr/lib/x86_64-linux-gnu/libsw*-aimp.so*
		rm -f -d -r /opt/aimp/	
	fi
	update-desktop-database -q
}
