#!/bin/bash

unset TopSecAgentDir
TopSecAgentDir="/opt/apps/cn.com.topsec.edragent/files/"
if [ -z $EXEC ]; then EXEC=${TopSecAgentDir}/usr/local/bin/linux_client; fi
if [ -z $GUARD_EXEC ]; then GUARD_EXEC=${TopSecAgentDir}/usr/local/bin/update_guard; fi
if [ -z $GUARD_ALIVE ]; then GUARD_ALIVE=${TopSecAgentDir}/usr/local/bin/guard_client; fi
if [ -z $EDR_AGNET_UI ]; then EDR_AGNET_UI=${TopSecAgentDir}/Edragent.sh; fi

wait_guard_end(){
    for i in $(seq 1 10)
    do
        if ! is_running; then
            break
        fi
        echo -n "."
        sleep 1
    done
    echo
}

ensure_root() {
  if [ $UID -ne 0 ]; then
    echo "User has insufficient privileges. $0 must be run as root."
    exit 4
  fi
}

status(){
    $EXEC status
}
usage(){
    echo "Usage: $0 {uninstall|start|stop|status|restart|foreground|version}"
    exit 2
}

stop(){
    $GUARD_ALIVE stop >>"/var/log/linux_client.log"
    $EXEC stop
    $EDR_AGNET_UI stop
	  #remove_guard_crond_task >>"/var/log/linux_client.log"
}

restart(){
    sleep 3s
    stop
    sleep 1s
    start
}

start(){
    $EXEC start
    $GUARD_ALIVE start >>"/var/log/linux_client.log"
    $EDR_AGNET_UI start
#	  add_guard_crond_task >>"/var/log/linux_client.log"
}

uninstall(){
    echo "*****Thank you for using Topsec Linux Client*****"
    stop
    $EXEC remove 2>>"/var/log/linux_client.log"
    $GUARD_EXEC remove 2>>"/var/log/linux_client.log"
    $GUARD_ALIVE remove 2>>"/var/log/linux_client.log"
    remove_files
}

version(){
    $EXEC version
}

foreground(){
    $EXEC stop
    $EDR_AGNET_UI start
#	  add_guard_crond_task >>"/var/log/linux_client.log"
    $EXEC
    sleep 0.05s
}
unlock_rm(){
	chattr -i $1 >>/dev/null
	rm -f $1
}

#$1 is process name
kill_running() {
  process_id=`ps -ef | grep $1 | grep -v "grep" |grep -v runnung|awk '{print $2}'`
  for id in $process_id
  do
    kill -9 $id
  done
}
uninstall_desktop()
{
    for file in `ls /home`
    do
        if [ -d /home/$file/Desktop ];then
            if [ -f /home/$file/Desktop/$1 ];then rm -f /home/$file/Desktop/$1 ||true ;fi
        fi
        if [ -d /home/$file/桌面 ];then
            if [ -f /home/$file/桌面/$1 ];then rm -f /home/$file/桌面/$1 ||true;fi

        fi
        if [ -f /home/$file/.local/share/applications/$1 ];then
            rm -f /home/$file/.local/share/applications/$1 ||true
        fi
    done
    if [ -f /root/桌面/$1 ];then rm -f /root/桌面/$1 || true;fi
    if [ -f /root/Desktop/$1 ];then rm -f /root/Desktop/$1 || true ;fi
    if [ -f /usr/share/applications/$1 ];then rm -f /usr/share/applications/$1 ||true;fi
    if [ -f /etc/xdg/autostart/$1 ];then rm -f /etc/xdg/autostart/$1 ||true;fi
    if [ -f /usr/share/deepin/dde-file-manager/oem-menuextensions/$1 ];then rm -f /usr/share/deepin/dde-file-manager/oem-menuextensions/$1 ||true;fi
}
remove_files(){
#close runing clamd
kill_running clamd
kill_running cn.com.topsec.edragentui
uninstall_desktop topav-clientui.desktop
uninstall_desktop topav-clientui-uninstall.desktop
uninstall_desktop cn.com.topsec.edragent.uninstall.desktop
uninstall_desktop cn.com.topsec.edragent.desktop
clear_used_file_aaray=(
/usr/local/bin/tav
/usr/local/bin/guard_client
/usr/local/bin/update_guard
/usr/local/bin/linux_client
/usr/local/bin/topclientctl_server
/etc/profile.d/sysprot_run.sh
/etc/profile.d/sysprot_flag.sh
/etc/profile.d/topav_client_start.sh
)
for i in ${clear_used_file_aaray[@]}
do
   if [ -f $i ];then unlock_rm $i;fi
done
echo "[info] uninstall client files complete!"

ui_desktop_file_array=(
/usr/share/deepin/dde-file-manager/oem-menuextensions/topsec-topavscan.desktop
/usr/lib/nautilus/extensions-3.0/libnautilus-topav.so
/usr/lib/x86_64-linux-gnu/caja/extensions-2.0/libcaja-topav.so
/usr/lib/x86_64-linux-gnu/nautilus/extensions-2.0/libnautilus-topav.so
/usr/lib/x86_64-linux-gnu/nautilus/extensions-3.0/libnautilus-topav.so
/usr/lib64/nautilus/extensions-2.0/libnautilus-topav.so
/usr/lib64/nautilus/extensions-3.0/libnautilus-topav.so
/usr/lib/peony-qt-extensions/libpeony-menu-plugin-topav.so
)
for i in ${ui_desktop_file_array[@]}
do
   if [ -e $i ];then rm -rf $i;fi
done
echo "[info] uninstall client_ui files complete!"

clear_used_dir_aaray=(
/var/run/topsec
/etc/topav
/var/lib/clamav
/topsec/topav_client
/opt/apps/topclient.domesticdesktop/
/opt/apps/cn.com.topsec.edragent/
)
for i in ${clear_used_dir_aaray[@]}
do
   if [ -d $i ];then rm -rf $i;fi
done
echo "[info] uninstall client directory complete!"

#删除完毕
echo "uninstall Topsec Linux Client finished!"

rm -f $0 
}

ui_uninstall(){
    echo "*****Thank you for using Topsec Linux Client*****"
    $GUARD_ALIVE stop >>"/var/log/linux_client.log"
    $GUARD_EXEC remove 2>>"/var/log/linux_client.log"
    $GUARD_ALIVE remove 2>>"/var/log/linux_client.log"
	  #remove_guard_crond_task >>"/var/log/linux_client.log"
    $EDR_AGNET_UI uninstall
    dpkg -P cn.com.topsec.edragent 2>/dev/null || true
    dpkg -P topclient.domesticdesktop 2>/dev/null || true
    remove_files
    $EXEC remove 2>>"/var/log/linux_client.log"
    kill_running "linux_client"
}

start_guard(){
    $GUARD_ALIVE start >>"/var/log/linux_client.log"

#	  add_guard_crond_task >>"/var/log/linux_client.log"
}
update_guard(){
    $GUARD_ALIVE start >>"/var/log/linux_client.log"
}

#添加定时检查guard_agent进程是否存活crontab任务
add_guard_crond_task()
{
    crontab -l | sed  "/guard_agent/d;/^#/d" > /tmp/linux_client_cron.rule
    ret=$?
    if [[ $ret -ne 0 ]] && [[ $ret -ne 1 ]]; then
        echo "conf crontab fail"
        return 1
    fi

    echo "*/10 * * * * /bin/bash ${TopSecAgentDir}/usr/local/bin/guard_agent start >> /var/log/linux_client.log > /dev/null 2>&1" >> /tmp/linux_client_cron.rule

    crontab /tmp/linux_client_cron.rule
    if [ $? -ne 0 ]; then
        echo "restore contab fail"
        return 1
    fi
    reload_cron
}

#删除定时检查guard_agent进程是否存活crontab任务
remove_guard_crond_task()
{
    crontab -l | sed  "/guard_agent/d;/^#/d" > /tmp/linux_client_cron.rule
    ret=$?
    if [[ $ret -ne 0 ]] && [[ $ret -ne 1 ]]; then
        echo "conf crontab fail"
        return 1
    fi

    crontab /tmp/linux_client_cron.rule
    if [ $? -ne 0 ]; then
        echo "restore contab fail"
        return 1
    fi
    reload_cron
}

#重新加载crontab任务
reload_cron()
{
    service crond reload 2> /dev/null
    ret=$?
    if [ $ret -eq 0 ]; then
        return 0
    fi
    if [ $ret -eq 7 ]; then
        echo "crond is stop, please start crond"
        return 255;
    fi
    service cron reload 2> /dev/null
    if [ $? -eq 0 ]; then
        return 0
    fi
    echo "reload cron fail"
    return 255
}

main(){
    ensure_root
     case "$1" in
        start)
          $1
          ;;
        stop)
        kill_running client_ui
          $1
          ;;
        restart)
          $1
          ;;
        status)
          $1
          ;;
        foreground)
          $1
          ;;
        uninstall)
          $1
          ;;
        ui_uninstall)
          $1
          ;;
        version)
          $1
          ;;
        start_guard)
          $1
          ;;
        *)
          usage
          ;;
      esac
}
main $@
