#!/bin/bash

gBinPath=$(dirname "$0")
if [ -d "${gBinPath}/office6" ]; then
	gInstallPath=${gBinPath}
else
	gInstallPath=/usr/lib
fi

gApp=wpspdf
gConfigPath="${HOME}/.config/Kingsoft/Office.conf"
gIsFushion=0

function get_model_type()
{
	currentMode=""
	haveConf=0
	if [ -e "${gConfigPath}" ];then
		haveConf=1
	fi
	if [ $haveConf -eq 1 ]; then
		currentMode=$(awk -v line=$(awk '/\[6.0\]/{print NR}' "${gConfigPath}")  -F"=" '$1=="wpsoffice\\Application%20Settings\\AppComponentMode" {if(NR>=line){print $2}}' "${gConfigPath}")
	fi
	if [ $haveConf -eq 1 ] && [ "$currentMode" = "prome_fushion" ]; then
			gApp="wpsoffice"
			gIsFushion=1
	fi
}

function run()
{
	if [ -e "${gInstallPath}/office6/${gApp}" ] ; then
		if [ 1 -eq ${gIsFushion} ] && [ "$1" != "/prometheus" ]; then
			{ unset GIO_LAUNCHED_DESKTOP_FILE && ${gInstallPath}/office6/${gApp} /prometheus "$@"; } > /dev/null 2>&1
		else
			{ ${gInstallPath}/office6/${gApp} "$@"; } > /dev/null 2>&1
		fi
	else
		echo "${gApp} does not exist!"
	fi
}

function main()
{
	get_model_type "$@"
	run "$@"
	exit 0
}

main "$@"
