#!/bin/bash

export XDG_DATA_HOME=${XDG_DATA_HOME:-/usr/share/}
ksv_xdg_dirs="/usr/share/:$XDG_DATA_DIRS"
ksv_xdg_dir=
ksv_xdg_data_dir="/usr/share"
ksv_def_icon_theme=icons/hicolor
hasDesktopMenu=true
isZYJ=false
hasAuthInfo=true
domesticVersion=0
isFullPackage=true
isRunScripts=true
isUos=false;
isVisioReadOnly=false
isManualCopyIcon=false
isForceFushion=true
isEducation=false
productName="Unified"
holdFilePaths=($allpaths)
if [ -e '/usr/share/mime/packages/custom-wps-office.xml' ]; then
	domesticVersion=1
fi

ksv_wps_mimes=('application-wps-office.wps' 'wps-office-wps.png' 'application-wps-office.wpt' 'wps-office-wpt.png' 'application-wps-office.wpss' 'wps-office-wpss.png' ' application-wps-office.wpso' 'wps-office-wpso.png')
ksv_word_mimes=('application-wps-office.doc' 'wps-office-doc.png' 'application-wps-office.dot' 'wps-office-dot.png' 'application-vnd.ms-word' 'wps-office-doc.png' 'application-msword' 'wps-office-doc.png' 'application-msword-template' 'wps-office-dot.png' 'application-wps-office.docx' 'wps-office-doc.png' 'application-wps-office.dotx' 'wps-office-dot.png' 'vnd.ms-word.document.macroenabled.12' 'wps-office-doc.png' 'vnd.ms-word.template.macroenabled.12' 'wps-office-dot.png' 'application-wps-office.uot' 'wps-office-uot.png' 'application-wps-office.uot3' 'wps-office-uot3.png' 'application-wps-office.uott3' 'wps-office-uott3.png')
ksv_et_mimes=('application-wps-office.et' 'wps-office-et.png' 'application-wps-office.ett' 'wps-office-ett.png' 'application-wps-office.ets' 'wps-office-ets.png' 'application-wps-office.eto' 'wps-office-eto.png' 'application-wps-office.uos3' 'wps-office-uos3.png' 'application-wps-office.uost3' 'wps-office-uost3.png')
ksv_excel_mimes=('application-wps-office.xls' 'wps-office-xls.png' 'application-wps-office.xlt' 'wps-office-xlt.png' 'application-vnd.ms-excel' 'wps-office-xls.png' 'application-msexcel' 'wps-office-xls.png' 'application-wps-office.xlsx' 'wps-office-xls.png' 'application-wps-office.xltx' 'wps-office-xlt.png' 'vnd.ms-excel.sheet.macroenabled.12' 'wps-office-xls.png' 'vnd.ms-excel.template.macroenabled.12' 'wps-office-xlt.png' 'application-wps-office.uos' 'wps-office-uos.png')
ksv_wpp_mimes=('application-wps-office.dps' 'wps-office-dps.png' 'application-wps-office.dpt' 'wps-office-dpt.png' 'application-wps-office.dpso' 'wps-office-dpso.png' 'applicaion-wps-office.dpss' 'wps-office-dpss.png')
ksv_ppt_mimes=('application-wps-office.ppt' 'wps-office-ppt.png' 'application-wps-office.pot' 'wps-office-pot.png' 'application-vnd.ms-powerpoint' 'wps-office-ppt.png' 'application-vnd.mspowerpoint' 'wps-office-ppt.png' 'application-powerpoint' 'wps-office-ppt.png' 'application-wps-office.pptx' 'wps-office-ppt.png' 'application-wps-office.potx' 'wps-office-pot.png' 'vnd.ms-powerpoint.presentation.macroenabled.12' 'wps-office-ppt.png' 'vnd.ms-powerpoint.template.macroenabled.12' 'wps-office-pot.png' 'application-wps-office.uop' 'wps-office-uop.png' 'application-wps-office.uop3' 'wps-office-uop3.png' 'application-wps-office.uopt3' 'wps-office-uopt3.png')
ksv_pdf_mimes=('application-wps-office.pdf' 'wps-office-pdf.png' 'application-pdf' 'wps-office-pdf.png')
ksv_ofd_mimes=('application-wps-office.ofd' 'wps-office-ofd.png')
if [ "$isVisioReadOnly" = "true" ] ; then
	ksv_flow_mimes=('application-wps-office.pos' 'wps-office-pos.png' 'application-wps-office.vsdx' 'wps-office-pos.png' 'application-wps-office.vsd' 'wps-office-pos.png')
else
	ksv_flow_mimes=('application-wps-office.pos' 'wps-office-pos.png')
fi 
ksv_mind_mimes=('application-wps-office.pof' 'wps-office-pof.png')

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')
if [ "$isVisioReadOnly" = "true" ] ; then
	common_flow_mimes=('application/wps-office.pos' 'application/wps-office.vsdx' 'application/wps-office.vsd')
else
	common_flow_mimes=('application/wps-office.pos')
fi
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' 'application/wps-office.uot' 'application/wps-office.uot3' 'application/wps-office.uott3')
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' 'application/wps-office.uop' 'application/wps-office.uop3' 'application/wps-office.uopt3')
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' 'application/wps-office.uos')

#读取mimeapps.list文件,并把mimetype的关联的defaulttype放到最前边
#参数1 mimetype
#参数2 defaulttype
function write_mimeapp_config()
{
	mimetype=$1
	defaulttype=$2
	configFilePath=$3
	if [ ! -f '/usr/share/mime/packages/custom-wps-office.xml' ]; then
		if [ ${configFilePath} = "/usr/share/applications/mimeapps.list" ]; then
			configFilePath='/etc/xdg/mimeapps.list'
		fi
	fi
	touch $configFilePath
	if type python3 >/dev/null 2>&1 ; then
		python3 /opt/kingsoft/wps-office/INSTALL/writemime_python3.py "$configFilePath" "$mimetype" "$defaulttype"
	elif type python2 >/dev/null 2>&1 ; then
		python2 /opt/kingsoft/wps-office/INSTALL/writemime_python2.py "$configFilePath" "$mimetype" "$defaulttype"
	fi
}

function kmin()
{
	if [ $# -eq 0 ]; then
		echo ""
	elif [ $# -eq 1 ]; then
		echo $1
	elif [ $1 -le $2 ]; then
		echo $1
	else
		echo $2
	fi
}

function kmax()
{
	if [ $# -eq 0 ]; then
		echo ""
	elif [ $# -eq 1 ]; then
		echo $1
	elif [ $1 -ge $2 ]; then
		echo $1
	else
		echo $2
	fi
}

function vercomp ()
{
	if [[ $1 == $2 ]]
	then
		echo 3
	return
	fi
	local IFS=.
	local i ver1=($1) ver2=($2)
	for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
	do
		ver1[i]=0
	done
	for ((i=0; i<${#ver1[@]}; i++))
	do
		if [[ -z ${ver2[i]} ]]
		then
			ver2[i]=0
		fi
		if ((10#${ver1[i]} > 10#${ver2[i]}))
		then
			echo 3
		return
		fi
		if ((10#${ver1[i]} < 10#${ver2[i]}))
		then
			echo 1
		return
		fi
	done
	echo 2
}

function ks_check_library_version()
{
	cd /opt/kingsoft/wps-office/office6
	wps_lib_name=$1
	if [ ! -h $wps_lib_name ]; then
		return
	fi
	wps_lib_arch=`hexdump $wps_lib_name -n 2 -s 18 -e '1/2 "%u" '`
	wps_lib_bit=`hexdump  $wps_lib_name -n 1 -s 4 -e '1/1 "%u"'`
	wps_lib_abs_path=$(readlink -f $wps_lib_name)
	wps_lib_version=$(echo $wps_lib_abs_path | awk -F '.so.' '{print $NF}')
	sys_libs_path=$(ldconfig -p 2>/dev/null | grep $wps_lib_name | awk -F '> ' '{print $2}')

	if [ ! -z "$sys_libs_path" ]; then
		for item in $sys_libs_path ; do
			item_abs_path=$(readlink -f $item)
			item_arch=`hexdump $item_abs_path -n 2 -s 18 -e '1/2 "%u" '`
			item_bit=`hexdump  $item_abs_path -n 1 -s 4 -e '1/1 "%u"'`
			if [ "$wps_lib_arch" = "$item_arch" ] && [ "$wps_lib_bit" = "$item_bit" ]; then
				item_version=$(echo $item_abs_path | awk -F '.so.' '{print $NF}')
				rst=$(vercomp $item_version $wps_lib_version)
				if [ $rst -eq 3 ]; then
					ln -sf $item_abs_path $wps_lib_name
					break
				fi
			fi
		done
	fi
}

function is_valid_group() 
{
    local group_name=$1
    getent group "$group_name" >/dev/null 2>&1
    return $?
}

function ks_transfer_auth_info()
{

	AUTH_DATA_PATH='/opt/kingsoft/.auth'
	if [ ! -d "$AUTH_DATA_PATH" ]; then
		if [ -d "/opt/.auth" ]; then
			cp /opt/.auth /opt/kingsoft -r
			rm -rf /opt/.auth
		fi
		mkdir -p "$AUTH_DATA_PATH/tmp"
		touch "${AUTH_DATA_PATH}/license2.dat"
		chmod -R 777 "$AUTH_DATA_PATH"
	fi
	
	find /home/*/.kingsoft/office6/data/license2.dat -exec bash -c 'cp "$0" /opt/kingsoft/.auth/tmp/$RANDOM.dat && rm -f "$0"' {} \; >/dev/null 2>&1 || true
	find /home/*/.local/share/Kingsoft/office6/data/license2.dat -exec bash -c 'cp "$0" /opt/kingsoft/.auth/tmp/$RANDOM.dat && rm -f "$0"' {} \; >/dev/null 2>&1 || true
	cp /root/.kingsoft/office6/data/license2.dat /opt/kingsoft/.auth/tmp/$RANDOM.dat >/dev/null 2>&1 || true
	cp /root/.local/share/Kingsoft/office6/data/license2.dat /opt/kingsoft/.auth/tmp/$RANDOM.dat >/dev/null 2>&1 || true
	[ -f /root/.kingsoft/office6/data/license2.dat ] && rm -f /root/.kingsoft/office6/data/license2.dat >/dev/null 2>&1 || true
	[ -f /root/.local/share/Kingsoft/office6/data/license2.dat ] && rm -f /root/.local/share/Kingsoft/office6/data/license2.dat >/dev/null 2>&1 || true

	chown -R root:root $AUTH_DATA_PATH
	if is_valid_group "root"; then
		chgrp -R root $AUTH_DATA_PATH
	fi
	chmod -R 777 $AUTH_DATA_PATH

	AUTH_CONF_PATH='/opt/kingsoft/.auth/Auth.conf'
	AUTH_CONF_SEC_OLD_PATH='/opt/kingsoft/Auth.conf'
	AUTH_CONF_OLD_PATH='/etc/wps-office/Auth.conf'
	if [ -f "$AUTH_CONF_PATH" ]; then
		return
	fi

	mkdir -p "$(dirname "$AUTH_CONF_PATH")" && touch "$AUTH_CONF_PATH"
	chmod 666  -R $AUTH_CONF_PATH

	if [ -f "$AUTH_CONF_SEC_OLD_PATH" ]; then
		cp "$AUTH_CONF_SEC_OLD_PATH" "$AUTH_CONF_PATH" -a
		rm -f "$AUTH_CONF_SEC_OLD_PATH"
		return
	fi

	if [ -f "$AUTH_CONF_OLD_PATH" ]; then
		cp "$AUTH_CONF_OLD_PATH" "$AUTH_CONF_PATH" -a
		rm -rf "$(dirname "$AUTH_CONF_OLD_PATH")"
		return
	fi

	OFFICE_CONF_PATH="/root/.config/Kingsoft/Office.conf"
	if [ -f $OFFICE_CONF_PATH ]; then
		WPS_T=$(kmin $WPS_T $(grep -oP '(?<=wps\\wps_t=)\d+' $OFFICE_CONF_PATH))
		WPP_T=$(kmin $WPP_T $(grep -oP '(?<=wpp\\wpp_t=)\d+' $OFFICE_CONF_PATH))
		ET_T=$(kmin $ET_T $(grep -oP '(?<=et\\et_t=)\d+' $OFFICE_CONF_PATH))
		WPS_L=$(kmax $WPS_L $(grep -oP '(?<=wps\\wps_l=)\d+' $OFFICE_CONF_PATH))
		WPP_L=$(kmax $WPP_L $(grep -oP '(?<=wpp\\wpp_l=)\d+' $OFFICE_CONF_PATH))
		ET_L=$(kmax $ET_L $(grep -oP '(?<=et\\et_l=)\d+' $OFFICE_CONF_PATH))
	fi

	for HOMEDIR in /home/*; do
		USERNAME="${HOMEDIR##*/}"
		[ ! -d "${HOMEDIR}" ] && continue
		getent passwd "${USERNAME}" >/dev/null 2>&1 || continue
		OFFICE_CONF_PATH="$HOMEDIR/.config/Kingsoft/Office.conf"
		if [ -f $OFFICE_CONF_PATH ]; then
			WPS_T=$(kmin $WPS_T $(grep -oP '(?<=wps\\wps_t=)\d+' $OFFICE_CONF_PATH))
			WPP_T=$(kmin $WPP_T $(grep -oP '(?<=wpp\\wpp_t=)\d+' $OFFICE_CONF_PATH))
			ET_T=$(kmin $ET_T $(grep -oP '(?<=et\\et_t=)\d+' $OFFICE_CONF_PATH))
			WPS_L=$(kmax $WPS_L $(grep -oP '(?<=wps\\wps_l=)\d+' $OFFICE_CONF_PATH))
			WPP_L=$(kmax $WPP_L $(grep -oP '(?<=wpp\\wpp_l=)\d+' $OFFICE_CONF_PATH))
			ET_L=$(kmax $ET_L $(grep -oP '(?<=et\\et_l=)\d+' $OFFICE_CONF_PATH))
		fi
	done

	if [ -n "${WPS_T}" ] || [ -n "${WPP_T}" ] || [ -n "${ET_T}" ] || [ -n "${WPS_L}" ] || [ -n "${WPP_L}" ] || [ -n "${ET_L}" ]; then
		AUTHCONTENT=$(cat <<-EOF
		[wps]
		wps_t=${WPS_T}
		wps_l=${WPS_L}

		[wpp]
		wpp_t=${WPP_T}
		wpp_l=${WPP_L}

		[et]
		et_t=${ET_T}
		et_l=${ET_L}
		EOF
		)
		echo "$AUTHCONTENT" > $AUTH_CONF_PATH
	fi
}

function ks_install_icon()
{
	local context=$1
	shift
	local mode=$1
	shift

	local png=
	local mime=
	local nextIsPng=0
	for item in $@
	do
		if [ "" = "${item}" ] ; then
			continue
		fi
		if [ ${nextIsPng} -eq 1 ] ; then
			png=${item}
			for i in 16 22 32 48 64 128 256; do
				if [ -e "${ksv_xdg_dir}/${ksv_def_icon_theme}/${i}x${i}/${context}/${png}" ] ; then
					xdg-icon-resource install --noupdate --mode ${mode} --context ${context} --size ${i} "${ksv_xdg_dir}/${ksv_def_icon_theme}/${i}x${i}/${context}/${png}" ${mime}
				fi
			done
			nextIsPng=0
		else
			mime=${item}
			nextIsPng=1
		fi
	done
}

function ks_init()
{
	set -e
	for i in `echo "${ksv_xdg_dirs}" | sed 's/:/ /g'`
	do
		if [ -w ${i}/${ksv_def_icon_theme} ] ; then
			ksv_xdg_dir="${i}"
		fi
	done
}

function ks_install_desk()
{
	if [ $isUos ]; then
		deskFiles="`find /usr/share/applications -name "wps*.desktop" 2>/dev/null`" || true
		if [[ -n "${deskFiles}" ]]; then
			for deskFile in ${deskFiles};do
				deskName="`echo ${deskFile##*/}`"
				if [[ -n "${deskName}" ]]; then
					if [ ! -e "${ksv_xdg_data_dir}/applications/${deskName}" ]; then
						ln -sf ${deskFile} "${ksv_xdg_data_dir}/applications/${deskName}" 2>/dev/null || true
					fi
				fi
			done
		fi
	fi
}

function ks_copy_icon()
{
	if $isUos ;then
		oempath="/opt/kingsoft/wps-office/office6/cfgs/oem.ini"
		if [ -f ${oempath} ] ;then
			if $isManualCopyIcon ;then
				deskpath="/usr/share/applications/"
				icon_parentpath="`echo "${deskpath::-13}"`"
				icon_parentpath_length=${#icon_parentpath}
				iconpath="${icon_parentpath}icons"
				if [ -d ${iconpath} ]; then
					iconFiles="`find ${iconpath} -name "wps*.png" 2>/dev/null`" || true
					if [[ -n "${iconFiles}" ]]; then
						for iconFile in ${iconFiles};do
							src_iconpath="`echo "${iconFile:${icon_parentpath_length}}"`"
							tar_iconPath="/usr/share/${src_iconpath}"
							tar_iconoarentPath="${tar_iconPath%/*}"
							real_iconpath="`readlink -f ${tar_iconPath} 2>/dev/null || true`"
							if [ "${real_iconpath}" != "${iconFile}" ]; then
								if [ ! -e "${tar_iconoarentPath}" ]; then
									mkdir -p ${tar_iconoarentPath} >/dev/null 2>&1 || true
								fi
								ln -sf ${iconFile} ${tar_iconPath} >/dev/null 2>&1 || true
							fi
						done
					fi
				fi
			fi
		fi
	fi
}

function ks_config_del()
{
	if [ -f "/opt/kingsoft/wps-office/INSTALL/delconfs" ]; then
		while read -r item
		do
			[ -f "/root/.config/Kingsoft/Office.conf" ] && sed -i "/${item}/d" "/root/.config/Kingsoft/Office.conf"
			find /home/*/.config/Kingsoft/Office.conf 2>/dev/null | xargs sed -i "/${item}/d" 2>/dev/null || true
		done < "/opt/kingsoft/wps-office/INSTALL/delconfs"
	else
		[ -f "/root/.config/Kingsoft/Office.conf" ] && sed -i "/AcceptedEULA/d" "/root/.config/Kingsoft/Office.conf" 2>/dev/null || true
		find /home/*/.config/Kingsoft/Office.conf 2>/dev/null | xargs sed -i "/AcceptedEULA/d" 2>/dev/null || true
	fi
	for HOMEDIR in /home/*; do
		USERNAME="${HOMEDIR##*/}"
		[ ! -d "${HOMEDIR}" ] && continue
		getent passwd "${USERNAME}" >/dev/null 2>&1 || continue
		if [ -f "$HOMEDIR/.local/share/Kingsoft/office6\\synccfg\\default\\head\\workarea.cfg" ]; then
			rm -f "$HOMEDIR/.local/share/Kingsoft/office6\\synccfg\\default\\head\\workarea.cfg"
		fi
		if [ -d "$HOMEDIR/.config/wpsofdreader" ]; then
			rm -rf "$HOMEDIR/.config/wpsofdreader"
		fi
		if [ -d "$HOMEDIR/.config/suwellofdplugin" ]; then
			rm -rf "$HOMEDIR/.config/suwellofdplugin"
		fi
	done
}

function ks_config_menu()
{
	if [ -f /usr/share/mime/packages/custom-wps-office.xml ]; then
		[ -f /usr/share/mime/packages/wps-office-et.xml ] && rm -f /usr/share/mime/packages/wps-office-et.xml
		[ -f /usr/share/mime/packages/wps-office-wps.xml ] && rm -f /usr/share/mime/packages/wps-office-wps.xml
		[ -f /usr/share/mime/packages/wps-office-wpp.xml ] && rm -f /usr/share/mime/packages/wps-office-wpp.xml
		[ -f /usr/share/mime/packages/wps-office-pdf.xml ] && rm -f /usr/share/mime/packages/wps-office-pdf.xml
	fi
	#如果存在这个文件（安装时勾选了不默认使用doc等文件)，则在卸载前先把相应的xml和desktop文件处理一下，不生成mime，不进行关联。卸载后为防止失败，还需要在把相应的xml文件加回来。
	noUse=0	
	fileName="/tmp/cancleFileAssociation"
	touch /usr/share/applications/mimeapps.list || true
	if [ -f "$fileName" ]; then
		rm -f "$fileName"
		noUse=1
		[ -f /usr/share/mime/packages/custom-wps-office.xml ] && rm -f /usr/share/mime/packages/custom-wps-office.xml
		sed -i '/^MimeType/d' /usr/share/applications/wps-office-wps.desktop
		sed -i '/^MimeType/d' /usr/share/applications/wps-office-wpp.desktop
		sed -i '/^MimeType/d' /usr/share/applications/wps-office-et.desktop
	fi
	if [ -e "/etc/xdg/mimeapps.list" ]; then
		sed -i '/^application\/wps-office.pdf/d' "/etc/xdg/mimeapps.list"
		sed -i '/^application\/pdf/d' "/etc/xdg/mimeapps.list"
	fi
	if [ -f '/usr/share/mime/packages/custom-wps-office.xml' ]; then
		ln -sf /usr/share/mime/packages/custom-wps-office.xml /usr/share/mime/packages/business-office.xml 
	fi
	if [ -f "/usr/share/applications/wps-office-et.desktop" ] && [ $noUse -eq 0 ]; then
		ks_install_icon "mimetypes" "system" "${ksv_et_mimes[*]}" "${ksv_excel_mimes[*]}"
		write_mimeapp_config "${common_excel_mimes[*]}" "wps-office-et.desktop" "/usr/share/applications/mimeapps.list"
		write_mimeapp_config "${wps_excel_mimes[*]}" "wps-office-et.desktop" "/usr/share/applications/mimeapps.list"
	fi

	if [ -f "/usr/share/applications/wps-office-wpp.desktop" ] && [ $noUse -eq 0 ]; then
		ks_install_icon "mimetypes" "system" "${ksv_wpp_mimes[*]}" "${ksv_ppt_mimes[*]}"
		write_mimeapp_config "${common_ppt_mimes[*]}" "wps-office-wpp.desktop" "/usr/share/applications/mimeapps.list"
		write_mimeapp_config "${wps_ppt_mimes[*]}" "wps-office-wpp.desktop" "/usr/share/applications/mimeapps.list"
	fi

	if [ -f "/usr/share/applications/wps-office-wps.desktop" ] && [ $noUse -eq 0 ]; then
		ks_install_icon "mimetypes" "system" "${ksv_wps_mimes[*]}" "${ksv_word_mimes[*]}"
		xdg-icon-resource forceupdate --mode system

		write_mimeapp_config "${common_word_mimes[*]}" "wps-office-wps.desktop" "/usr/share/applications/mimeapps.list"
		write_mimeapp_config "${wps_word_mimes[*]}" "wps-office-wps.desktop" "/usr/share/applications/mimeapps.list"
	fi

	if [ -f "/usr/share/applications/wps-office-pdf.desktop" ] && [ $noUse -eq 0 ] && [ $domesticVersion -eq 1 ]; then
		ks_install_icon "mimetypes" "system" "${ksv_pdf_mimes[*]}"
		xdg-icon-resource forceupdate --mode system
	fi
	if [ -f "/usr/share/applications/wps-office-ofd.desktop" ] && [ $noUse -eq 0 ]; then
		ks_install_icon "mimetypes" "system" "${ksv_ofd_mimes[*]}"
		xdg-icon-resource forceupdate --mode system
		write_mimeapp_config "${common_ofd_mimes[*]}" "wps-office-ofd.desktop" "/usr/share/applications/mimeapps.list"
	fi
	if [ -f "/usr/share/applications/wps-office-flow.desktop" ] && [ $noUse -eq 0 ]; then
		ks_install_icon "mimetypes" "system" "${ksv_flow_mimes[*]}"
		xdg-icon-resource forceupdate --mode system
		write_mimeapp_config "${common_flow_mimes[*]}" "wps-office-flow.desktop" "/usr/share/applications/mimeapps.list"
	fi
	if [ -f "/usr/share/applications/wps-office-mind.desktop" ] && [ $noUse -eq 0 ]; then
		ks_install_icon "mimetypes" "system" "${ksv_mind_mimes[*]}"
		xdg-icon-resource forceupdate --mode system
		write_mimeapp_config "${common_mind_mimes[*]}" "wps-office-mind.desktop" "/usr/share/applications/mimeapps.list"
	fi
	update-mime-database ${ksv_xdg_dir}/mime &> /dev/null || :
	update-desktop-database &> /dev/null || true
}

# root用户注销时杀死WPS的后台进程
# 其他用户注销可通过autostart中desktop中quickstartoffice restart实现
function ks_config_wpsdaemon()
{
	[ ! -f /usr/bin/quickstartoffice ] && return
	quickstartoffice stop

	declare -A property
	sessionid=
	user=
	for session in $(loginctl --no-legend list-sessions | awk '{ print $1 }' ); do
		while IFS="=" read key value ; do
			property[$key]="$value"
		done < <(loginctl show-session $session)

		if [ "${property[Type]}" = "x11" ] || [ "${property[Type]}" = "wayland" ]; then
			sessionid=$session
			user=${property[Name]}
			if [ "${property[Active]}" = "yes" ];then
				break;
			fi
		fi
	done
	[ -z "$user" -o -z "$sessionid" ] && return
	id -u $user >/dev/null 2>&1 || return 0
	pids=($(ps e -u "$user" 2>/dev/null | sed -rn "/^.*XDG_SESSION_ID=$sessionid.*/p" | sed -rn "/^.*DISPLAY.*/p" | sed -rn "/^.*XMODIFIERS.*/p" | sed -rn "/^.*(SESSION_MANAGER|DESKTOP_SESSION_ID).*/p" | sed -rn "s/^\s*([0-9]+).*/\1/p"))

	for i in ${pids[*]}; do 
		if [ -f "/proc/$i/environ" ]; then
			environ=$(cat /proc/$i/environ 2>/dev/null | tr '\0' '\n')
			if echo $environ | grep "DESKTOP_AUTOSTART_ID" >/dev/null 2>&1; then
				unset environ
				continue
			fi
			break;
		fi
	done
	[ -z "$environ" ] && return
	(
		set +e
		for e in $environ; do 
			export $e >/dev/null 2>&1
		done
		unset LD_LIBRARY_PATH
		[ -n "$DISPLAY" ] && su $user -c "quickstartoffice start" >/dev/null 2>&1 || true
	)
}

function copy_install_to_desktop()
{
	if [ $# -ne 3 ] ; then
		return;
	fi
	local curFileName=$1
	local wpsDesktopName=$2
	local curUserName=$3
	if [ -e "/usr/share/applications/${wpsDesktopName}" ]; then
		cp -f /usr/share/applications/${wpsDesktopName} "${curFileName}/${gDesktopName}/${wpsDesktopName}"
		if test $? -eq 0; then
			chmod +x "${curFileName}/${gDesktopName}/${wpsDesktopName}"
			chown ${curUserName} "${curFileName}/${gDesktopName}/${wpsDesktopName}"
		fi
	fi
	
}

function readforcefushionoem()
{
	oempath="/opt/kingsoft/wps-office/office6/cfgs/oem.ini"
	if [ -e $oempath ]; then
		if $isForceFushion ; then
			echo 0
		else
			echo 1
		fi
	else
		echo 1
	fi
}

function get_desktop_type()
{
	desktopType=0
	currentMode=""
	currentLine=0
	haveConf=0
	if [ $# -ne 1 ] ; then
		return;
	fi
	local curFileName=$1
	if [ -e "${curFileName}/.config/Kingsoft/Office.conf" ];then
		haveConf=1	
	fi	
	if [ $haveConf -eq 1 ]; then
		currentMode=$(awk -v line=$(awk '/\[6.0\]/{print NR}' "${curFileName}/.config/Kingsoft/Office.conf")  -F"=" '$1=="wpsoffice\\Application%20Settings\\AppComponentModeInstall" {if(NR>=line){print $2}}' "${curFileName}/.config/Kingsoft/Office.conf")
		currentLine=$(awk -v line=$(awk '/\[6.0\]/{print NR}' "${curFileName}/.config/Kingsoft/Office.conf")  -F"=" '$1=="wpsoffice\\Application%20Settings\\AppComponentModeInstall" {if(NR>=line){print NR}}' "${curFileName}/.config/Kingsoft/Office.conf")
	fi
	if [ $haveConf -eq 1 ] && [ "$currentMode" = "prome_fushion" ]; then
		desktopType=0
	elif [ $haveConf -eq 1 ] && [ "$currentMode" = "prome_independ" ]; then
		desktopType=1
	else
		if [ "$productName" = "Unified" ]; then
			desktopType=0
		elif [ $domesticVersion -eq 1 ]; then
			desktopType=$(readforcefushionoem)
		else
			desktopType=0
		fi
	fi
}

function deploy_wps_desktop_icon()
{
	if [ $# -ne 2 ] ; then
		return;
	fi
	local curFileName=$1
	local curUserName=$2
	if [ -d "${curFileName}/.config" ] ; then
		touch "${curFileName}/.config/mimeapps.list"
		chmod 666 "${curFileName}/.config/mimeapps.list"
		chown ${curUserName} "${curFileName}/.config/mimeapps.list"
	fi
	if [ $haveConf -eq 1 ] && [ $desktopType -eq 0 ]; then
		#有本地配置且是整合模式
		if [ "${currentMode}" = "prome_fushion" ]; then
			#有本地配置，配置中写的是整合模式
			sed -i "${currentLine}a\wpsoffice\\\Application%20Settings\\\AppComponentMode=${currentMode}" "${curFileName}/.config/Kingsoft/Office.conf"
		else
			#有本地配置，但是配置中没有写整合，社区版第一次装，默认用整合，需要将整合配置写进去
			startLine=$(awk '/\[6.0\]/{print NR}' "${curFileName}/.config/Kingsoft/Office.conf")
			sed -i "${startLine}a\wpsoffice\\\Application%20Settings\\\AppComponentModeInstall=prome_fushion" "${curFileName}/.config/Kingsoft/Office.conf"
			sed -i "${startLine}a\wpsoffice\\\Application%20Settings\\\AppComponentMode=prome_fushion" "${curFileName}/.config/Kingsoft/Office.conf"
		fi
		if [ -d "${curFileName}/.config" ] ; then
			write_mimeapp_config "${common_excel_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
			write_mimeapp_config "${wps_excel_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
			write_mimeapp_config "${common_ppt_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
			write_mimeapp_config "${wps_ppt_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
			write_mimeapp_config "${common_word_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
			write_mimeapp_config "${wps_word_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
		fi 
		if [ -e "/usr/share/applications/wps-office-ofd.desktop" ]; then
			write_mimeapp_config "${common_ofd_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
		fi
		if [ -e "/usr/share/applications/wps-office-flow.desktop" ]; then
			write_mimeapp_config "${common_flow_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
		fi
		if [ -e "/usr/share/applications/wps-office-mind.desktop" ]; then
			write_mimeapp_config "${common_mind_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
		fi
		copy_install_to_desktop ${curFileName} "wps-office-prometheus.desktop" ${curUserName}
	else
		if [ $desktopType -eq 0 ]; then
			#无配置是整合（创建配置文件，写进去整合配置）
			if [ -d "${curFileName}/.config" ] ; then
				write_mimeapp_config "${common_excel_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
				write_mimeapp_config "${wps_excel_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
				write_mimeapp_config "${common_ppt_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
				write_mimeapp_config "${wps_ppt_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
				write_mimeapp_config "${common_word_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
				write_mimeapp_config "${wps_word_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
			fi
			if [ -e "/usr/share/applications/wps-office-ofd.desktop" ]; then
				write_mimeapp_config "${common_ofd_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
			fi
			if [ -e "/usr/share/applications/wps-office-flow.desktop" ]; then
				write_mimeapp_config "${common_flow_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
			fi
			if [ -e "/usr/share/applications/wps-office-mind.desktop" ]; then
				write_mimeapp_config "${common_mind_mimes[*]}" "wps-office-prometheus.desktop" "${curFileName}/.config/mimeapps.list"
			fi
			copy_install_to_desktop ${curFileName} "wps-office-prometheus.desktop" ${curUserName}
			if [ ! -e "${curFileName}/.config/Kingsoft" ]; then
			    if [ ! -d "${curFileName}/.config" ]; then
					mkdir -p "${curFileName}/.config"
					chown ${curUserName} "${curFileName}/.config"
					if is_valid_group "${curUserName}"; then
						chgrp ${curUserName} "${curFileName}/.config"
					fi
				fi
				mkdir -p "${curFileName}/.config/Kingsoft"
				chown -R ${curUserName} "${curFileName}/.config/Kingsoft"
				if is_valid_group "${curUserName}"; then
					chgrp -R ${curUserName} "${curFileName}/.config/Kingsoft"
				fi
				chmod 775 "${curFileName}/.config/Kingsoft"
			fi
			touch "${curFileName}/.config/Kingsoft/Office.conf"
			chmod 664 "${curFileName}/.config/Kingsoft/Office.conf"
			chown ${curUserName} "${curFileName}/.config/Kingsoft/Office.conf"
			if is_valid_group "${curUserName}"; then
				chgrp ${curUserName} "${curFileName}/.config/Kingsoft/Office.conf"
			fi
			echo '[6.0]' > "${curFileName}/.config/Kingsoft/Office.conf"
			sed -i "1a\wpsoffice\\\Application%20Settings\\\AppComponentModeInstall=prome_fushion" "${curFileName}/.config/Kingsoft/Office.conf"
			sed -i "1a\wpsoffice\\\Application%20Settings\\\AppComponentMode=prome_fushion" "${curFileName}/.config/Kingsoft/Office.conf"
		else
			#有无配置是多组建
			copy_install_to_desktop ${curFileName} "wps-office-et.desktop" ${curUserName}
			copy_install_to_desktop ${curFileName} "wps-office-wpp.desktop" ${curUserName}
			copy_install_to_desktop ${curFileName} "wps-office-wps.desktop" ${curUserName}
			copy_install_to_desktop ${curFileName} "wps-office-pdf.desktop" ${curUserName}
			copy_install_to_desktop ${curFileName} "wps-office-ofd.desktop" ${curUserName}
			if [ -e "/usr/share/applications/wps-office-flow.desktop" ]; then
				copy_install_to_desktop ${curFileName} "wps-office-flow.desktop" ${curUserName}
			fi
			if [ -e "/usr/share/applications/wps-office-mind.desktop" ]; then
				copy_install_to_desktop ${curFileName} "wps-office-mind.desktop" ${curUserName}
			fi
			if [ -e "/usr/share/applications/wps-office-official.desktop" ]; then
				copy_install_to_desktop ${curFileName} "wps-office-official.desktop" ${curUserName}
			fi
			if [ $haveConf -eq 1 ]; then
				#有本地配置并且是多组件
				startLine=$(awk '/\[6.0\]/{print NR}' "${curFileName}/.config/Kingsoft/Office.conf")
				sed -i "${startLine}a\wpsoffice\\\Application%20Settings\\\AppComponentModeInstall=prome_independ" "${curFileName}/.config/Kingsoft/Office.conf"
				sed -i "${startLine}a\wpsoffice\\\Application%20Settings\\\AppComponentMode=prome_independ" "${curFileName}/.config/Kingsoft/Office.conf"
			fi
		fi			
	fi

	if [ -e "/usr/share/applications/wps-office-wpsclouddisk.desktop" ]; then
		copy_install_to_desktop ${curFileName} "wps-office-wpsclouddisk.desktop" ${curUserName}
	fi
}

function to_deploy_wps_desktop_icon()
{
	if [ $# -ne 2 ] ; then
		return;
	fi
	local curFileName=${1}
	local curUserName=${2}
	get_desktop_type ${curFileName}
	deploy_wps_desktop_icon ${curFileName} ${curUserName}
}

function ks_config_desktop_templates()
{
	gTemplatesName="模板"
	if [ -d "/root/模板" ]; then
		gTemplatesName="模板"
	elif [ -d "/root/Templates" ]; then
		gTemplatesName="Templates"
	fi

	if [ ! -d "/root/${gTemplatesName}" ]; then
		gTemplatesName="模板"
		mkdir -p "/root/${gTemplatesName}" >/dev/null 2>&1 || true
	fi

	gDesktopName="桌面"
	if [ -d "/root/桌面" ]; then
		gDesktopName="桌面"
	elif [ -d "/root/Desktop" ]; then
		gDesktopName="Desktop"
	fi
		
	if [ -d "/opt/kingsoft/wps-office/templates/" ]; then
		if [ -d "/root/${gTemplatesName}" ]; then
			for tempFile in `ls /root/${gTemplatesName} | tr " " "\?"`
			do
				if [ "${tempFile##*.}" = "et" ]||[ "${tempFile##*.}" = "wps" ]||[ "${tempFile##*.}" = "dps" ]; then
					[ -f "/root/${gTemplatesName}/${tempFile}" ] && rm -f "/root/${gTemplatesName}/${tempFile}"
				fi
				if [ "${tempFile:0:3}" = "DOC" ]||[ "${tempFile:0:3}" = "XLS" ]||[ "${tempFile:0:3}" = "PPT" ]; then
					[ -f "/root/${gTemplatesName}/${tempFile}" ] && rm -f "/root/${gTemplatesName}/${tempFile}"
				fi
			done
			cp -f "/opt/kingsoft/wps-office/templates/"* "/root/${gTemplatesName}"
		fi

		if [ ! -d "/etc/skel/${gTemplatesName}" ]; then
			mkdir /etc/skel/${gTemplatesName}
			chmod 755 /etc/skel/${gTemplatesName}
			if is_valid_group "root"; then
				chgrp root /etc/skel/${gTemplatesName}
			fi
		fi
		cp -f "/opt/kingsoft/wps-office/templates/"* "/etc/skel/${gTemplatesName}"

	fi

	[ -f "/root/${gDesktopName}/wps-office-wps.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-wps.desktop"
	[ -f "/root/${gDesktopName}/wps-office-et.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-et.desktop"
	[ -f "/root/${gDesktopName}/wps-office-wpp.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-wpp.desktop"
	[ -f "/root/${gDesktopName}/wps-office-pdf.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-pdf.desktop"
	[ -f "/root/${gDesktopName}/wps-office-ofd.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-ofd.desktop"
	[ -f "/root/${gDesktopName}/wps-office-flow.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-flow.desktop"
	[ -f "/root/${gDesktopName}/wps-office-mind.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-mind.desktop"
	[ -f "/root/${gDesktopName}/wps-office-official.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-official.desktop"
	[ -f "/root/${gDesktopName}/wps-office-prometheus.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-prometheus.desktop"
	[ -f "/root/${gDesktopName}/wps-office-officeassistant.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-officeassistant.desktop"
	[ -f "/root/${gDesktopName}/wps-office-uninstall.desktop" ] && rm -f "/root/${gDesktopName}/wps-office-uninstall.desktop"
	if [ -d "/root/${gDesktopName}" ] && $hasDesktopMenu; then
		to_deploy_wps_desktop_icon "/root" "root"
	fi

	if [ -f "/root/.kingsoft/office6/templates/wps/zh_CN/Normal.dotm" ]; then
		rm -f "/root/.kingsoft/office6/templates/wps/zh_CN/Normal.dotm"
	fi

	for FILENAME in /home/*; do
		USERNAME="${FILENAME##*/}"
		[ ! -d "${FILENAME}" ] && continue
		getent passwd "${USERNAME}" >/dev/null 2>&1 || continue

		if id -u `echo ${FILENAME} | awk '{print substr($FILENAME, 7, 32)}'` >/dev/null 2>&1; then
			:
		else
			continue
		fi

		if [ -f "${FILENAME}/.kingsoft/office6/templates/wps/zh_CN/Normal.dotm" ]; then
			rm -f "${FILENAME}/.kingsoft/office6/templates/wps/zh_CN/Normal.dotm"
		fi

		if [ -f "${FILENAME}/.config/user-dirs.dirs" ] && \
			grep "XDG_DESKTOP_DIR" "${FILENAME}/.config/user-dirs.dirs" >/dev/null 2>&1  && \
			grep "XDG_TEMPLATES_DIR" "${FILENAME}/.config/user-dirs.dirs" >/dev/null 2>&1 ; then
			if [ ! $HOME ]; then 
				HOME=${FILENAME} 
			fi
			export $(sed 's|$HOME|'"${FILENAME}"'|g' ${FILENAME}/.config/user-dirs.dirs | grep XDG_DESKTOP_DIR | grep -v '^#')
			XDG_DESKTOP_DIR=${XDG_DESKTOP_DIR#\"*}
			XDG_DESKTOP_DIR=${XDG_DESKTOP_DIR%*\"}
			export $(sed 's|$HOME|'"${FILENAME}"'|g' ${FILENAME}/.config/user-dirs.dirs | grep XDG_TEMPLATES_DIR | grep -v '^#')
			XDG_TEMPLATES_DIR=${XDG_TEMPLATES_DIR#\"*}
			XDG_TEMPLATES_DIR=${XDG_TEMPLATES_DIR%*\"}
			if [ ! -d "${XDG_DESKTOP_DIR}" ]; then
				mkdir -p "${XDG_DESKTOP_DIR}" >/dev/null 2>&1 || true
			fi
			gDesktopName="${XDG_DESKTOP_DIR//${FILENAME}\//}"
			if [ ! -d "${XDG_TEMPLATES_DIR}" ]; then
				mkdir -p "${XDG_TEMPLATES_DIR}" >/dev/null 2>&1 || true
			fi
			gTemplatesName="${XDG_TEMPLATES_DIR//${FILENAME}\//}"
		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

			if [ ! -d "${FILENAME}/${gTemplatesName}" ]; then
				gTemplatesName="模板"
				mkdir -p "${FILENAME}/${gTemplatesName}" >/dev/null 2>&1 || true
			fi
		fi 

		if [ -d "${FILENAME}/${gDesktopName}" ]; then
			if [ -e "/etc/xdg/menus/applications-merged/wps-office.menu" ]; then
				if [ -e "${FILENAME}/.config/menus/applications-merged" ]; then
					cp -f /etc/xdg/menus/applications-merged/wps-office.menu "${FILENAME}/.config/menus/applications-merged"
				fi
			fi

			local curUserName=`echo ${FILENAME} | awk '{print substr($FILENAME, 7, 32)}'`
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-wps.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-wps.desktop"
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-et.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-et.desktop"
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-wpp.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-wpp.desktop"
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-pdf.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-pdf.desktop"
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-ofd.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-ofd.desktop"
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-flow.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-flow.desktop"
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-mind.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-mind.desktop"
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-official.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-official.desktop"
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-prometheus.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-prometheus.desktop"
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-officeassistant.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-officeassistant.desktop"
			[ -f "/home/${curUserName}/${gDesktopName}/wps-office-uninstall.desktop" ] && rm -f "/home/${curUserName}/${gDesktopName}/wps-office-uninstall.desktop"
			if $hasDesktopMenu; then
				to_deploy_wps_desktop_icon ${FILENAME} ${curUserName}
			fi

		fi

		if [ -d "${FILENAME}/${gTemplatesName}" ]; then
			for tempFile in `ls ${FILENAME}/${gTemplatesName} | tr " " "\?"`
			do
				if [ "${tempFile##*.}" = "et" ]||[ "${tempFile##*.}" = "wps" ]||[ "${tempFile##*.}" = "dps" ]; then
					[ -f "${FILENAME}/${gTemplatesName}/${tempFile}" ] && rm -f "${FILENAME}/${gTemplatesName}/${tempFile}"
				fi
				if [ "${tempFile:0:3}" = "DOC" ]||[ "${tempFile:0:3}" = "XLS" ]||[ "${tempFile:0:3}" = "PPT" ]; then
					[ -f "${FILENAME}/${gTemplatesName}/${tempFile}" ] && rm -f "${FILENAME}/${gTemplatesName}/${tempFile}"
				fi
			done
			cp -f "/opt/kingsoft/wps-office/templates/"* "${FILENAME}/${gTemplatesName}"
		fi
	done

	ks_config_wpsdaemon

	if [ -f "/opt/kingsoft/wps-office/office6/libbrowsergrapher.so" ] && [ -d "/usr/lib64/mozilla/plugins" ]; then
		ln -sf "/opt/kingsoft/wps-office/office6/libbrowsergrapher.so" "/usr/lib64/mozilla/plugins/libbrowsergrapher.so"
	fi
	if [ -f "/opt/kingsoft/wps-office/office6/libbrowsergrapher.so" ] && [ -d "/usr/lib/mozilla/plugins" ]; then
		ln -sf "/opt/kingsoft/wps-office/office6/libbrowsergrapher.so" "/usr/lib/mozilla/plugins/libbrowsergrapher.so"
	fi
	if [ -f "/opt/kingsoft/wps-office/office6/addons/ofdplugin/libnpWpsOFDReader.so" ] && [ -d "/usr/lib64/mozilla/plugins" ]; then
		ln -sf "/opt/kingsoft/wps-office/office6/addons/ofdplugin/libnpWpsOFDReader.so" "/usr/lib64/mozilla/plugins/libnpWpsOFDReader.so"
	fi

	if [ -f "/opt/kingsoft/wps-office/office6/libwps-print.so" ] && ! $isZYJ ; then
		if [ -d "/usr/lib/x86_64-linux-gnu/caja/extensions-2.0/" ] ; then
			cp -f "/opt/kingsoft/wps-office/office6/libwps-print.so" "/usr/lib/x86_64-linux-gnu/caja/extensions-2.0/libwps-print.so"
		fi
		if [ -d "/usr/lib64/caja/extensions-2.0" ] ; then
			cp -f "/opt/kingsoft/wps-office/office6/libwps-print.so" "/usr/lib64/caja/extensions-2.0/libwps-print.so"
		fi
		if [ -d "/usr/lib/aarch64-linux-gnu/caja/extensions-2.0" ] ; then
			cp -f "/opt/kingsoft/wps-office/office6/libwps-print.so" "/usr/lib/aarch64-linux-gnu/caja/extensions-2.0/libwps-print.so"
		fi
		if [ -d "/usr/lib/mips64el-linux-gnu/caja/extensions-2.0" ] ; then
			cp -f "/opt/kingsoft/wps-office/office6/libwps-print.so" "/usr/lib/mips64el-linux-gnu/caja/extensions-2.0/libwps-print.so"
		fi
	fi
	cp -f "/opt/kingsoft/wps-office/office6/libpeony-wpsprint-menu-plugin.so" /usr/lib/$(uname -m)*-linux-gnu*/peony-extensions/ >/dev/null 2>&1 || true

	if [ -d "/usr/share/applications/context-menus" ] && [ -f "/opt/kingsoft/wps-office/desktops/wps-office-print.conf" ] ; then 
		cp -f  "/opt/kingsoft/wps-office/desktops/wps-office-print.conf" "/usr/share/applications/context-menus"
	fi
#mbset
        if [ -f "/opt/kingsoft/wps-office/office6/libwps-cajambset.so" ]  && ! $isZYJ ; then
	if [ -d "/usr/lib/x86_64-linux-gnu/caja/extensions-2.0/" ] ; then
		cp -f "/opt/kingsoft/wps-office/office6/libwps-cajambset.so" "/usr/lib/x86_64-linux-gnu/caja/extensions-2.0/libwps-cajambset.so"
	fi
	if [ -d "/usr/lib64/caja/extensions-2.0" ] ; then
		cp -f "/opt/kingsoft/wps-office/office6/libwps-cajambset.so" "/usr/lib64/caja/extensions-2.0/libwps-cajambset.so"
	fi
	if [ -d "/usr/lib/aarch64-linux-gnu/caja/extensions-2.0" ] ; then
		cp -f "/opt/kingsoft/wps-office/office6/libwps-cajambset.so" "/usr/lib/aarch64-linux-gnu/caja/extensions-2.0/libwps-cajambset.so"
	fi
	if [ -d "/usr/lib/mips64el-linux-gnu/caja/extensions-2.0" ] ; then
		cp -f "/opt/kingsoft/wps-office/office6/libwps-cajambset.so" "/usr/lib/mips64el-linux-gnu/caja/extensions-2.0/libwps-cajambset.so"
	fi
fi

if [ -f "/opt/kingsoft/wps-office/office6/libwps-nautilusmbset.so" ]  && ! $isZYJ ; then
	if [ -d "/usr/lib/nautilus/extensions-3.0/" ] ; then
		cp -f "/opt/kingsoft/wps-office/office6/libwps-nautilusmbset.so" "/usr/lib/nautilus/extensions-3.0/libwps-nautilusmbset.so"
	fi
        if [ -d "/usr/lib/aarch64-linux-gnu/nautilus/extensions-3.0/" ] ; then
		cp -f "/opt/kingsoft/wps-office/office6/libwps-nautilusmbset.so" "/usr/lib/aarch64-linux-gnu/nautilus/extensions-3.0/libwps-nautilusmbset.so"
	fi
        if [ -d "/usr/lib/x86_64-linux-gnu/nautilus/extensions-3.0/" ] ; then
		cp -f "/opt/kingsoft/wps-office/office6/libwps-nautilusmbset.so" "/usr/lib/x86_64-linux-gnu/nautilus/extensions-3.0/libwps-nautilusmbset.so"
	fi
        if [ -d "/usr/lib/arm-linux-gnueabihf/nautilus/extensions-3.0/" ] ; then
		cp -f "/opt/kingsoft/wps-office/office6/libwps-nautilusmbset.so" "/usr/lib/arm-linux-gnueabihf/nautilus/extensions-3.0/libwps-nautilusmbset.so"
	fi
        if [ -d "/usr/lib/powerpc64le-linux-gnu/nautilus/extensions-3.0/" ] ; then
		cp -f "/opt/kingsoft/wps-office/office6/libwps-nautilusmbset.so" "/usr/lib/powerpc64le-linux-gnu/nautilus/extensions-3.0/libwps-nautilusmbset.so"
	fi
        if [ -d "/usr/lib/s390x-linux-gnu/nautilus/extensions-3.0/" ] ; then
		cp -f "/opt/kingsoft/wps-office/office6/libwps-nautilusmbset.so" "/usr/lib/s390x-linux-gnu/nautilus/extensions-3.0/libwps-nautilusmbset.so"
	fi
fi
}

function ks_config_user_language
{
	local curUserName=$1
	local curFileName=$2
	local curKingsoftDir="${curFileName}/.config/Kingsoft"
	local curOfficeConf="${curKingsoftDir}/Office.conf"

	if [ ! -e "${curFileName}/.config" ]; then
		return;
	fi

	if [ ! -e "${curKingsoftDir}" ]; then
		mkdir -p "${curKingsoftDir}"
		chown -R ${curUserName} "${curKingsoftDir}"
		if is_valid_group "${curUserName}"; then
			chgrp -R ${curUserName} "${curKingsoftDir}"
		fi
		chmod 775 "${curKingsoftDir}"
	fi

	if [ ! -e "${curOfficeConf}" ]; then
		touch "${curOfficeConf}"
		chmod 664 "${curOfficeConf}"
		chown ${curUserName} "${curOfficeConf}"
		if is_valid_group "${curUserName}"; then
			chgrp ${curUserName} "${curOfficeConf}"
		fi
	fi
	if [ -e "/opt/kingsoft/wps-office/office6/wps" ] && [ ! -e "/opt/kingsoft/wps-office/office6/libclang_rt.asan.so" ]; then
		/opt/kingsoft/wps-office/office6/wps -misc_linux -confirm_language  ${curOfficeConf}  mui/lang_list/lang_setting.json > /dev/null 2>&1 || true
		chown ${curUserName} "${curOfficeConf}"
		if is_valid_group "${curUserName}"; then
			chgrp ${curUserName} "${curOfficeConf}"
		fi
	fi
}

function ks_config_language()
{
	ks_config_user_language root /root

	for FILENAME in /home/*; do
		USERNAME="${FILENAME##*/}"
		[ ! -d "${FILENAME}" ] && continue
		getent passwd "${USERNAME}" >/dev/null 2>&1 || continue

		if id -u `echo ${FILENAME} | awk '{print substr($FILENAME, 7, 32)}'` >/dev/null 2>&1; then
			:
		else
			continue
		fi

		local curUserName=`echo ${FILENAME} | awk '{print substr($FILENAME, 7, 32)}'`
		local curFileName=/home/${USERNAME}
		ks_config_user_language ${curUserName} ${curFileName}
	done
}

if [ -d "/usr/share/applications/context-menus" ] && [ -f "/opt/kingsoft/wps-office/desktops/wps-office-mbset.conf" ] && [ -f "/opt/kingsoft/wps-office/desktops/../oem/mbconfig.cfg" ]; then 
	cp -f  "/opt/kingsoft/wps-office/desktops/wps-office-mbset.conf" "/usr/share/applications/context-menus"
fi

function ks_config()
{
	ks_check_library_version libfreetype.so.6
	ks_check_library_version libstdc++.so.6

	ks_install_desk
	ks_copy_icon
	ks_config_del
	ks_config_menu
	ks_config_desktop_templates
	ks_config_language

	# 教育版需要授权操作
	if [ $domesticVersion -eq 1 ] || [ $isEducation = true ]; then
		ks_transfer_auth_info
	fi

	for holdFilePath in ${holdFilePaths[@]} 
	do
		if [ -f "/tmp/wpstmp$holdFilePath" ] || [ -d "/tmp/wpstmp$holdFilePath" ] ; then 
			cp /tmp/wpstmp$holdFilePath $holdFilePath -arT
		fi
	done

	if [ -f "/opt/kingsoft/wps-office/office6/applyoldconfig.py" ]; then
		python3 /opt/kingsoft/wps-office/office6/applyoldconfig.py > /dev/null 2>&1 || true
	fi

	[ -d /tmp/wpstmp ] && rm -rf /tmp/wpstmp

	if [ -f '/usr/bin/caja' ] && [ -f '/usr/bin/xdotool' ]; then
		{ xdotool search --class caja key F5; } 2>/dev/null || true # 向caja桌面系统发送F5 解决麒麟系统安装后不刷新的问题
	fi 

	nohup fc-cache >/dev/null 2>&1 || true &
	ks_update_init
	ks_allinone_init
	ks_fuse_init
	
	if [ -f /tmp/wpsupgrade.prerm ]; then 
		rm -f /tmp/wpsupgrade.prerm
	fi
}

function ks_update_init()
{
	updatePath="/opt/kingsoft/wps-office/office6/wtool"
	if [ -d ${updatePath} ] ; then
		chmod 755 ${updatePath}
		cfgDir="${updatePath}/update"
		downDir="${updatePath}/update/down"
		if [[ ! -d "${downDir}" ]]; then
			mkdir -m 777 -p "${downDir}"
		fi
		chmod 777 "${cfgDir}"
		if [ -e "${updatePath}/update.cfg" ] ; then
			cp "${updatePath}/update.cfg" "${cfgDir}" >/dev/null 2>&1 || true
		fi
		chmod 666 "${cfgDir}/update.cfg" >/dev/null 2>&1 || true
		if [ -e "${updatePath}/wpsupdateserver" ] ; then
			chmod 754 "${updatePath}/wpsupdateserver"
		fi
		if [ -e "${updatePath}/wpsupdate" ] ; then
			[ -f /opt/kingsoft/wtool/wpsupdate ] && rm -f /opt/kingsoft/wtool/wpsupdate
			[ -f /opt/kingsoft/wtool/wpsupdateserver ] && rm -f /opt/kingsoft/wtool/wpsupdateserver

			if [ -f "/opt/kingsoft/wtool/libc++.so.1.0" ]; then
				rm -f "${updatePath}/libc++"*
			fi 
			[ -d /opt/kingsoft/wtool ] && rm -rf /opt/kingsoft/wtool/*
			cp ${updatePath}/../libssl* ${updatePath} -arp  >/dev/null 2>&1 || true 
			cp ${updatePath}/../libcrypto* ${updatePath} -arp  >/dev/null 2>&1 || true 
			cp ${updatePath}/../libQt5Core* ${updatePath} -arp  >/dev/null 2>&1 || true 
			cp ${updatePath}/../libQt5Gui* ${updatePath} -arp  >/dev/null 2>&1 || true 
			cp ${updatePath}/../libQt5Widgets* ${updatePath} -arp  >/dev/null 2>&1 || true 
			cp ${updatePath}/../libQt5Network* ${updatePath} -arp  >/dev/null 2>&1 || true 
			cp ${updatePath}/../libQt5XcbQpa* ${updatePath} -arp  >/dev/null 2>&1 || true 
			cp ${updatePath}/../libQt5DBus* ${updatePath} -arp  >/dev/null 2>&1 || true 
			cp ${updatePath}/../libauth.so ${updatePath} -arp  >/dev/null 2>&1 || true
			cp ${updatePath}/../libkbaseconfigcenter.so  ${updatePath} -arp  >/dev/null 2>&1 || true
			mkdir -p ${updatePath}/platforms
			cp ${updatePath}/../qt/plugins/platforms/libqxcb.so* ${updatePath}/platforms -arp  >/dev/null 2>&1 || true 
                        rm -f /opt/kingsoft/wtool/libQt*
			cp ${updatePath} /opt/kingsoft/ -arp >/dev/null 2>&1 || true
			cp ${updatePath}/../libc++* ${updatePath} -arp  >/dev/null 2>&1 || true 
			chmod 755 /opt/kingsoft/wtool
			strAppInfo=$(cat <<-EOF

			[app]
			officedir=/opt/kingsoft/wps-office/office6
			EOF
			)
			echo "${strAppInfo}" >> /opt/kingsoft/wtool/update/update.cfg || true
			if ! $isUos ; then
				cp /opt/kingsoft/wtool/wps-office-xcupdate.desktop /etc/xdg/autostart/ -p
				MINUTES=$((RANDOM % 60))
				echo "$MINUTES * * * * root /bin/bash -c 'sleep $((RANDOM % 60)) && /opt/kingsoft/wtool/wpsupdate /from:task'" > /opt/kingsoft/wtool/wpsupdatetask
				echo "@reboot root /opt/kingsoft/wtool/wpsupdateserver" >> /opt/kingsoft/wtool/wpsupdatetask
				cp /opt/kingsoft/wtool/wpsupdatetask /etc/cron.d/ -p
			fi
			HOME=/root /opt/kingsoft/wtool/wpsupdate /sendinstallinfo:true >/dev/null 2>&1 &
		else
			mkdir -p /opt/kingsoft/wtool
			chmod 755 /opt/kingsoft/wtool
		fi
	fi
	updateTaskPath="/etc/cron.d/wpsupdatetask"
	if [ -e ${updateTaskPath} ] ; then
		chmod 644 ${updateTaskPath}
		chown root ${updateTaskPath}
		if is_valid_group "root"; then
			chgrp root ${updateTaskPath}
		fi
	fi
	updateLogPath="/var/log/wpslog"
	if [ -d ${updateLogPath} ] ; then
		chmod 777 ${updateLogPath}
	else
		mkdir -m 777 -p ${updateLogPath}
	fi
}

function ks_allinone_init()
{
	allinonePath="/opt/kingsoft/wps-office/office6/ofdmodule"
	if [ -e "${allinonePath}/ofd" ] ; then
		if [ -e ${allinonePath}/../libc++abi.so.1 ] ; then
			ln -sf ${allinonePath}/../libc++abi.so.1 ${allinonePath}/libc++abi.so.1
		fi 
		if [ -e ${allinonePath}/../libc++.so.1 ] ; then
			ln -sf ${allinonePath}/../libc++.so.1 ${allinonePath}/libc++.so.1
		fi 
		if [ -e ${allinonePath}/../libstdc++.so.6 ] ; then
			ln -sf ${allinonePath}/../libstdc++.so.6 ${allinonePath}/libstdc++.so.6
		fi
		if [ -e ${allinonePath}/../libgcc_s.so.1 ] ; then
			ln -sf ${allinonePath}/../libgcc_s.so.1 ${allinonePath}/libgcc_s.so.1
		fi	
	fi
}

function ks_fuse_init()
{
	fusermount3Path="/opt/kingsoft/wps-office/office6/fusermount3"
	if [ -e ${fusermount3Path} ] ; then
		chown root:root ${fusermount3Path}
		chmod u+s ${fusermount3Path}
	fi
}

function ks_custom() 
{
	custom_folder="/opt/kingsoft/wps-office/office6/custom_script/install"
	for file in $custom_folder/*.sh; do
		if [[ -f "$file" && ! -h "$file" && ! -d "$file" ]]; then
			bash $file
		fi
	done
}

function ks_main()
{
	if [ $# -eq 0 ] ; then
		return;
	fi

	ks_init
	case $1 in
		configure )
			shift
			if [ "$isFullPackage" != "true" ]; then
				if ! /opt/kingsoft/wps-office/office6/cfgs/wpsupgrade/patch.py -a MYMD5SUM; then
					echo "增量补丁应用失败"
					exit 1
				fi
			fi
			if [ "$isRunScripts" = "true" ]; then
				ks_config $@
			fi
			if [ -f '/opt/kingsoft/wps-office/office6/cfgs/wpsupgrade/patch.py' ]; then
				/opt/kingsoft/wps-office/office6/cfgs/wpsupgrade/patch.py -m "/"
			fi
			if [ -e '/opt/kingsoft/wps-office/office6/mui/zh_CN/resource/help.tar' ]; then
				tar xf /opt/kingsoft/wps-office/office6/mui/zh_CN/resource/help.tar -C /opt/kingsoft/wps-office/office6/mui/zh_CN/resource >/dev/null 2>&1 || true  &
			fi
			ks_custom
			;;
		abort-remove )
			;;
		abort-upgrade )
			;;
	esac
}

args="$@"
ks_main $@

