#!/bin/bash

examdir=/root/rhcsa9-exam200
examzip=/root/rhcsa9-exam200.zip
scripts=${examdir}/scripts
pre_cla_script=prepare_classroom.sh
pre_sa_script=prepare_servera.sh
pre_sb_script=prepare_serverb.sh

# 解压 exam 材料
[[ -f ${examzip} ]] && unzip -o ${examzip} &> /dev/null || echo ${examzip} is not exist

# 配置 exam.lib
rsync ${examdir}/exam.lib /usr/local/lib/exam.lib
source /usr/local/lib/exam.lib

# 判断执行用户
if [[ `id -u` -ne 0 ]];then
    echo "Please run as root"
    exit 1
fi

# # 配置免密登录，测试 utility是否启动需要
# # 配置 kiosk 免密登录
# rsync -a ${examdir}/.ssh/* ~kiosk/.ssh/
# chmod 600 ~kiosk/.ssh/*
# chown -R kiosk:kiosk ~kiosk/.ssh
# # 配置 root 免密登录
# rsync -a ${examdir}/.ssh/* ~/.ssh
# chmod 600 ~/.ssh/*


# 配置 classroom 解析
if ! grep -q classroom /etc/hosts ;then
  echo "172.25.254.254 classroom" >> /etc/hosts
else
  sed -ri 's/^.*classroom.*$/172.25.254.254 classroom/' /etc/hosts
fi

# 重置虚拟机
pad "Reset all VMs"
rht-vmctl -q reset classroom  &>/dev/null
rht-vmctl -q reset all  &>/dev/null
print_DONE

# 等待虚拟机启动
echo "Wait for VMs running:"
sleep 30 
for host in classroom bastion workstation utility servera serverb
do
  pad "  Wait for $host running"
  wait_ssh $host &>/dev/null
  if [ "$host" == "utility" ] ;then
    while :
    do 
      # 判断 registry 是否启动完成
      if ssh utility podman search registry.lab.example.com/ubi &>/dev/null;then
        print_DONE
        break
      fi
      # 3 秒后再次判断
      sleep 3
    done
  else
    print_DONE
  fi
done

############# 配置 foundation0 #############
pad "Prepare foundation"

# 准备 check_password 程序
for host in servera serverb
do
  rsync ${scripts}/check_password root@$host:/usr/local/bin/check_password &>/dev/null
  ssh root@$host chmod a+x /usr/local/bin/check_password
done

# 关闭免密登录
# 将.ssh 配置文件放到 ~kiosk/.ssh.bak
[ -d ~kiosk/.ssh.bak ] && rm -fr ~kiosk/.ssh.bak
cp -a ${examdir}/.ssh ~kiosk/.ssh.bak
rm -fr ~kiosk/.ssh/*

# 准备容器文件
materials=/content/courses/rh124/rhel9.0/materials/
rsync -a ${examdir}/laoma ${materials}

# 准备 exam 试题
rsync -a ${examdir}/EX200-RHCSA9-202312.html ${materials}
mv ${materials}/EX200-RHCSA9-202312.html ${materials}/index.html

# 桌面图标
rsync ${examdir}/redhat_logo.png /usr/share/icons/redhat_logo.png
rsync ${examdir}/exam.desktop /home/kiosk/.local/share/applications/
chown kiosk:kiosk /home/kiosk/.local/share/applications/exam.desktop

# 设置评分脚本
rsync ${scripts}/grade /usr/local/bin
chmod a+x /usr/local/bin/grade

print_DONE

############# 配置 foundation0 #############


############# 配置 classroom #############
pad "Prepare classroom"
rsync ${scripts}/${pre_cla_script} root@classroom: &>/dev/null
ssh root@classroom chmod +x /root/$pre_cla_script
ssh root@classroom /root/$pre_cla_script &> /dev/null
ssh root@classroom rm -f /root/$pre_cla_script
print_DONE
############# 配置 classroom #############


############# 配置 servera #############
pad "Prepare servera"
rsync ${scripts}/${pre_sa_script} root@servera:
ssh root@servera chmod +x /root/$pre_sa_script
(ssh root@servera "/root/$pre_sa_script;rm -f /root/$pre_sa_script"  &> /dev/null ) & 
print_DONE
############# 配置 servera #############


############# 配置 serverb #############
pad "Prepare serverb"
rsync ${scripts}/${pre_sb_script} root@serverb:
ssh root@serverb chmod +x /root/${pre_sb_script}
ssh root@serverb "/root/${pre_sb_script};rm -f /root/${pre_sb_script}" &> /dev/null
print_DONE
############# 配置 serverb #############


# 开始做题
read -p '
-----------------------------------------------------------------------
          按 Super 键，搜索 EXAM，打开试题，按回车键开始答题
-----------------------------------------------------------------------'
