#!/bin/bash
domesticVersion=0
if [ -e '/usr/share/mime/packages/custom-wps-office.xml' ]; then
	domesticVersion=1
fi
common_word_mimes=('text/rtf' 'application/rtf' 'application/msword' 'application/x-msword' 'application/wordperfect' 'application/vnd.ms-word' 'application/msword-template' 'application/vnd.wordperfect' 'application/vnd.sun.xml.writer' 'application/vnd.sun.xml.writer.global' 'application/vnd.sun.xml.writer.template' 'application/vnd.oasis.opendocument.text' 'application/vnd.oasis.opendocument.text-template' 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' 'application/vnd.openxmlformats-officedocument.wordprocessingml.template')

common_ppt_mimes=('application/powerpoint' 'application/mspowerpoint' 'application/x-mspowerpoint' 'application/vnd.mspowerpoint' 'application/vnd.ms-powerpoint' 'application/vnd.sun.xml.impress' 'application/vnd.stardivision.impress' 'application/vnd.sun.xml.impress.template' 'application/vnd.oasis.opendocument.presentation' 'application/vnd.oasis.opendocument.presentation-template' 'application/vnd.openxmlformats-officedocument.presentationml.slide' 'application/vnd.openxmlformats-officedocument.presentationml.template' 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' 'application/vnd.openxmlformats-officedocument.presentationml.presentation')

common_excel_mimes=('text/csv' 'application/xls' 'application/csv' 'text/spreadsheet' 'application/x-123' 'application/x-xls' 'application/excel' 'application/x-oleo' 'application/msexcel' 'application/x-msexcel' 'application/x-ms-excel' 'application/vnd.ms-excel' 'application/x-quattropro' 'application/x-planperfect' 'application/x-dos_ms_excel' 'text/comma-separated-values' 'application/vnd.sun.xml.calc' 'text/x-comma-separated-values' 'application/x-applix-spreadsheet' 'application/tab-separated-values' 'application/vnd.stardivision.calc' 'application/vnd.sun.xml.calc.template' 'application/vnd.oasis.opendocument.spreadsheet' 'application/vnd.oasis.opendocument.spreadsheet-template' 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 'application/vnd.openxmlformats-officedocument.spreadsheetml.template' 'application/vnd.lotus-1-2-3' 'application/x-excel' 'application/x-mps' 'application/x-sc' 'application/x-sylk' 'application/x-xbase' 'text/tab-separated-values' 'zz-application/zz-winassoc-xls')

common_pdf_mimes=('application/pdf' 'application/wps-office.pdf')
common_ofd_mimes=('application/wps-office.ofd')
common_flow_mimes=('application/wps-office.pos')
common_mind_mimes=('application/wps-office.pof')

wps_word_mimes=('application/wps-office.doc' 'application/wps-office.dot' 'application/wps-office.dotx' 'application/wps-office.docx' 'application/wps-office.wps' 'application/wps-office.wpt' 'application/wps-office.wpss' 'application/wps-office.wpso')
wps_ppt_mimes=('application/wps-office.ppt' 'application/wps-office.pptx' 'application/wps-office.pot' 'application/wps-office.potx' 'application/wps-office.dps' 'application/wps-office.dpt' 'application/wps-office.dpss' 'application/wps-office.dpso')
wps_excel_mimes=('application/wps-office.xls' 'application/wps-office.xlt' 'application/wps-office.xlsx' 'application/wps-office.xltx' 'application/wps-office.et' 'application/wps-office.ett' 'application/wps-office.ets' 'application/wps-office.eto')

function restore_mimeapp_config()
{
	mimetype=$1
	defaulttype=$2
	configFilePath=$3
	touch $configFilePath
	if type python3 >/dev/null 2>&1 ; then
	python3 - <<END
import configparser
section = 'Default Applications'
inifilepath = '$configFilePath'
config = configparser.ConfigParser(strict=False)
config.read(inifilepath)
allmime = ""
for indexc in '$mimetype':
	allmime += indexc
for onemime in allmime.split(' '):
	if config.has_section(section):
		if config.has_option(section,'$mimetype'):
			appstr = config[section]['$mimetype']
			apps = appstr.split(';')
			appcount = len(apps)
			if appcount == 1:
				if apps[0] == '$defaulttype':
					config.remove_option(section,'$mimetype')
			else:
				if '$defaulttype' in apps:
					apps.remove('$defaulttype')
				appstr = '++'.join(apps)
				config[section]['$mimetype'] = appstr
	with open(inifilepath, 'w') as configfile:
		config.write(configfile, space_around_delimiters=False)
END
	elif type python2 >/dev/null 2>&1 ; then
	python2 - <<END
import ConfigParser
section = 'Default Applications'
inifilepath = '$configFilePath'
config = ConfigParser.ConfigParser()
config.read(inifilepath)
for indexc in '$mimetype':
	allmime += indexc
for onemime in allmime.split(' '):
	if config.has_section(section):
		if config.has_option(section,'$mimetype'):
			appstr = config.get(section, '$mimetype')
			apps = appstr.split(';')
			appcount = len(apps)
			if appcount == 1:
				if apps[0] == '$defaulttype':
					config.remove_option(section,'$mimetype')
			else:
				if '$defaulttype' in apps:
					apps.remove('$defaulttype')
				appstr = '++'.join(apps)
				config.set(section, '$mimetype', appstr)
	with open(inifilepath, 'w') as configfile:
		config.write(configfile)
END
        fi
}

if [ ! -f "/usr/share/mime/packages/wps-office-wps.xml" ]; then 
	touch /usr/share/mime/packages/custom-wps-office.xml
fi

if [ -f "/usr/bin/quickstartoffice" ]; then
	if [ -f "/etc/profile" ]; then
		sed -i '/quickstartoffice/d' /etc/profile
	fi
	if [ -f "/root/.bash_logout" ]; then
		sed -i '/quickstartoffice/d' /root/.bash_logout
	fi
	/usr/bin/quickstartoffice stop
fi


gTemplatesName="模板"
if [ -d "/root/模板" ]; then
	gTemplatesName="模板"
elif [ -d "/root/Templates" ]; then
	gTemplatesName="Templates"
fi

gDesktopName="桌面"
if [ -d "/root/桌面" ]; then
	gDesktopName="桌面"
elif [ -d "/root/Desktop" ]; then
	gDesktopName="Desktop"
fi

rm -rf /root/.kingsoft/office6/cache/*
if [ -d "/root/${gDesktopName}" ]; then
	rm -rf /root/${gDesktopName}/wps-office-wpp.desktop
	rm -rf /root/${gDesktopName}/wps-office-wps.desktop
	rm -rf /root/${gDesktopName}/wps-office-et.desktop
	rm -rf /root/${gDesktopName}/wps-office-pdf.desktop
	rm -rf /root/${gDesktopName}/wps-office-ofd.desktop
	rm -rf /root/${gDesktopName}/wps-office-flow.desktop
	rm -rf /root/${gDesktopName}/wps-office-mind.desktop
	rm -rf /root/${gDesktopName}/wps-office-official.desktop
	rm -rf /root/${gDesktopName}/wps-office-prometheus.desktop
	rm -rf /root/${gDesktopName}/wps-office-officeassistant.desktop
fi

rm -rf "/root/.local/share/Kingsoft/account"
rm -rf "/root/.local/share/Kingsoft/WPS Cloud Files"

for templateName in "/opt/kingsoft/wps-office/templates/"*
do
	rm -rf "/root/${gTemplatesName}/$(basename "$templateName")"
	rm -rf "/etc/skel/${gTemplatesName}/$(basename "$templateName")"
done

for FILENAME in /home/*; do

	if [ -f "${FILENAME}/.config/user-dirs.dirs" ]; then
		source "${FILENAME}/.config/user-dirs.dirs"
		if [ ! -d "${XDG_DESKTOP_DIR}" ]; then
			mkdir -p "${XDG_DESKTOP_DIR}" >/dev/null 2>&1 || true
		fi
		gDesktopName="${XDG_DESKTOP_DIR//${HOME}\//}"
		if [ ! -d "${XDG_TEMPLATES_DIR}" ]; then
			mkdir -p "${XDG_TEMPLATES_DIR}" >/dev/null 2>&1 || true
		fi
		gTemplatesName="${XDG_TEMPLATES_DIR//${HOME}\//}"
	else 
		if [ -d "${FILENAME}/桌面" ]; then
			gDesktopName="桌面"
		elif [ -d "${FILENAME}/Desktop" ]; then
			gDesktopName="Desktop"
		fi

		if [ -d "${FILENAME}/模板" ]; then
			gTemplatesName="模板"
		elif [ -d "${FILENAME}/Templates" ]; then
			gTemplatesName="Templates"
		fi
	fi

	rm -rf ${FILENAME}/.kingsoft/office6/cache/*

	if [ -f  "${FILENAME}/${gDesktopName}/wps-office-wpp.desktop" ]; then
		rm -rf "${FILENAME}/${gDesktopName}/wps-office-wpp.desktop"
	fi
	if [ -f  "${FILENAME}/${gDesktopName}/wps-office-wps.desktop" ]; then
		rm -rf "${FILENAME}/${gDesktopName}/wps-office-wps.desktop"
	fi
	if [ -f "${FILENAME}/${gDesktopName}/wps-office-et.desktop" ]; then
		rm -rf "${FILENAME}/${gDesktopName}/wps-office-et.desktop"
	fi
	if [ -f "${FILENAME}/${gDesktopName}/wps-office-pdf.desktop" ]; then
		rm -rf "${FILENAME}/${gDesktopName}/wps-office-pdf.desktop"
	fi
	if [ -f "${FILENAME}/${gDesktopName}/wps-office-ofd.desktop" ]; then
		rm -rf "${FILENAME}/${gDesktopName}/wps-office-ofd.desktop"
	fi
	if [ -f "${FILENAME}/${gDesktopName}/wps-office-flow.desktop" ]; then
		rm -rf "${FILENAME}/${gDesktopName}/wps-office-flow.desktop"
	fi
	if [ -f "${FILENAME}/${gDesktopName}/wps-office-mind.desktop" ]; then
		rm -rf "${FILENAME}/${gDesktopName}/wps-office-mind.desktop"
	fi
	rm -rf ${FILENAME}/${gDesktopName}/wps-office-prometheus.desktop
	rm -rf ${FILENAME}/${gDesktopName}/wps-office-official.desktop
	rm -rf ${FILENAME}/${gDesktopName}/wps-office-officeassistant.desktop
	for templateName in "/opt/kingsoft/wps-office/templates/"*
	do
		rm -rf "${FILENAME}/${gTemplatesName}/$(basename "$templateName")"
	done
	if [ -f "/${FILENAME}/.config/Kingsoft/Office.conf" ]; then
		sed -i "/^wpsoffice\\\Application%20Settings\\\AppComponentMode=/d" "${FILENAME}/.config/Kingsoft/Office.conf"
	fi
	#.local mime
	if [ -d "${FILENAME}/${gDesktopName}" ]; then
		restore_mimeapp_config "${common_excel_mimes[*]}" "wps-office-prometheus.desktop" "${FILENAME}/.config/mimeapps.list"
		restore_mimeapp_config "${wps_excel_mimes[*]}" "wps-office-prometheus.desktop" "${FILENAME}/.config/mimeapps.list"
		restore_mimeapp_config "${common_ppt_mimes[*]}" "wps-office-prometheus.desktop" "${FILENAME}/.config/mimeapps.list"
		restore_mimeapp_config "${wps_ppt_mimes[*]}" "wps-office-prometheus.desktop" "${FILENAME}/.config/mimeapps.list"
		restore_mimeapp_config "${common_word_mimes[*]}" "wps-office-prometheus.desktop" "${FILENAME}/.config/mimeapps.list"
		restore_mimeapp_config "${wps_word_mimes[*]}" "wps-office-prometheus.desktop" "${FILENAME}/.config/mimeapps.list"
		restore_mimeapp_config "${common_pdf_mimes[*]}" "wps-office-prometheus.desktop" "${FILENAME}/.config/mimeapps.list"
		if [ -f "${FILENAME}/${gDesktopName}/wps-office-ofd.desktop" ]; then
			restore_mimeapp_config "${common_ofd_mimes[*]}" "wps-office-prometheus.desktop" "${FILENAME}/.config/mimeapps.list"
		fi
		if [ -f "${FILENAME}/${gDesktopName}/wps-office-flow.desktop" ]; then
			restore_mimeapp_config "${common_flow_mimes[*]}" "wps-office-prometheus.desktop" "${FILENAME}/.config/mimeapps.list"
		fi
		if [ -f "${FILENAME}/${gDesktopName}/wps-office-mind.desktop" ]; then
			restore_mimeapp_config "${common_mind_mimes[*]}" "wps-office-prometheus.desktop" "${FILENAME}/.config/mimeapps.list"
		fi
	fi
	rm -rf "${FILENAME}/.local/share/Kingsoft/account"
	rm -rf "${FILENAME}/.local/share/Kingsoft/WPS Cloud Files"
done

if [ -f "/usr/bin/quickstartoffice" ]; then
	if [ -f "/etc/profile" ]; then
		sed -i '/quickstartoffice/d' /etc/profile
	fi
	if [ -f "/root/.bash_logout" ]; then
		sed -i '/quickstartoffice/d' /root/.bash_logout
	fi
	/usr/bin/quickstartoffice stop
	killall -9 EverythingDaemon wpsofd &>/dev/null || true
else
	killall wps wpp et promecefpluginhost wpsoffice wpspdf wpsofd parsecloudfiletool wpsflow wpsmind wpsofd &>/dev/null || true
	sleep 3
	killall -9 wps wpp et promecefpluginhost wpsoffice wpspdf wpsofd parsecloudfiletool wpsflow wpsmind EverythingDaemon wpsofd &>/dev/null || true
fi

if [ -h "/usr/lib64/mozilla/plugins/libbrowsergrapher.so" ]; then
	rm -rf "/usr/lib64/mozilla/plugins/libbrowsergrapher.so"
fi
if [ -f "/usr/lib/x86_64-linux-gnu/caja/extensions-2.0/libwps-print.so" ] ; then
	rm -rf "/usr/lib/x86_64-linux-gnu/caja/extensions-2.0/libwps-print.so"
fi
if [ -f "/usr/lib64/caja/extensions-2.0/libwps-print.so" ] ; then
	rm -rf "/usr/lib64/caja/extensions-2.0/libwps-print.so"
fi
if [ -f "/usr/lib/aarch64-linux-gnu/caja/extensions-2.0/libwps-print.so" ] ; then
	rm -rf "/usr/lib/aarch64-linux-gnu/caja/extensions-2.0/libwps-print.so"
fi

for HOMEDIR in /home/*; do
	OA_CONF_PATH="$HOMEDIR/.kingsoft/wps/jsaddons"
	if [ -d "$OA_CONF_PATH" ]; then
		rm -rf "$OA_CONF_PATH"
	fi
	# 卸载WPS时删除邮箱账号信息
	if [ -f "$HOMEDIR/.local/share/Kingsoft/office6/mailconfig/encrypt.txt" ]; then
		rm -rf "$HOMEDIR/.local/share/Kingsoft/office6/mailconfig/encrypt.txt"
	fi
	if [ -f "$HOMEDIR/.kingsoft/office6/mailconfig/encrypt.txt" ]; then
		rm -rf "$HOMEDIR/.kingsoft/office6/mailconfig/encrypt.txt"
	fi
	# 卸载WPS时删除云文档登录信息
	if [ -d "$HOMEDIR/.local/share/Kingsoft/account" ]; then
		rm -rf "$HOMEDIR/.local/share/Kingsoft/account"
	fi 
	# 卸载WPS时删除文件搜索缓存数据
	if [ -d "$HOMEDIR/.local/share/Kingsoft/Ksearch" ]; then
		rm -rf "$HOMEDIR/.local/share/Kingsoft/Ksearch"
	fi
	# 卸载删除关联检测文件
	if [ -f "$HOMEDIR/.local/share/mime/packages/Override.xml" ]; then
		rm -rf "$HOMEDIR/.local/share/mime/packages/Override.xml"
		if [ -x /usr/bin/update-mime-database ]; then
			update-mime-database "$HOMEDIR/.local/share/mime" >/dev/null 2>&1 || true
		fi
	fi
	rm -f $HOMEDIR/.local/share/Kingsoft/office6\\synccfg\\default\\head\\workarea.cfg
done

function ks_update_rm()
{
	updatePath="/opt/kingsoft/wtool"
	if [ -e "${updatePath}/wpsupdate" ] ; then
		${updatePath}/wpsupdate /senduninstallinfo:true >/dev/null 2>&1 || true
	fi
}

function ks_allinone_rm()
{
	allinonePath="/opt/kingsoft/wps-office/office6/ofdmodule"
	rm -rf ${allinonePath}/libc++abi.so*
	rm -rf ${allinonePath}/libc++.so*
	rm -rf ${allinonePath}/libstdc++.so*
	rm -rf ${allinonePath}/libgcc_s.so*
}


ks_update_rm
ks_allinone_rm
if [ -h '/usr/share/mime/packages/business-office.xml' ]; then
	rm -rf /usr/share/mime/packages/business-office.xml 
fi
rm -rf /opt/kingsoft/wps-office/office6/*_extend_api.so

if [ $domesticVersion -eq 1 ]; then
	rm -rf /opt/kingsoft/wps-office/office6/mui/zh_CN/resource/help
fi

if [ -f '/opt/kingsoft/wtool/update.cfg' ]; then
	res=`cat /opt/kingsoft/wtool/update.cfg | grep "ClearUpdateCfg" 2>/dev/null || true`
	if [ -z "$res" ]; then
		rm -rf '/opt/kingsoft/wtool'
		for HOMEDIR in /home/*; do
			rm -f $HOMEDIR/.config/autostart/wps-office-update.desktop
		done
		rm -rf /etc/xdg/autostart/wps-office-xcupdate.desktop
		rm -rf /etc/cron.d/wpsupdatetask
	else
		:
	fi
elif [ -d '/opt/kingsoft/wtool' ]; then
	rm -rf '/opt/kingsoft/wtool'
fi

for HOMEDIR in /home/*; do
	OA_CONF_PATH="$HOMEDIR/.kingsoft/wps/jsaddons"
	if [ -d "$OA_CONF_PATH" ]; then
		rm -rf "$OA_CONF_PATH"
	fi
done
