#!/bin/bash

SCORE=0
DAY_NUM=1

function print_PASS() {
  echo -ne ' \033[1;32mPASS\033[0;39m '
}
function print_FAIL() {
  echo -ne ' \033[1;31mFAIL\033[0;39m '
}
function LINE {
  STTY_SIZE=$(stty size)
  STTY_COLUMNS=$(echo $STTY_SIZE | cut -f2 -d" ")
  yes = 2>/dev/null | sed $STTY_COLUMNS'q' | tr -d '\n'
  printf "\n"
}

function LCOURSE() {
  echo -n " The results of your Day${DAY_NUM}: "
  if [ ${SCORE} -ge 70 ]; then
    print_PASS ; echo -e " Your score: \033[1;32m${SCORE}\033[0;39m"
  else
    print_FAIL ; echo -e " Your score: \033[1;31m${SCORE}\033[0;39m"
  fi
}

function MQ1 {
  if [ "$MQ" = "1" ]; then MQ=1; else MQ=0; fi
}

#### GRADE ####
function g1 {
  MQ=1
	if diff /etc/fstab /root/fstab &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task1. 拷贝文件 /etc/fstab 到 /root 下, 文件名不变'
	fi
}

function g2 {
  MQ=1
	if id -gn tom 2>/dev/null | grep -wq mygroup; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task2. 创建用户 tom, 属于 mygroup'
	fi
}

function g3 {
  MQ=1
	if grep "A firewall daemon with D-Bus interface providing a dynamic firewall" /root/firewall.summary &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task3. 使用 rpm 命令查询软件包 firewalld 的作用'
	fi
}

function g4 {
  MQ=1

	if grep "$(date +%Y%m%d)" /root/date.txt &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task4. 当前系统时间以 20230731 的形式,保存在文件 /root/date.txt 文件中'
	fi
}

function g5 {
  MQ=1

	if grep "hello world" /root/new.file &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task5. 创建文件 /root/new.file,内容为 hello world'
	fi
}

function g6 {
  MQ=1

	if getfacl /etc/fstab 2>/dev/null | grep -wq group::rw- \
    && getfacl /etc/fstab 2>/dev/null | grep -wq other::---; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task6. /etc/fstab 文件的权限,改为组只有读写权限,其他人没有任何权限'
	fi
}

function g7 {
  MQ=1

	if ls -lh /pagefile.sys 2>/dev/null | awk '{print $5}' 2>/dev/null | grep -wq 10M; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task7. 生成一个10MB的文件，位置 /pagefile.sys'
	fi
}

function g8 {
  MQ=1

	if getfacl /pagefile.sys 2>/dev/null | grep -wq 'owner: tom' \
    && getfacl /pagefile.sys 2>/dev/null | grep -wq 'group: ftp'; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task8. /pagefile.sys 文件的属主为 tom,属组为 ftp'
	fi
}

function g9 {
  MQ=1

	if grep -q https://mirror.nju.edu.cn/centos/7/os/x86_64 /etc/yum.repos.d/*.repo \
    && ls /etc/yum.repos.d/*.repo 2>/dev/null -l | wc -l | grep -wq 1 &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task9. '
	fi
}

function g10 {
  MQ=1

	if rpm -q vim-enhanced &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task10. vim-enhanced'
	fi
}

# Main area
echo
echo -e " Spend Time: \e[1;32m$(uptime -p | sed -e 's/ hours, /h/' -e 's/ minutes/m/')\e[0;39m, $(date '+%Y-%m-%d %a %H:%M')"
LINE

g1
g2
g3
g4
g5
g6
g7
g8
g9
g10

LINE
LCOURSE
echo
