#!/bin/bash
#
# Copyright 2014 Red Hat, Inc.
#
# NAME
#     lab-grep - setup script for RH134 grep lab
#
# SYNOPSIS
#     lab-grep setup
#
#     This setup function works on serverX.example.com.
#
# DESCRIPTION
#     This script performs the setup for the RH134 grep lab.
#
# CHANGELOG
#   * Wed Jul 2 2014 Scott McBrien <smcbrien@redhat.com>
#   - Added a systemctl restart rsyslog to fix an issue with
#     machines with a non US Eastern timezone setting.

#   * Mon Apr 21 2014 Unknown <unknown@redhat.com>
#   - original code

# Initialize and set some variables
MYHOST=""
CMD=""
DEBUG='true'
RUN_AS_ROOT='true'

# Source library of functions
source /usr/local/bin/labtool.shlib
trap on_exit EXIT

# Additional functions for this shell script
function print_usage {
  local problem_name=$(basename $0 | sed -e 's/^lab-//')
  cat << EOF
This script controls the setup and grading of this lab.
Usage: lab ${problem_name} COMMAND
       lab ${problem_name} -h|--help
COMMAND is one of:
    setup - perform any setup steps for this lab on this machine
    grade - perform any grading steps and report on this machine
EOF
}

function lab_setup {
    VMNAME=$1
    case $VMNAME in
        desktop)
            # Put in the steps to set up the lab
            # Change to the example of server) if should only run
            # there - and adjust the catchall message with *)
            print_FAIL && echo "The setup script needs to be run on server"
            ;;
        server)
            # HORRIBLE HACK
            # To get rsyslog to pick up the correct timezone (applied late
            # in the boot process), it must first be restarted.  This should
            # be fixed when general locale related fixes can be applied to the
            # VM images...

            systemctl restart rsyslog

            # Need to add an entry to the log outside of the "start" window
            # first.
            logger -t database -i "bad entry" "ERROR: $(md5sum /etc/passwd | cut -d' ' -f1) invalid"
            
            echo "Seeding log files (this can take a couple of minutes)..."
            # this sleep makes sure that the above error log is outside of the
            # start window (minutes)
            sleep 60

            # If the time is on the ninth minute, wait a minute (so that the
            # regexs used by students can be a little easier.
            if [ $(date +%M | cut -c2) -eq 9 ]
            then
              sleep 60
            fi

            echo
            echo -n "Start time: $(date)"
            echo
            seed_logs
            # Obviously adjust this to actual commands if script needs
            # to run on server - and adjust the catchall message with *)
            echo "Complete, you may now continue with the lab"
            ;;
        *)
            # Should never get here, but what the hey....
            print_FAIL && echo "The setup script needs to be run on desktop"
            ;;
    esac

}

function lab_grade {
    VMNAME=$1
    case $VMNAME in
        desktop)
            echo "Steps to grade lab on desktop"
            # Put in the steps to grade the lab (use print_PASS or print_FAIL)
            # Change to the example of server) if should only run
            # there - and adjust the catchall message with *)
            ;;
        server)
            # Obviously adjust this to actual commands if script needs
            # to run on server - and adjust the catchall message with *)
            print_FAIL && echo "The grade script needs to be run on desktop"
            ;;
        *)
            # Should never get here, but what the hey....
            print_FAIL && echo "The grade script needs to be run on desktop"
            ;;
    esac
}

function seed_logs
{  
  # This functions is for the writing of different log data into 
  # /var/log/messages so that students have something known to work with.

  # first lets make some random data to use in our log content
  find /usr/lib64 -type f -exec md5sum {} \; | cut -f1 -d" " > /tmp/md5sums


  # Let's put in some normal messages first
  for sum in $(head /tmp/md5sums)
  do
    logger -t ima_daemon -i logging "ACCESS: $sum"
  done
  for sum in $(tail -n +20 /tmp/md5sums | head -n 13)
  do
    logger -t database -i listener "NORMAL: $sum"
  done
  sleep 5
  
  # now some ERROR messages
  for sum in $(tail -n +30 /tmp/md5sums | head -n 17)
  do
    logger -t database -i "bad entry" "ERROR: $sum invalid"
  done
  sleep 5

  # Lets throw a couple in that are not case sensative
  for sum in $(tail -n +40 /tmp/md5sums | head -n 3)
  do
    logger -t database -i "invalid access" "error: $sum invalid"
  done
  sleep 5

  # now some more ERROR messages
  for sum in $(tail -n +50 /tmp/md5sums | head)
  do
    logger -t database -i "bad entry" "ERROR: $sum invalid"
  done
  sleep 5 

  # more ERROR messages but now some from the 'daemon'
  for sum in $(tail -n +60 /tmp/md5sums | head -n 7)
  do
    logger -t ima_daemon -i "connection refused" "ERROR: client identity $sum "
  done
  sleep 5 

  # now more NOTICE messages
  for sum in $(tail -n +70 /tmp/md5sums | head -n 12)
  do
    logger -t ima_daemon -i "user hash captured" "NOTICE: $sum"
  done
  sleep 5

  # An ERROR log from the database that is not a md5sum as the content
  logger -t database -i "bad entry" "ERROR: user cletus does not exist"

  # now more other types messages
  for sum in $(tail -n +90 /tmp/md5sums | head -n 23)
  do
    logger -t ima_daemon -i "processing entry" "$sum"
  done
  for sum in $(tail -n +100 /tmp/md5sums | head -n 16)
  do
    logger -t database -i "record request" "$sum"
  done
  sleep 5

  rm /tmp/md5sums

}

# Main area

# Check if to be run as root (must do this first)
if [[ "${RUN_AS_ROOT}" == 'true' ]] && [[ "${EUID}" -gt "0" ]] ; then
  if [[ -x /usr/bin/sudo ]] ; then
    ${SUDO:-sudo} $0 "$@"
    exit
  else
    # Fail out if not running as root
    check_root
  fi
fi

# Process command line
# Parse input
case $1 in
    setup|grade)
        CMD=$1
        ;;
    -h|--help)
        print_usage
        exit 0
        ;;
    *)
        echo Bad option $1
        print_usage
        exit 1
        ;;
esac
# Validation
[ -z "$CMD" ] && debug "Missing command" && print_usage && exit 1

# Get local system information
get_X

# Branch based on short (without number) hostname
case $MYHOST in
    desktop|server)
        lab_$CMD $MYHOST
        ;;
    *)
        debug "Bad or missing hostname - $MYHOST"
        debug "This command needs to be run on desktop or server"
        exit 3
        ;;
esac

