#!/bin/bash
#
# Copyright 2014 Red Hat, Inc.
#
# NAME
#     lab-network - grading script for RH124/RH199/RH299 network lab
#
# SYNOPSIS
#     lab-network {setup|grade}
#
# DESCRIPTION
#     This script is the lab grading script for the RH124/RH199
#     network lab. It should only be executed on desktopX.example.com.
#
# CHANGELOG
#   * Tue Jul  8 2014 George Hacker <ghacker@redhat.com>
#   - corrected a regular expression that improperly matches IP addresses
#   * Wed Apr 23 2014 George Hacker <ghacker@redhat.com>
#   - original code (inspired by Rudy's grading code for the comp review)

PATH=/usr/bin:/bin:/usr/sbin:/sbin

# 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)
      echo "Nothing to set up on 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 *)
      ;;
    server)
      # Obviously adjust this to actual commands if script needs
      # to run on server - and adjust the catchall message with *)
      print_FAIL && echo "The setup script needs to be run on desktop"
      ;;
    *)
      # Should never get here, but what the hey....
      print_FAIL && echo "The setup script needs to be run on desktop"
      ;;
  esac
}

function grade_network {
#  local old_iface_config=/etc/sysconfig/network-scripts/ifcfg-eth0
#  local new_iface_config=/etc/sysconfig/network-scripts/ifcfg-lab
  local hosts_name=private

  echo -n "Checking for correct network settings... "
  if ! (nmcli con show |grep lab) &>/dev/null; then
    print_FAIL
    echo 'The network connection "lab" does not exist.'
    return 1
  fi

  (nmcli con show lab |grep ipv4.addresses |grep "ip *= *172\.25\.[[:digit:]]*\.10") &>/dev/null
  RESULT=$?
  if [[ "${RESULT}" -ne 0 ]]; then
    print_FAIL
    echo "The first ip address is not correct."
    return 1
  fi

# Commenting this out because the netmask will be different in ROL v. ILT/VT
#  (nmcli con show lab |grep ipv4.addresses |grep "ip *= *172\.25\.[[:digit:]]*\.10/24") &>/dev/null
#  RESULT=$?
#  if [ "${RESULT}" -ne 0 ]; then
#    print_FAIL
#    echo "The network prefix for the first ip address is not correct."
#    return 1
#  fi

  (nmcli con show lab |grep ipv4.dns |grep '172.25.254.254') &>/dev/null
  RESULT=$?
  if [ "${RESULT}" -ne 0 ]; then
    print_FAIL
    echo "The DNS name server is incorrect."
    return 1
  fi

  (nmcli con show lab |grep connection.autoconnect |grep yes) &>/dev/null
  RESULT=$?
  if [[ "${RESULT}" -ne 0 ]]; then
    print_FAIL
    echo "The review network device is not configured to start on boot."
    return 1
  fi

  (nmcli con show "System eth0" |grep connection.autoconnect |grep no) &>/dev/null
  RESULT=$?
  if [[ "${RESULT}" -ne 0 ]]; then
    print_FAIL
    echo "The original network device is configured to start on boot."
    return 1
  fi

  (nmcli con show lab |grep ipv4.addresses |grep "ip *= *10\.0\.[[:digit:]]*\.1") &>/dev/null
  RESULT=$?
  if [[ "${RESULT}" -ne 0 ]]; then
    print_FAIL
    echo "The second ip address has not been set."
    return 1
  fi
  
  (nmcli con show lab |grep ipv4.addresses |grep "ip *= *10\.0\.[[:digit:]]*\.1/24")  &>/dev/null
  RESULT=$?
  if [[ "${RESULT}" -ne 0 ]]; then
    print_FAIL
    echo "The network prefix for the second ip address is not correct."
    return 1
  fi

  grep "10\.0\.[[:digit:]]*\.1.*${hosts_name}" /etc/hosts &>/dev/null
  RESULT=$?
  if [[ "${RESULT}" -ne 0 ]]; then
    print_FAIL
    echo "The /etc/hosts file is not configured correctly."
    return 1
  fi
print_PASS
}

function lab_grade {
  VMNAME=$1
  case $VMNAME in
    desktop)
      grade_network || :
      ;;
    server)
      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
}

# 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
