#!/bin/bash

RECORD_PKGS_VERSION=(kylin-system-updater kylin-activation kylin-update-frontend libkylin-activation 
                    ukui-notification-daemon ukui-session-manager)

#系统升级收集bug日志使用
if [ $(id -u) -eq 0 ]; then
    echo "当前执行权限是root，请使用普通权限来执行"
    exit 1
fi

BOLD="$(tput bold 2>/dev/null || printf '')"
GREY="$(tput setaf 0 2>/dev/null || printf '')"
UNDERLINE="$(tput smul 2>/dev/null || printf '')"
RED="$(tput setaf 1 2>/dev/null || printf '')"
GREEN="$(tput setaf 2 2>/dev/null || printf '')"
YELLOW="$(tput setaf 3 2>/dev/null || printf '')"
BLUE="$(tput setaf 4 2>/dev/null || printf '')"
MAGENTA="$(tput setaf 5 2>/dev/null || printf '')"
NO_COLOR="$(tput sgr0 2>/dev/null || printf '')"

info() {
  printf '%s\n\n' "${BOLD}${GREY}>${NO_COLOR} $*"
}

warn() {
  printf '%s\n' "${YELLOW}! $*${NO_COLOR}"
}

error() {
  printf '%s\n' "${RED}x $*${NO_COLOR}" >&2
}

completed() {
  printf '%s\n' "${GREEN}✓${NO_COLOR} $*"
}


cleanup() {
    info "${BOLD}捕获终端信号，直接进行打包日志...${NO_COLOR}"
    tar_upgrade_log
    exit 0
}

tar_upgrade_log() {
    info "${BOLD}正在压缩日志...${NO_COLOR}"

    outputName="$(date +%m-%d,%H-%M-%S)-updaterLog.tar.gz"
    #将所有的日志进行打包
    tar -czvf updaterLog.tar.gz updaterlog >/dev/null

    #删除收集的日志目录
    rm -rf updaterlog

    #将文件存储到桌面
    if [ ! -d ~/桌面 ]; then
        mv updaterLog.tar.gz ~/Desktop/$outputName
        info "${BOLD}请将桌面下日志文件提交给管理员,输出位置：${UNDERLINE}~/Desktop/$outputName${NO_COLOR}"
    else
        mv updaterLog.tar.gz ~/桌面/$outputName
        info "${BOLD}请将桌面下日志文件提交给管理员,输出位置：${UNDERLINE}~/桌面/$outputName${NO_COLOR}"
    fi
}

main() {

    #建立收集的log目录
    mkdir updaterlog

    date >> updaterlog/base-info

    # 记录包版本
    for pkg in "${RECORD_PKGS_VERSION[@]}"; do
        dpkg -l | grep $pkg >> updaterlog/base-info
    done

    info "${BOLD}正在导出系统更新日志...${NO_COLOR}"

    # 收集当前系统配置
    cp /var/lib/kylin-system-updater/sources.list updaterlog/private-sources.list >/dev/null 2>&1 || true 
    cp /etc/apt/sources.list updaterlog/system-sources.list >/dev/null 2>&1 || true 
    cp -r /etc/apt/apt.conf.d updaterlog >/dev/null 2>&1 || true 
    cp -r /usr/share/kylin-update-desktop-config updaterlog >/dev/null 2>&1 || true 
    cp -r /var/lib/kylin-system-updater/json updaterlog/upgrade-data >/dev/null 2>&1 || true 
    cp -r /var/cache/kylin-system-updater updaterlog/upgrade-data >/dev/null 2>&1 || true 
    cp -r /var/lib/dpkg/status updaterlog >/dev/null 2>&1 || true 

    #收集系统更新的日志
    cp -r /var/log/apt updaterlog >/dev/null 2>&1 || true 
    cp -r /var/log/dpkg.log updaterlog >/dev/null 2>&1 || true 
    cp -r /var/log/kylin-unattended-upgrades updaterlog >/dev/null 2>&1 || true 
    cp -r ~/.config/kylin-background-upgrade updaterlog >/dev/null 2>&1 || true 
    cp -r /var/log/kylin-system-updater/ updaterlog >/dev/null 2>&1 || true 
    cp -r /var/log/syslog updaterlog >/dev/null 2>&1 || true 

    #激活
    mkdir -p updaterlog/kylin-activation
    cp -r ~/.log/kylin-activation/ updaterlog/kylin-activation >/dev/null 2>&1 || true 
    cp /etc/LICENSE updaterlog/kylin-activation >/dev/null 2>&1 || true 
    cp /etc/.kyinfo updaterlog/kylin-activation >/dev/null 2>&1 || true 

    #P2P
    cp -r /var/log/apt-p2p.log updaterlog >/dev/null 2>&1 || true 

    # 源更新管理器
    cp -r /var/log/kylin-software-properties/ updaterlog >/dev/null 2>&1 || true 

    #收集前端日志
    cp -r ~/.log/kylin-update-frontend-notifysend.log updaterlog >/dev/null 2>&1 || true
    cp -r ~/.log/ukui-control-center.log updaterlog >/dev/null 2>&1 || true 
    cp -r ~/.log/ukui-notification-daemon.log updaterlog >/dev/null 2>&1 || true
    cp -r ~/.config/ukui-session/ updaterlog >/dev/null 2>&1 || true
    cp -r /tmp/kylin-updateresult-notify.log updaterlog >/dev/null 2>&1 || true

    tar_upgrade_log
}

main "$@"