#!/bin/bash

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

ins_info_dir="/opt/bdfz/jyn/insinfo"
jyoem="${ins_info_dir}/jyoem"
ccip="${ins_info_dir}/ccip"
domain_path="${ins_info_dir}/jydomain"
nostart_path="${ins_info_dir}/jynostart"
rs_path="${ins_info_dir}/rspath"

set_ip_or_domain()
{
	echo setup_time=`date +%s` >> /opt/bdfz/jyn/JingyunUiConfig.ini 
	ip=`cat $ccip`

	if [ "$ip" != "" ]; then
		sed -i "/^Url.1/cUrl.1=http://$ip" /opt/bdfz/jyn/Config/UrlService.ini
		sed -i "/^ServiceUrl.10/cServiceUrl.10=http://$ip[tcp:3006]" /opt/bdfz/jyn/NetService.ini
	fi	
	domain=`cat $domain_path`
	if [ "$domain" != "" ]; then
		sed -i "/^Url.1/cUrl.1=http://$domain" /opt/bdfz/jyn/Config/UrlService.ini
		sed -i "/^ServiceUrl.10/cServiceUrl.10=http://$domain[tcp:3006]" /opt/bdfz/jyn/NetService.ini
	fi
	nostart=`cat $nostart_path`
}

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

is_ui()
{
	CTYPE_VALUE=`cat $jyn_install_path/Config.ini|grep -v "Mon"|grep -v "Version"|grep "Type"`
    if [ ${CTYPE_VALUE:17:1} == "1" ];then
        echo "1"
    fi
}

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

set_xdg_runtime_dir()
{
    username=`last -w |grep log |head -n 1 |awk '{print $1}'`
    #uid=`runuser -l $username -c 'echo $UID'`      
    uid=`getent passwd $username|awk -F : '{print $3}'`
    if [ -z $XDG_RUNTIME_DIR ] ;then
            export XDG_RUNTIME_DIR=/run/user/$uid
    fi
    echo $XDG_RUNTIME_DIR
}

copy_scan_menu()
{
	lib_menu_suport_list="caja nautilus peony"
	#file support: nemo , dde-filemanager

	for each in $lib_menu_suport_list
	do
		if command_exists $each ; then
			check_pack="$each $each-open-terminal"

			if command_exists dpkg; then
                lib_target_dir=`dpkg -L  $check_pack | grep extensions | head -n 1`
			fi

			if command_exists rpm; then
                lib_target_dir=`rpm -ql $check_pack| grep extensions | head -n 1`
			fi

            if [ "$lib_target_dir"e != ""e ]; then
                postfix=${lib_target_dir##*/}
                if [[ ! $postfix =~ "extensions" ]]; then
                    lib_target_dir=${lib_target_dir%/*}
                fi
			    echo "copy $each to $lib_target_dir"
			    cp -b $jyn_install_path/gjyn/$each/* $lib_target_dir
                rs_lib_name=`ls $jyn_install_path/gjyn/$each/*`
                rs_lib_name=${rs_lib_name##*/}
			    echo "$lib_target_dir/$rs_lib_name" > $rs_path
			    return
            fi
		fi
	done


	if command_exists nemo ; then
		echo "copy nemo files"
		cp -b $jyn_install_path/gjyn/nemo/jynscan.nemo_action /usr/share/nemo/actions/
		return
	elif command_exists dde-file-manager ; then
		echo "cp dde-file-manager files"
		cp -b $jyn_install_path/gjyn/dde/deepin-chenxinsd.desktop  /usr/share/deepin/dde-file-manager/oem-menuextensions/
		return
	fi
}

start_run(){
	if test -f /etc/init.d/jyngjcz ; then
        service jyngjcz stop
        service jyngjcz start
    fi
    if test -f /etc/init.d/jyngltx ; then
        service jyngltx stop
        service jyngltx start
    fi
    if test -f /etc/init.d/jynzdfy ; then
        service jynzdfy stop
        service jynzdfy start
    fi
    if test -f /etc/init.d/jynengsvr ; then
        service jynengsvr stop
        service jynengsvr start
    fi
	if [ "$(is_ui)"e = "1"e ] ; then
		set_xdg_runtime_dir
        if test -f "/tmp/rpminstall.log"; then
            rm -f /tmp/rpminstall.log
        fi
		sudo -u `$jyn_install_path/cur_user | head -1` -H -E $jyn_install_path/JYNRJJH1 -autodisp>> /tmp/rpminstall.log 2>&1 &
	fi
}

cp_config()
{
	instver=`cat $jyn_install_path/Config.ini|grep InstallVersion`
	virusver=`cat $jyn_install_path/Config.ini|grep VirusVersion`
	restore $jyn_install_path/Config.ini
	sed -i "/^InstallVersion/c$instver" $jyn_install_path/Config.ini
	sed -i "/^VirusVersion/c$virusver" $jyn_install_path/Config.ini
	sed -i "/^SelfProtect/cSelfProtect = 1" $jyn_install_path/Config.ini
}

cp_file()
{
    if [ ! -e /etc/udev/rules.d/90-mtoken.rules ];then
        cp $jyn_install_path/RJJHGLTX/90-mtoken.rules /etc/udev/rules.d/90-mtoken.rules
    fi
	if [[ $(is_ui) && ! -e /etc/xdg/autostart/JYNRJJH2.desktop ]] ;then
		cp $jyn_install_path/scripts/JYNRJJH2.desktop /etc/xdg/autostart/
	fi
	restore $jyn_install_path/RJJHGLTX/AuthConfig.ini
	restore $jyn_install_path/RJJHGLTX/AuthConfig.dat
	restore $jyn_install_path/Config/UrlService.ini
	restore $jyn_install_path/NetService.ini
	cp_config

    # 兼容4004 前版本， 保留隔离区文件
    if [ -d $jyn_install_path/ISO_FILES ];then
		file_num=`ls -l $jyn_install_path/ISO_FILES | wc -l`
        if [ $file_num -gt 1 ];then
            cp -uv $jyn_install_path/ISO_FILES/* $jyn_install_path/.quarzone/
        fi
    fi

    #拷贝右键菜单
    copy_scan_menu	

    if test -f $jyn_install_path/scripts/check_cpplib.sh ; then
    	$jyn_install_path/scripts/check_cpplib.sh
	fi

    if [ $udisk_flag -ne 1  ] ; then
        #copy install path
        if [[ ! -d "/etc/jyn"   ]]; then
            mkdir /etc/jyn
        fi
        cp $jyn_install_path/path.conf  /etc/jyn/
        if test -f $jyn_install_path/JYNQuickCliq ; then
            rm $jyn_install_path/JYNQuickCliq
            rm $jyn_install_path/JYNMonitoring
        fi
    else
        mv $jyn_install_path/JYNQuickCliq $jyn_install_path/../
        mv $jyn_install_path/JYNMonitoring $jyn_install_path/../
    fi

    if [ ! -f "${jyn_install_path}/RJJHetc/JingyunUiConfig.ini" ];then
	    echo setup_time=`date +%s` >> $jyn_install_path/JingyunUiConfig.ini 
        cp "${jyn_install_path}/JingyunUiConfig.ini" ${jyn_install_path}/RJJHetc/
    fi

    echo `dmidecode -t1 | grep Serial\ Number | awk -F: '{print $2}'` > $jyn_install_path/RJJHGLTX/SerialNumber
}

changmode()
{
    chmod +x $jyn_install_path/JYN*
    chmod +x $jyn_install_path/scripts/*.sh
    chmod +x $jyn_install_path/scripts/uninstall
    chmod 777 $jyn_install_path
    chmod 666 $jyn_install_path/Config/*.ini
    chmod 666 $jyn_install_path/*.ini
    chmod 666 $jyn_install_path/*.dat
    chmod 777 $jyn_install_path/zav
    chmod 777 $jyn_install_path/zav/ZAVUpdate.xml
    chmod 777 $jyn_install_path/zav/zbase -R
    chmod 777 $jyn_install_path/RJJHGLTX/ -R
    chmod 777 $jyn_install_path/RJJHGJCZ/ -R
    chmod 777 $jyn_install_path/RJJHZDFY/ -R
    chmod 777 $jyn_install_path/RJJHetc/ -R
    if [ -d $jyn_install_path/.quarzone ] ; then
        chmod 777 $jyn_install_path/.quarzone/ -R
    fi
}

set_startup()
{
	echo "install startup"
	$jyn_install_path/JYNSetStartup install
}

case "$1" in
    udisk)
        udisk_flag=1
        cp_file
        changmode
        ;;
    *)
        cp_file
        changmode
        set_startup
        set_ip_or_domain
        start_run
        ;;
esac

