#!/bin/bash

jyn_install_path=$(cd `dirname $0`;pwd)
ins_info_dir="$jyn_install_path/insinfo"
rs_path="${ins_info_dir}/rspath"
soft_name=${jyn_install_path%/}
soft_name=${soft_name##*/}
desktop_name="${soft_name}.desktop"


command_exists(){
	command -v "$1" &> /dev/null;
}

backup(){
    if test -e $1; then
        cp $1 $1".old"
    fi
}

backup_file()
{
    backup $jyn_install_path/RJJHGLTX/AuthConfig.ini
    backup $jyn_install_path/RJJHGLTX/AuthConfig.dat
    backup $jyn_install_path/Config/UrlService.ini
    backup $jyn_install_path/Config.ini
    backup $jyn_install_path/RJJHGLTX/UpConfig.ini

    if test -e "$jyn_install_path/RJJHGJCZ/isolate_bak.txt"; then
        cp $jyn_install_path/RJJHGJCZ/isolate_bak.txt $jyn_install_path/RJJHGJCZ/isolate_bak.txt.old
        # .log 不使用 用于排查备份
        cp $jyn_install_path/RJJHGJCZ/isolate_bak.txt $jyn_install_path/RJJHGJCZ/isolate_bak.txt.log
    fi
}

rm_gjyn(){
    if test -e $rs_path ; then
		echo "rm `cat $rs_path`"
        rm `cat $rs_path`
    fi

	dskpath=`cat $jyn_install_path/insinfo/desktop_path | head -1`
	if test -e $dskpath ; then
		rm $dskpath
	fi

	curuser=`$jyn_install_path/cur_user | head -1`
	if [ "$curuser" == "root" ]; then
		desktop_path=/$curuser/Desktop
	else
		desktop_path=/home/$curuser/Desktop
	fi
	if [ ! -d $desktop_path ] ; then
		desktop_path=${desktop_path%/*}/桌面
	fi
	if test -e $desktop_path/$desktop_name ; then
		rm $desktop_path/$desktop_name
	fi
}

smui_config()
{
	#从配置文件中删除涉密界面版托盘,如果配置文件存在的话
	if test -e "/opt/software/config.ini"; then
		sed -i '/jingyunsd/,+5d' /opt/software/config.ini
	fi
}

rm_file()
{
    echo "rm all files : [$1]" 
    if [ "$1"e = ""e ] ; then
        find $jyn_install_path -type f | grep -v TXAlert.so | grep -v UrlService.ini.old | grep -v Config.ini.old | grep -v UpConfig.ini | grep -v .quarzone | grep -v RJJHetc | grep -v RJJHG | grep -v RJJHZDFY | xargs -i rm -rf {}
		find $jyn_install_path -type d -empty -delete

        if test -e /etc/xdg/autostart/$desktop_name ; then
            rm /etc/xdg/autostart/$desktop_name
        fi

        if test -e /usr/share/applications/$desktop_name ; then
            rm /usr/share/applications/$desktop_name
        fi
    fi

	if [ -f $jyn_install_path/Config/UrlService.ini.old ] ; then
		sed -i "/^RegisteredUrl = /cRegisteredUrl = http://" $jyn_install_path/Config/UrlService.ini.old
	fi

	if test -e /etc/udev/rules.d/90-mtoken.rules ;then
		rm /etc/udev/rules.d/90-mtoken.rules
	fi

	if test -e /etc/jyn/path.conf ;then
		rm -rf /etc/jyn
	fi

	# if command_exists nemo ; then
	# 	echo "rm nemo files"
	# 	rm /usr/share/nemo/actions/jynscan.nemo_action
	# elif command_exists caja ; then
	# 	echo "rm caja files"
	# 	rm_gjyn caja libcaja-gjyn.so
	# elif command_exists nautilus ; then
	# 	echo "rm nautilus files"
	# 	rm_gjyn nautilus libnautilus-gjyn.so
	# elif command_exists dde-file-manager ; then
	# 	echo "rm dde-file-manager files"
	# 	rm /usr/share/deepin/dde-file-manager/oem-menuextensions/deepin-vrvsd-tl.desktop
	# elif command_exists peony ; then
	# 	echo "rm peony files"
	# 	rm_gjyn peony libpeony-gjyn.so
	# fi
	rm -rf $jyn_install_path/rtl $jyn_install_path/platforminputcontexts $jyn_install_path/platforms $jyn_install_path/xcbglintegrations
}

stop_svr()
{
	arr_svr_name=(jyngjcz jyngltx jynzdfy jynengsvr jynproc)
	for svr_name in ${arr_svr_name[@]}; do
		#服务配置文件是在set_startup被拷贝的
		if command -v "systemctl" &> /dev/null ; then
			if [ "`systemctl list-unit-files | grep $svr_name.service`" != "" ] ;then
				systemctl stop $svr_name
			fi
		elif test -f /etc/init.d/$svr_name ; then			
				service $svr_name stop
		fi
	done

	#异常容错,kill掉依旧存在的进程
	if `pidof JYNGJCZ2 > /dev/null`; then
		kill -9 `pidof JYNGJCZ2`
	fi
	if `pidof JYNGLTX1 > /dev/null`; then
		kill -9 `pidof JYNGLTX1`
	fi
	if `pidof JYNPROC1 > /dev/null`; then
		kill -9 `pidof JYNPROC1`
	fi
	if `pidof filemonitor > /dev/null`; then
		kill -9 `pidof filemonitor`
	fi
	if `pidof jy_tasksvr > /dev/null`; then
		kill -9 `pidof jy_tasksvr`
	fi
	if `pidof JingyunsdIPCServer > /dev/null`; then
		kill -9 `pidof JingyunsdIPCServer`
	fi
	if `pidof JingyunSdCtrlCenter > /dev/null`; then
		kill -9 `pidof JingyunSdCtrlCenter`
	fi

	#驱动在确保JYNZDFY2(filemonitor)关闭之后进行卸载 防止因为被使用
	#进而导致卸载失败遗留系统中影响系统性能 或 导致下一次驱动安装失败
	smUi=`grep "ZYJ = 1" $jyn_install_path/Config/Version.ini | awk '{print $3}' | tr -d "\n"`
	if [ -n "${smUi}" ] ; then
		#卸载内核模块
		KO_PATH=$jyn_install_path/ZDFY001.ko
		if test -e $KO_PATH ; then
			rmmod ZDFY001
		fi
	fi
}

delete_self_protect()
{
	echo "delete self protect"
	if [ -f $jyn_install_path/filemonitor ]; then
		$jyn_install_path/filemonitor stopSelfProtect
	fi
	if [ -f $jyn_install_path/Config.ini ]; then
		smUi=`grep "ZYJ = 1" $jyn_install_path/Config.ini | awk '{print $3}' | tr -d "\n"`
	fi
	if [ ! -n "${smUi}" ] ; then
		PRELOAD_NAME=zyhook.so
		LD_HOOK_PATH=`ldconfig -p | grep libc.so | head -n 1 |  awk '{print $NF}'`
		LD_HOOK_PATH=`dirname $LD_HOOK_PATH`
	
		if test -e /etc/ld.so.preload; then
		    sed -i "/$PRELOAD_NAME/d" /etc/ld.so.preload
		fi
	
		if test -e $LD_HOOK_PATH/$PRELOAD_NAME; then
	        	rm $LD_HOOK_PATH/$PRELOAD_NAME
		fi
	fi
}

stop_run()
{
	echo "stop all"
	stop_svr
	if [ -f $jyn_install_path/filemonitor ]; then
		$jyn_install_path/filemonitor stopSelfProtect
	fi
	if `pidof JYNRJJH2 > /dev/null`; then
		echo "stop JYNRJJH"
		kill -9 `pidof JYNRJJH2`
	fi
	if `pidof ZyUDiskTray_UI > /dev/null`; then
		echo "stop ZyUDiskTray_UI"
		kill -9 `pidof ZyUDiskTray_UI`
	fi
}

unset_startup()
{
	echo "uninstall startup"
	$jyn_install_path/JYNSetStartup uninstall $1
}

uninstll_report()
{
	if [ -f $jyn_install_path/UninstllReport ]; then
	        $jyn_install_path/UninstllReport
	fi
}

wait_agree_remove()
{
	echo "等待用户确认是否卸载 ..."
	$jyn_install_path/JYNRJJH1 --uninstall
	if [ `grep -c "jingyunsd" $jyn_install_path/insinfo/sd_ins_log` -ne '0' ]; then
		oem="jingyunsd"
	elif [ `grep -c "vrvsd-tl" $jyn_install_path/insinfo/sd_ins_log` -ne '0' ]; then
		oem="vrvsd-tl"
	elif [ `grep -c "vrvsd" $jyn_install_path/insinfo/sd_ins_log` -ne '0' ]; then
		oem="vrvsd"
	else
		oem="TianxunSd"
	fi  

	while [ 1 ] 
	do
		agreefile="/tmp/uninstall_rjjh.ini"
		if [ -f $agreefile ] ; then
			echo "$agreefile 存在"
			flag=`cat $agreefile`
			echo "$flag - $oem"
			if [ "$flag" == "cancel" ] ; then
				if command -v "dpkg" &> /dev/null ; then
					sudo kill -9 `ps -ef | grep dpkg | grep $oem | grep -v grep | awk '{print $2}'`
				else
					sudo kill -9 `ps -ef | grep rpm | grep $oem | grep -v grep | awk '{print $2}'`
				fi
				exit 1
			else
				return
			fi
			rm $agreefile
		fi  
		sleep 1000
	done
}

backups_dir()
{
	mkdir -p /var/jyn/backups/$jyn_install_path
	cp -rf $jyn_install_path/* /var/jyn/backups/$jyn_install_path/
	res=`ls -A /opt/apps`
	if [ -z $res ] ; then
		rm -rf /opt/apps
	else
		rm -rf $jyn_install_path
	fi
}

case "$1" in
	upgrade)
		delete_self_protect
		stop_run
		backup_file
    ;;
	auto)
        #临时注释
        #wait_agree_remove
		rm_gjyn
		delete_self_protect
        stop_run
        backup_file
        unset_startup auto
        uninstll_report
		smui_config
        rm_file auto
    ;;
    *)
		#临时注释
		#wait_agree_remove
		rm_gjyn
		delete_self_protect
		stop_run
		backup_file
		unset_startup
		uninstll_report
		rm_file
		backups_dir
    ;;
esac

echo "uninstall finish"

