#!/bin/bash

# 主要做的:
# - 初始化一个ostree仓库:testos-repo
# - 在osdata目录创建模拟一些系统文件 and 提交两次到testos-repo 仓库
# - 复制osdata数据到osdata-devel,做出修改然后再次提交
# - 用init-fs创建sysroot 和 os-init 和 syslinux
# - 提交一个新的commit用空目录，标记初始分支（testos/buildmaster/x86_64-runtime）为EOL 重新定向到新分支（testos/buildmaster/newbranch）

set -euo pipefail

. ./libtest.sh

moniter_signal_success () {
    signal=$1
    
    while IFS= read -r line <&3; do
        if echo -e "$line" | grep -q $signal" success = True"; then
            echo -e "$line \n"
            break
        fi

        if echo "$line" | grep -q 'ERROR'; then
            echo -e "\033[91m$line\033[0m"
        else
            echo "$line"
        fi
    done
}

test_deploy_limit() {
    for ((i=1; i<=$1; i++))
    do
        y=$((i - 1))

        if [ $y -eq 0 ]; then
            y=""
        fi
        os_repository_new_commit 1 1 testos/buildmaster/newbranch$i

        mkdir empty$i
        echo -n "Refs:testos/buildmaster/x86_64-runtime and new commit revision:"
        ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo commit --tree=dir=$(pwd)/empty$i --add-metadata-string "ostree.endoflife-rebase=testos/buildmaster/newbranch$i" -b testos/buildmaster/newbranch$y -s "EOL redirect to new branch"

        echo ""
        echo "Ostree test environment is deployed, Start System update Fuction tested"
        echo ""

        # 检查更新
        sudo gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
        moniter_signal_success $update_finished_signal

        # 下载过程
        sudo gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$download_method > /dev/null
        moniter_signal_success $download_finished_signal

        # 部署 
        sudo gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$deploy_method > /dev/null
        moniter_signal_success $deploy_finished_signal
    done
}

# 创建仓库 仓库类型 and boot类型
setup_os_repository "archive" "syslinux"

cd ${test_tmpdir}
${CMD_PREFIX} ostree --repo=sysroot/ostree/repo remote add --set=gpg-verify=false testos $(cat httpd-address)/ostree/testos-repo

${CMD_PREFIX} ostree --repo=sysroot/ostree/repo pull testos testos/buildmaster/x86_64-runtime > /dev/null
rev=$(${CMD_PREFIX} ostree --repo=sysroot/ostree/repo rev-parse testos/buildmaster/x86_64-runtime)
echo "Pull a branch(testos/buildmaster/x86_64-runtime) from remote repo and revision=${rev}"

${CMD_PREFIX} ostree admin deploy --karg=root=LABEL=MOO --karg=quiet --os=testos testos:testos/buildmaster/x86_64-runtime > /dev/null
assert_has_dir sysroot/boot/ostree/testos-${bootcsum}

echo "Frist deploy refs:testos/buildmaster/x86_64-runtime to success"

exec 3< <(/home/x/share/ostree/kylin-system-updater/backend/kylin-system-updater -r -d --sysroot=$test_tmpdir/sysroot --os=testos 2>&1)
# 等待dbus服务启动
sleep 3

test_deploy_limit 1

# 部署完成后的检查
# assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf "${bootcsum}"
# rev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse testos/buildmaster/newbranch)
# assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0/usr/bin/content-iteration "1"

# assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0.origin "newbranch"

echo "ok update and redirect"