#!/bin/bash

SCORE=0
DAY_NUM=2
vg=0007
NF=/tmp/n.conf
FD=/tmp/f
test -f $FD && rm -rf $FD
mkdir -p $FD
find / -user tom 2>/dev/null -exec cp {} $FD \;
egrep -v '^$|#' /etc/nginx/nginx.conf > $NF 2>/dev/null
SF=/tmp/s.txt
find /etc -size +1M -size -5M > $SF 2>/dev/null
SR=/tmp/px.txt
ls -l /usr/bin | sort -nrk5 | head | awk '{print $NF}' > $SR
CN=/tmp/cmd
ls /usr/sbin | wc -l > $CN
PF=/tmp/pf
cut -d: -f1,3,7 /etc/passwd > $PF
PS=/tmp/ppp
grep PS1 /etc/bashrc | grep -v \# > $PS

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 su - tom -c umask 2>/dev/null | grep -wq $vg; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task1. 只有 tom 用户,新创建的文件权限默认是"rw-rw----"'
	fi
}

function g2 {
  MQ=1

	if getfacl /share/sales 2>/dev/null | grep -q flags.*-s- \
    && getfacl /share/sales 2>/dev/null | grep -q group.*ftp; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task2. 任何用户在该目录中新创建的文件,自动更改为 ftp 组所属'
	fi
}

function g3 {
  MQ=1

	if curl localhost &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task3. 保证文件权限正确,该站点可以访问'
	fi
}

function g4 {
  MQ=1

	if diff $NF /mynginx.conf &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task4. nginx服务的主配置文件中,空行和注释行删除'
	fi
}

function g5 {
  MQ=1
	if diff -r $FD /home/backup &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task5. 查找属于 tom 用户的文件'
	fi
}

function g6 {
  MQ=1
	if diff $SF /size.file &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task6. 查找 /etc 目录下,大于 1M 小于 5M 的文件名'
	fi
}

function g7 {
  MQ=1
	if diff $SR /paixu.txt &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task7. 查找 /usr/bin 目录下最大的10个文件'
	fi
}

function g8 {
  MQ=1
	if diff $CN /command.txt 2>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task8. 计算 /usr/sbin 目录下有多少个命令'
	fi
}

function g9 {
  MQ=1
	if diff /pass.txt $PF &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task9. 只保留 /etc/passwd 文件的第1列第3列和最后一列'
	fi
}

function g10 {
  MQ=1
	if diff /ps1.txt $PS &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task10. /etc/profile ,只保留包含 PS1 的行,不要注释行'
	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
