#!/bin/bash

jyn_install_path=$(cd `dirname $0`;pwd)

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/NetService.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(){
	echo "rm gjyn : $1 $2"
	if [ -f "/usr/lib/x86_64-linux-gnu/peony-extensions/$2" ];then
		rm /usr/lib/x86_64-linux-gnu/peony-extensions/$2
	elif [ -f "/usr/lib/mips64el-linux-gnuabi64/peony-extensions/$2" ];then
		rm /usr/lib/mips64el-linux-gnuabi64/peony-extensions/$2
	elif [ -f "/usr/lib/aarch64-linux-gnu/peony-extensions/$2" ];then
		rm /usr/lib/aarch64-linux-gnu/peony-extensions/$2
	elif [ -f "/usr/lib/mips64el-linux-gnu/$1/extensions-2.0/$2" ];then
		rm /usr/lib/mips64el-linux-gnu/$1/extensions-2.0/$2
	elif [ -f "/usr/lib/$1/extensions-3.0/$2" ];then
		rm /usr/lib/$1/extensions-3.0/$2
	elif [ -f "usr/lib64/$1/extensions-3.0/$2" ];then
		rm /usr/lib64/$1/extensions-3.0/$2
	elif [ -f "/usr/lib/$1/extensions-2.0/$2" ];then
		rm /usr/lib/$1/extensions-2.0/$2
	elif [ -f "/usr/lib64/$1/extensions-2.0/$2" ];then
		rm /usr/lib64/$1/extensions-2.0/$2
	elif [ -f "/usr/lib/aarch64-linux-gnu/caja/extensions-2.0/libcaja-gjyn.so" ];then
		rm /usr/lib/aarch64-linux-gnu/caja/extensions-2.0/libcaja-gjyn.so
	else
		echo "gjyn.so is failure"
	fi
}

rm_file()
{
    echo "rm all files : [$1]" 
    if [ "$1"e = ""e ] ; then
        find $jyn_install_path | tail -n +2 | 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 {}

		sed -i "/^RegisteredUrl = /cRegisteredUrl = http://" $jyn_install_path/Config/UrlService.ini.old

        if test -e /etc/xdg/autostart/JYNRJJH2.desktop ; then
            rm /etc/xdg/autostart/JYNRJJH2.desktop
        fi
    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-chenxinsd.desktop
	elif command_exists peony ; then
		echo "rm peony files"
		rm_gjyn peony libpeony-gjyn.so
	fi
}

stop_svr()
{
    if command -v "update-rc.d" &> /dev/null ; then
        echo "update-rc.d stop"
        if test -f /etc/init.d/jyngjcz ; then
            service jyngjcz stop
        fi
        if test -f /etc/init.d/jyngltx ; then
            service jyngltx stop
        fi
        if test -f /etc/init.d/jynzdfy ; then
            service jynzdfy stop
        fi
        if test -f /etc/init.d/jynengsvr ; then
            service jynengsvr stop
        fi
    elif command -v "chkconfig" &> /dev/null ; then
        echo "chkconfig stop" 
        if test -f /etc/init.d/jyngjcz ; then
            service jyngjcz stop
        fi  
        if test -f /etc/init.d/jyngltx ; then
            service jyngltx stop
        fi  
        if test -f /etc/init.d/jynzdfy ; then
            service jynzdfy stop
        fi  
        if test -f /etc/init.d/jynengsvr ; then
            service jynengsvr stop
        fi  
    else
		if `pidof JYNGJCZ2 > /dev/null`; then
			kill -9 `pidof JYNGJCZ2`
		fi
		if `pidof JYNGLTX1 > /dev/null`; then
			kill -9 `pidof JYNGLTX1`
		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
	fi
}

delete_self_protect()
{
	echo "delete self protect"
	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`

	$jyn_install_path/filemonitor stopSelfProtect

	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
}

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

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

uninstll_report()
{
        $jyn_install_path/UninstllReport
}

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 "chenxinsd" $jyn_install_path/insinfo/sd_ins_log` -ne '0' ]; then
		oem="chenxinsd"
	elif [ `grep -c "vrvsd" $jyn_install_path/insinfo/sd_ins_log` -ne '0' ]; then
		oem="vrvsd"
	else
		oem="LeadSec"
	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
}

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

echo "uninstall finish"

