#!/bin/bash -x

#LOG_FILE="/tmp/diagnose.log"

# Redirect all output to the log file
#exec >> "$LOG_FILE" 2>&1
basearch=$(uname -m)

main()
{
    ###we need to update the sysak rpm###
    rpm -qa | grep sysak
    if [ $? -eq 0 ]
    then
	    echo "Already installed sysak!"
        exit 1
    fi

    if grep -q "BigCloud Enterprise Linux 21.10U4 LTS"  /etc/os-release; then
	    echo "os info: oe21.10u4"
        repo_content=$(cat <<EOF
[sysak-oe21.10u4]
name=BC-Linux-release - om
baseurl=http://mirrors.cmecloud.cn/bclinux/product/bclinux_om/oe21.10U4/$basearch/os/
enabled=1
gpgcheck=0
EOF
        )
        echo "$repo_content" > /etc/yum.repos.d/sysak.repo
        yum clean all && yum makecache
        sleep 1
    elif grep -q "BigCloud Enterprise Linux For Euler 22.10 LTS" /etc/os-release; then 
	    echo "os info: oe22.10"
        repo_content=$(cat <<EOF
[sysak-oe22.10]
name=BC-Linux-release - om
baseurl=http://mirrors.cmecloud.cn/bclinux/product/bclinux_om/oe22.10/$basearch/os/
enabled=1
gpgcheck=0
EOF
        )
        echo "$repo_content" > /etc/yum.repos.d/sysak.repo
        yum clean all && yum makecache
        sleep 1
    elif grep -q "BigCloud Enterprise Linux For Euler 21.10 LTS" /etc/os-release; then 
	    echo "os info: oe21.10"
        repo_content=$(cat <<EOF
[sysak-oe21.10]
name=BC-Linux-release - om
baseurl=http://mirrors.cmecloud.cn/bclinux/product/bclinux_om/oe21.10/$basearch/os/
enabled=1
gpgcheck=0
EOF
        )
        echo "$repo_content" > /etc/yum.repos.d/sysak.repo
        yum clean all && yum makecache
        sleep 1
    elif grep -q "BigCloud Enterprise Linux 8" /etc/os-release; then
	    echo "os info: bc-8.x"
        repo_content=$(cat <<EOF
[sysak-bc8]
name=BC-Linux-release - om
baseurl=http://mirrors.cmecloud.cn/bclinux/product/bclinux_om/el8/$basearch/os/
enabled=1
gpgcheck=0
EOF
        )
        echo "$repo_content" > /etc/yum.repos.d/sysak.repo
        yum clean all && yum makecache
        sleep 1
    elif grep -q "BigCloud Enterprise Linux 7" /etc/os-release; then
	    echo "os info: bc-7.x"
        repo_content=$(cat <<EOF
[sysak-bc7]
name=BC-Linux-release - om
baseurl=http://mirrors.cmecloud.cn/bclinux/product/bclinux_om/el7/$basearch/os/
enabled=1
gpgcheck=0
EOF
        )
        echo "$repo_content" > /etc/yum.repos.d/sysak.repo
        yum clean all && yum makecache
        sleep 1
    fi

    if [ ! -f /usr/bin/sysak ]; then
	    echo "installed sysak、perf、perl、python."
        #ln -s /usr/bin/python3 /usr/bin/python
        # 目标文件路径
        target="/usr/bin/python3"
        # 链接文件路径
        link="/usr/bin/python"

        # 检查链接文件是否存在
        if [ ! -L "$link" ] || [ ! -e "$link" ]; then
            # 创建符号链接
            ln -s "$target" "$link"
        fi
        yum install -y perf* 
        yum install -y perl*  || yum install -y perl* --nobest --allowerasing
        yum install -y ssar
        #yum install -y sysak    #3.2.0-1
        #yum install -y sysak-2.1.0-6.oe1.bclinux.x86_64
        #yum install -y sysak-2.2.0-1.oe1.bclinux.x86_64
        yum install -y sysak || yum install -y sysak-2.2.0-1.oe1.bclinux.x86_64
    fi
}

main

