#!/bin/bash

current_script=$(cd `dirname $0`;pwd)
BASH_PATH=$(dirname "$current_script")
LIB_PATH="${BASH_PATH}/lib/externals"

#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$BASH_PATH
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIB_PATH

IS_BUS="1"
if [ "e`cat /etc/issue | grep "Ubuntu 20"`" != "e" ];then
    IS_BUS="0"
fi
RJJH_PARA=$@

if test -f $JEMALLOC1_PATH ; then
    export LD_PRELOAD=$JEMALLOC1_PATH
elif test -f $JEMALLOC2_PATH ; then
    export LD_PRELOAD=$JEMALLOC2_PATH
fi

if [[ `command -v ibus` && $IS_BUS == "1" ]] ; then
    export GTK_IM_MODULE=ibus
    export QT_IM_MODULE=ibus
    export XMODIFIERS=@im=ibus
fi

if [[ `command -v fcitx` && $IS_BUS == "1" ]] ; then
    typewriting="-platforminputcontext fcitx"
fi

process_name="JYNSAFED"

# 保证助手先初始化完成，数据能够同步给rjjh
while true; do
    if pgrep -x "JYNRJJH2" > /dev/null; then
        break
    fi

    if pgrep -x "$process_name" > /dev/null; then
        sleep 3
        break
    else
        sleep 1
    fi
done

if [ -f $BASH_PATH/insinfo/desktop_path  ]; then
	desktop_path=`cat $BASH_PATH/insinfo/desktop_path | head -n1`
	if [ -f $desktop_path ]; then
		if [ -f /etc/issue ] ;then
			gio set $desktop_path metadata::trusted yes
			# 自Ubuntu 20.04开始，桌面快捷方式加白由yes变为true
			local_version=`cat /etc/issue | head -n1 | cut -b 1-12`
			if [[ "${local_version}" > "Ubuntu 20.03" ]];then
				gio set $desktop_path metadata::trusted true
			fi
		fi
	fi
fi

$BASH_PATH/bin/JYNRJJH2 "$RJJH_PARA" -platform xcb "$typewriting"

exit 0
