#!/bin/bash

gOpt=
#gOptExt=-multiply
gTemplateExt=("ett" "xlt" "xltx" "xltm" "dpt" "pot" "potx" "potm" "wpt" "dot" "dotx" "dotm")
gBinPath=$(dirname "$0")
if [ -d "${gBinPath}/office6" ]; then
	gInstallPath=${gBinPath}
else
	gInstallPath=/usr/lib
fi
gApp=wps
gDaemon=0
gIsCloud=0
gIsOA=0
gIsCloudOA=0
gIsUrl=0
gFilePath=
gFilePaths=()
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 parse_arg()
{
	if [ "$1" == "-quickstart" ]; then
		gOpt=-quickstart
		gDaemon=1
		return 0
	fi
	get_model_type "$@"
	if [ $# -eq 1 ] ; then
		if [[ $1 == ksoqing://* ]] || [[ $1 == ksowps://* ]] || [[ $1 == ksowpp://* ]] || [[ $1 == ksoet://* ]]
		then
			gIsCloud=1
		fi

		if [[ $1 == ksowebstartupwps://* ]] || [[ $1 == ksowebstartupwpp://* ]] || [[ $1 == ksowebstartupet://* ]]
		then
			gIsOA=1
		fi
		if [[ $1 == ksowpscloudsvr://* ]]
		then
			gIsCloudOA=1
		fi

		gFilePath=$1
		if [ ${gFilePath:0:7} = "file://" ]; then
			gFilePath=$(python -c 'import sys, urllib; print urllib.unquote(sys.argv[1])' "$1")
			gFilePath=${gFilePath#file://}
			gIsUrl=1
		fi

		ext="${1##*.}"
		if [ "" = "${ext}" ] ; then
			return 0
		fi

		for i in "${gTemplateExt[@]}"
		do
			if [ "${ext}" = "${i}" ] ; then
				gOpt=-t
			fi
		done
		gFilePaths=$gFilePath
	elif [ $# -gt 1 ] ; then
		for file in "$@"
		do 
			gFilePath=$file
			if [ ${gFilePath:0:7} = "file://" ]; then
				gFilePath=$(python -c 'import sys, urllib; print urllib.unquote(sys.argv[1])' "$file")
				gFilePath=${gFilePath#file://}
			else
				return 0					
			fi
				
			gIsUrl=1
			gFilePaths[${#gFilePaths[@]}]=$gFilePath
		    
		done 
	fi
}

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

function main()
{
	parse_arg "$@"
	if [ 1 -eq ${gIsCloud} ]; then
		${gInstallPath}/office6/wpscloudsvr "/qingbangong" "$@"
	elif [ 1 -eq ${gIsOA} ]; then
		${gInstallPath}/office6/oa/wpsoaassist.sh "$@"
	elif [ 1 -eq ${gIsCloudOA} ]; then
		${gInstallPath}/office6/wpscloudsvr "/jsapihttpserver" "$@"		
	else
		run "$@"
	fi
	exit 0
}

main "$@"
