#!/bin/bash
MYDIR=$(dirname $(readlink -f "$0"))
if [ $UID -ne 0 ]; then
    echo "请以root用户身份运行!"
    echo "提示： sudo -s 或 su root 可以切换到root用户"
    exit 1
fi

ARCH=$(arch)

usage()
{
    echo "输入错误！"
    echo "支持：睡眠（suspend）/休眠（hibernate）/重启（warnboot）/冷重启（coldboot）测试。"
    echo "用法：./RunTest [suspend/hibernate/warnboot/coldboot] [次数]"
    echo "例如： 循环300次重启测试： ./RunTest.sh warnboot 300"
    exit 1
}

if [ $# -lt 2 ]; then
    usage
fi


info(){
		# 获取key所在的行
        info_line=`sed -n "/^$1=/p" $2`
        if [[ -z $info_line ]];then
                echo ""
        fi
		# 返回key对应的value值
        echo $info_line|awk -F '=' '{print $2}'
}

install_ec_tools()
{
    if [ ! -f "/proc/ec_control/resume_timer" ];then
        cd EcTools
        insmod ./ec_operation.ko >> /dev/null 2>&1
        if [ ! -f "/proc/ec_control/resume_timer" ];then
            echo "install ec tools ..."
            make >> $login_user_home/debug.log 2>&1
            insmod ./ec_operation.ko >> $login_user_home/debug.log
            if [ $? -ne 0 ];then
                echo "ERROR: install ec tools fail"
                read
                exit 1
            fi
        else
            echo "ec tools ready"
        fi
        cd $MYDIR
    fi
}

set_autorun_uos()
{
    #设置关闭自动更新
    echo 设置关闭自动更新
    dbus-send --system --dest=com.deepin.lastore --type=method_call /com/deepin/lastore com.deepin.lastore.Updater.SetAutoCheckUpdates boolean:false > /dev/null 2>&1
    #设置关闭更新提醒
    dbus-send --system --dest=com.deepin.lastore --type=method_call /com/deepin/lastore com.deepin.lastore.Updater.SetUpdateNotify boolean:false > /dev/null 2>&1
    # set autologin
    sed -i 's/^#\(autologin-user=\)$/\1'${login_user_name}'/' /etc/lightdm/lightdm.conf || echo "设置自动登陆失败，请手动设置自动登陆，已经设置请忽略"
    echo "更改系统设置。。。"
    su $login_user_name <<EOF
bash $MYDIR/set_power_plan.sh;
exit;
EOF
    cat > $login_user_home/.config/autostart/reboot.desktop <<EOF
[Desktop Entry]
Type=Application
Exec=$MYDIR/reboot.sh
Name=运行量产测试
StartupNotify=false
Terminal=true
EOF
}

set_autorun_kylin()
{
    sed -i "\$a\*\/${interval:=2} * * * * root cd \/${MYDIR} && .\/reboot.sh" /etc/crontab
    # sed -i "\$a\*\/${interval:=2} * * * * root cd \/${MYDIR} && DISPLAY=:0.0 mate-terminal -e .\/reboot.sh" /etc/crontab
}

login_user_id=$(grep '^\<UID_MIN\>' /etc/login.defs | awk '{print $2}')
login_user_name=$(cat /etc/passwd | awk -v id="$login_user_id" -F: '{if ($3==id) print $1}')
login_user_home=$(cat /etc/passwd | awk -v id="$login_user_id" -F: '{if ($3==id) print $6}')

case "$ARCH" in
    mips64)
        # ifconfig enp1s0 down > /dev/null 2>&1
        # # 关闭WiFi
        # rfkill block 0 > /dev/null 2>&1
        ;;
    
    loongarch64)
        # ifconfig enp1s0 down > /dev/null 2>&1
        # # 关闭WiFi
        # rfkill block 0 > /dev/null 2>&1
        ;;

    aarch64)
        echo ""
        # ifconfig enp3s0 down > /dev/null 2>&1
        # # 关闭WiFi
        # rfkill block 0 > /dev/null 2>&1
        ;;
    x86_64)
        chmod +x ec$ARCH
        ;;
    *)
        echo "未支持 $ARCH 架构，测试未进行！"
        exit 1
    ;;
esac

date >> $login_user_home/debug.log


chmod +x *.sh
chmod +x rtc_resume rtc_resume_la
# chmod +x mem
chmod +x ec*

if [ $2 -le 0 ]; then
    echo "输入有误! 循环次数必须大于0 请重新运行"
    usage
fi
LOG_FILE=$1`date +%Y%m%d`.log

echo "日志文件： ${LOG_FILE}"

TIMECTRL=`date '+%s'`
sed -i "s#^TIMECTRL=.*#TIMECTRL=$TIMECTRL#" ./action.cfg
sed -i "s#^LOG=.*#LOG=$LOG_FILE#" ./action.cfg
sed -i "s#^ACTION=.*#ACTION=$1#" ./action.cfg
echo "timesLeft=$2" > ./times.left

OS=`info ID /etc/os-release`
if [ "$OS" == "uos" ];then
    set_autorun=set_autorun_uos
else
    set_autorun=set_autorun_kylin
fi

echo "测试即将开始。。。"

source "${MYDIR}/times.left"
source "${MYDIR}/action.cfg"
interval=2
echo "TIMER=${TIMER} " >> $login_user_home/debug.log
echo "TIMECTRL=${TIMECTRL}" >> $login_user_home/debug.log
echo "LOG=${LOG}" >> $login_user_home/debug.log
chown $login_user_name:$login_user_name $login_user_home/debug.log
case "$1" in
suspend)
    ./S3S4.sh
    exit 0
    ;;
warnboot)
	$set_autorun
    chown $login_user_name:$login_user_name $MYDIR -R
    ./reboot.sh
    ;;
hibernate)
    chown $login_user_name:$login_user_name $MYDIR -R
    ./S3S4.sh
    ;;
coldboot)
    $set_autorun
    ./reboot.sh
    ;;
    *)
    usage
    ;;
esac

