#!/bin/bash

SCORE=0
DAY_NUM=4

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 /root/kiss.sh 2>/dev/null | grep -wq haha \
    && /root/kiss.sh tom 2>/dev/null | grep -wq cat \
    && /root/kiss.sh jerry 2>/dev/null | grep -wq mouse; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task1. 编写脚本 /root/kiss.sh'
	fi
}

function g2 {
  MQ=1
  for i in {1..50}; do
    userdel -r user$if
  done &>/dev/null
  /root/yali.ceshi 2>/dev/null
	if id -u user9 2>/dev/null | grep -wq 2009 \
    && id -u user21 2>/dev/null | grep -wq 2021 \
    && id -u user41 2>/dev/null | grep -wq 2041; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task2. 编写脚本 /root/yali.ceshi'
	fi
}

function g3 {
  MQ=1
  source /root/func.lib &>/dev/null
	if jiafa 7 8 2>/dev/null | grep -q '7.*+.*8.*=.*15'; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task3. 编写库文件 /root/func.lib'
	fi
}

function g4 {
  MQ=1
	if [ -x /root/func.lib ]; then
    /root/ruguo.sh &>/dev/null \
    && [ ! -x /root/func.lib ] \
    && MQ1
  elif
    /root/ruguo.sh &>/dev/null \
    && [ -x /root/func.lib ]; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task4. 编写脚本 /root/ruguo.sh'
	fi
}

function g5 {
  MQ=1
  CN="$(nmcli -g NAME connection show | tail -n 1)"
  CM=$(nmcli -g ipv4.method con show "$CN")
  CD=$(nmcli -g ipv4.dns con show "$CN")
	if [ "$CM" = "manual" ] \
    && echo $CD 2>/dev/null | grep -wq 114.114.114.114; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task5. 当前的网络地址配置成静态地址'
	fi
}

function g6 {
  MQ=1

	if /root/listen.sh 2>/dev/null | grep -q 'tcp.*LISTEN.*:' \
    && /root/listen.sh 2>/dev/null | grep -q 'default.*via.*dev'; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task6. 编写脚本 /root/listen.sh'
	fi
}

function g7 {
  MQ=1
  /root/users.sh 2>/dev/null > /tmp/u
  awk -F: '$3 >= 1000 {print $1}' /etc/passwd > /tmp/U
	if diff /tmp/u /tmp/U &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task7. 编写脚本 /root/users.sh'
	fi
}

function g8 {
  MQ=1

	if rpm -q samba-client &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task8. smbclient 命令默认不存在,想办法安装在本系统'
	fi
}

function g9 {
  MQ=1
  LF=$(lsattr /root/users.sh 2>/dev/null)
	if [ "${LF:4:1}" = "i" ]; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task9. 不能直接删除的文件,想办法删除'
	fi
}

function g10 {
  MQ=1
  head -n 3 /var/log/messages > /tmp/d
	if diff /root/dmesg.time /tmp/d &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task10. 查找 /var/log/dmesg 文件中,耗时最长的3条记录'
	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
