#!/bin/bash
MYDIR=$(dirname $(readlink -f "$0"))
echo "dir: ${MYDIR}"  >> /tmp/debug.log
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}')
source "${MYDIR}/times.left"
source "${MYDIR}/action.cfg"
logFile=${MYDIR}/${LOG}
count=${timesLeft}
echo "[`date`] 开始待机${ACTION}测试。。。" | tee -a ${logFile}
# echo "[`date`] 开始待机${ACTION}测试。。。" >> result.txt
echo "日志文件: ${logFile}"
echo "循环次数： ${count}"  | tee -a ${logFile}
ARCH=$(arch)
#ARCH="loongarch64"
console=/dev/ttyS0

if [ $ARCH = "aarch64" ]; then
    console=/dev/ttyAMA0
fi

ec_wakeup()
{
    echo "write EC[0x0F] = 0x28"  >> ${logFile}
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -w 0x28 -a 0x0F
    sleep 0.5
    echo -ne "read EC[0x0F]:"  >> ${logFile}
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -r -a 0x0F  >> ${logFile}
}


loongson_suspend()
{
    if [ "$ARCH" == "mips64" ]; then
        ${MYDIR}/rtc_resume ${TIMER}s
    else
        # echo "set_rtc rtc_resume_la ${TIMER}s"
        ec_wakeup
        sleep 1
    fi
    # hwclock -w
	# freeze mem disk
    echo mem > /sys/power/state
}

loongson_hibernate()
{
    ec_wakeup
    echo disk > /sys/power/state
}

uos_suspend()
{
    rtcwake -m mem -s 60
    # hwclock -w
    #echo mem > /sys/power/state
}


uos_hibernate()
{
    #ec_wakeup
    #echo disk > /sys/power/state
    rtcwake -m disk -s 60
}

haiguagn_suspend()
{
    ec_wakeup
    systemctl suspend
}

haiguang_hibernate()
{
    ec_wakeup
    systemctl hibernate
}

phytium_suspend()
{
    ec_wakeup

    echo "write EC[0x0C] = 0x0F"  >> ${logFile}  #aarch64
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -w 0x0F -a 0x08
    #echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -w 0x01 -a 0x0A
    sleep 0.5
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -w 0x0F -a 0x08
    #echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -w 0x01 -a 0x0A
    sleep 0.5
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -w 0x0F -a 0x08
    #echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -w 0x01 -a 0x0A
    sleep 0.5
    echo -ne "read EC[0x08]:"  >> ${logFile}
    #echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -r -a 0x0C  >> ${logFile}
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -r -a 0x08  >> ${logFile}
    systemctl suspend
    #rtcwake -m mem -s 5
    #sleep 60
    ##ec_wakeup
}

phytium_hibernate()
{
    ec_wakeup
    echo "write EC[0x1E] = 0x08"  >> ${logFile}
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -w 0x1E -a 0x08
    sleep 0.5
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -w 0x1E -a 0x08
    sleep 0.5
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -w 0x1E -a 0x08
    sleep 0.5
    echo -ne "read EC[0x08]:"  >> ${logFile}
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -r -a 0x08  >> ${logFile}
    systemctl hibernate
    #sleep 60
    #ec_wakeup
}

case "$ARCH" in
    mips64)
        suspend_action=loongson_suspend
        hibernate_action=loongson_hibernate
        ;;
    loongarch64)
        suspend_action=loongson_suspend
        hibernate_action=loongson_hibernate
        ;;
    aarch64)
        suspend_action=phytium_suspend
        hibernate_action=phytium_hibernate
        ;;
    x86_64)
        #suspend_action=haiguagn_suspend
        #hibernate_action=haiguang_hibernate
		suspend_action=uos_suspend
		hibernate_action=uos_hibernate
        ;;
    *)
        echo "未支持$ARCH架构，测试未进行！"
        exit 1
        ;;
esac


while [ $count -gt 0 ]
do
    echo "[`date "+%m-%d-%H:%M:%S"`] Do ${ACTION} remaining ${count} times" > $console
    echo "还剩余${count}次${ACTION}测试"
    echo "[`date`] 还剩余${count}次${ACTION}测试" >> ${logFile}
    if read -t 30 -p "按Enter键停止测试!" start; then
        chown $login_user_name:$login_user_name $MYDIR -R
        echo "测试中止！"
        # ifconfig enp1s0 up
        # rfkill unblock 0
        exit 1
    fi
    s=$(printf "%-30s" "=")
    echo -e "${s// /=}\n" >> ${logFile}
    echo -e "${s// /=}\n" >> ${logFile}
    count=$(($count-1))
    case "${ACTION}" in
    suspend)
        $suspend_action
        ;;
    hibernate)
        $hibernate_action
        ;;
    esac
    echo "[`date "+%m-%d-%H:%M:%S"`] Do ${ACTION} finish!" > $console
    clear
done
echo "[`date "+%m-%d-%H:%M:%S"`] Do ${ACTION} all done!" > $console
echo "[`date`] ${ACTION}测试完成" | tee -a ${logFile}
# echo "[`date`] ${count}次${ACTION}测试完成" >> result.txt
chown $login_user_name:$login_user_name $MYDIR -R
read input
exit 0
