#!/bin/bash
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}')
# echo  $login_user_id
# echo  $login_user_name
# echo  $login_user_home

MYDIR=$(dirname $(readlink -f "$0"))
echo "dir: ${MYDIR}"  >> /tmp/debug.log

echo "${MYDIR}/times.left" >> /tmp/debug.log
source "${MYDIR}/times.left"
source "${MYDIR}/action.cfg"
logFile=${MYDIR}/${LOG}
ARCH=$(arch)
# resultFile=$(dirname "${MYDIR}")/result/result.txt
console=/dev/ttyS0

if [ $ARCH = "aarch64" ]; then
    console=/dev/ttyAMA0
fi
s=$(printf "%-30s" "=")
echo -e "${s// /=}\n" >> ${logFile}
now=`date '+%s'`
if [ $(($now - $TIMECTRL)) -ge 300 ]; then
    echo "[`date`] 警告：距离上次自动执行超5分钟"  | tee -a ${logFile}
    # echo -e "检测到超时（5分钟）异常，测试终止！\n $now - $TIMECTRL -ge 300" >> /tmp/debug.log
    # rm -f $login_user_home/.config/autostart/reboot.desktop
    # chown $login_user_name:$login_user_name $MYDIR -R
    # exit 1
fi
# export PATH=$PATH:$MYDIR

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

loongson_coldboot()
{
    ec_wakeup
}


phytium_coldboot2()
{
    #ibf write 66 bit 1 = 0  read obf 66 bit 0 = 0  
    echo lc111111 | sudo -S ${MYDIR}/mem write 1 0x20000066 0x81 > /dev/null
    sleep 0.1
    echo lc111111 | sudo -S ${MYDIR}/mem write 1 0x20000062 0xd > /dev/null
    sleep 0.1
    echo lc111111 | sudo -S ${MYDIR}/mem write 1 0x20000062 0x1 > /dev/null
    sleep 0.5
    echo lc111111 | sudo -S ${MYDIR}/mem write 1 0x20000066 0x81 > /dev/null
    sleep 0.1
    echo lc111111 | sudo -S ${MYDIR}/mem write 1 0x20000062 0xd > /dev/null
    sleep 0.1
    echo lc111111 | sudo -S ${MYDIR}/mem write 1 0x20000062 0x1 > /dev/null
    sleep 0.5
    echo lc111111 | sudo -S ${MYDIR}/mem write 1 0x20000066 0x81 > /dev/null
    sleep 0.1
    echo lc111111 | sudo -S ${MYDIR}/mem write 1 0x20000062 0xd > /dev/null
    sleep 0.1
    echo lc111111 | sudo -S ${MYDIR}/mem write 1 0x20000062 0x1 > /dev/null
    sleep 0.5
}

phytium_coldboot()
{
    echo "write EC[0x08] = 0x1E"  >> ${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[0x0D]:"  >> ${logFile}
    echo lc111111 | sudo -S ${MYDIR}/ec$ARCH -r -a 0x08  >> ${logFile}
}

case "$ARCH" in
    mips64)
        coldboot_action=loongson_coldboot
        ;;
    aarch64)
        coldboot_action=phytium_coldboot
        ;;
    *)
        coldboot_action=ec_wakeup
        # echo "未支持$ARCH架构，测试未进行！"
        # exit 1
    ;;
esac

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}'
}

OS=`info ID /etc/os-release`


if [ ${timesLeft} -gt '0' ];then
    echo "[`date "+%m-%d-%H:%M:%S"`] Do ${ACTION} remaining ${timesLeft} times" > $console
    echo "[`date`]还剩余${timesLeft}次${ACTION}。" | tee -a ${logFile}
    ############################################################
    if [ "$OS" == "uos" ];then
        if read -t 15 -p "按Enter键停止测试!" start; then
            rm -f $login_user_home/.config/autostart/reboot.desktop
            chown $login_user_name:$login_user_name $MYDIR -R
            echo "测试中止！"
            exit 1
        fi
    fi
    echo "timesLeft=$((timesLeft-1))" > ${MYDIR}/times.left  
    now=`date '+%s'`
    sed -i "s#^TIMECTRL=.*#TIMECTRL=$now#" ${MYDIR}/action.cfg
    chown $login_user_name:$login_user_name $MYDIR -R
    case "${ACTION}" in
    warnboot)
        /sbin/reboot
        ;;
    coldboot)
        $coldboot_action
        /sbin/poweroff
        ;;
    esac
    echo "[`date "+%m-%d-%H:%M:%S"`] Do ${ACTION} finish!" > $console
else
    echo "[`date "+%m-%d-%H:%M:%S"`] Do ${ACTION} all done!" > $console
    echo "[`date`] ${ACTION}测试完成" | tee -a ${logFile}
    rm -f $login_user_home/.config/autostart/reboot.desktop
    echo lc111111 | sudo -S sed -i '/.\/reboot.sh/d' /etc/crontab
    chown $login_user_name:$login_user_name $MYDIR -R
    read i
fi
