#!/bin/bash

SCORE=0
DAY_NUM=3

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/host.stat 2>/dev/null | grep -wq HOSTNAME:.*$(hostname) \
    && /root/host.stat 2>/dev/null | grep -wq MAC:.*$(ip a | awk '/ether/ {print $2}' | tail -n 1 ) \
    && /root/host.stat 2>/dev/null | grep -wq IP:.*$(ip a | awk '/global/ {print $2}' | grep -v 10.0.2.15) \
    && /root/host.stat 2>/dev/null | grep -wq GATEWAY:.*$(ip route | awk '/default/ {print $3}') \
    && /root/host.stat 2>/dev/null | grep -wq DNS:.*$(awk '/nameserver/ {print $2}' /etc/resolv.conf | head -n 1);then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task1. 编写脚本 /root/host.stat'
	fi
}

function g2 {
  MQ=1
  RH=$(df | awk '$6 == "/" {print $1}')
  RT=$(df | awk '$6 == "/" {print $6}')
  SH=$(swapon | awk '/partition/ {print $1}')
  ST=$(swapon | awk '/partition/ {print $4}')
	if /usr/local/bin/disk.stat 2>/dev/null | grep -wq ${RH}.*${RT} \
    && /usr/local/bin/disk.stat 2>/dev/null | grep -wq ${SH}.*${ST}; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task2. 编写脚本 /usr/local/bin/disk.stat'
	fi
}

function g3 {
  MQ=1
  /usr/local/bin/paixu.sh > /tmp/3 2>/dev/null
  ls -l /usr/bin | sort -nrk5 | head | awk '{print $NF}' > /tmp/three
	if diff /tmp/3 /tmp/three &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task3. 编写脚本 /usr/local/bin/paixu.sh'
	fi
}

function g4 {
  MQ=1
  /usr/local/sbin/effect.sh /etc/selinux/config 2>/dev/null >/tmp/a
  egrep -v '^$|#' /etc/selinux/config > /tmp/b
	if /usr/local/sbin/effect.sh 2>/dev/null | grep -q Usage:.*effect.sh.*/path/config \
    && diff /tmp/a /tmp/b &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task4. 编写脚本 /usr/local/sbin/effect.sh'
	fi
}

function g5 {
  MQ=1
  /usr/local/bin/ip.sh > /tmp/i 2>/dev/null
  IPv4="$(hostname -I | awk '{print $2}')"
  IPv6=$(ip a | awk '/inet6/ {print $2}' | awk -F/ '/fe80/ {print $1}')
  if [ "$IPv4" = "$(/usr/local/bin/ip.sh 2>/dev/null | head -n 1)" ] \
    && echo $IPv6 | grep -wq "$(/usr/local/bin/ip.sh 2>/dev/null | tail -n 1)" &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
    SCORE=$(expr $SCORE + 10 )
    print_PASS; 
    echo 'Task5. 编写脚本 /usr/local/bin/ip.sh'
  fi
}

function g6 {
  MQ=1
  userdel -r spike &>/dev/null
  /usr/sbin/account.mgr &>/dev/null
	if sshpass -p xiaogou \
    ssh -o StrictHostKeyChecking=no spike@localhost \
      grep /home/dog /etc/passwd &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task6. 编写脚本 /usr/sbin/account.mgr'
	fi
}

function g7 {
  MQ=1
  userdel -r fred &>/dev/null
  /usr/local/bin/account.bat fred &>/dev/null
	if sshpass -p fred123 \
    ssh -o StrictHostKeyChecking=no fred@localhost \
      id &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task7. /usr/local/bin/account.bat'
	fi
}

function g8 {
  MQ=1
  /usr/local/bin/change.age fred 14 90 &>/dev/null
	if chage -l fred 2>/dev/null | grep Minimum | grep -wq 14 \
    && chage -l fred 2>/dev/null | grep Maximum | grep -wq 90; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task8. 编写脚本 /usr/local/bin/change.age'
	fi
}

function g9 {
  MQ=1
  yum -y remove nginx httpd &>/dev/null
  /root/bin/web.build &>/dev/null
	if curl localhost:8080 2>/dev/null | grep -wq 'web.build' \
    && grep -wq enable /root/bin/web.build 2>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task9. 编写脚本 /root/bin/apache.build'
	fi
}

function g10 {
  MQ=1
  source /usr/bin/prompt.sh &>/dev/null
	if env | grep -wq DAY=3 \
    && echo $PS1 | grep -wq '1;32m'; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task10. 编写脚本 /usr/bin/prompt.sh'
	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
