#!/bin/bash
#
# Copyright 2014 Red Hat, Inc.
#
# NAME
#     lab-disk - setup script for RH134/RH199/RH299 disk management labs
#
# SYNOPSIS
#     lab-disk setup

#     This script only works on serverX.example.com.
#
# DESCRIPTION
#     This script is the RH134/RH199 disk management lab setup script.
#
# CHANGELOG
#   * Wed Sep 10 2014 Scott McBrien <smcbrien@redhat.com>
#   - Updated grading for priorities to not care which swap has +1 and -1
#     priorities.
#   - Updated output to use echo -n to be more consistent with other grading
#     scripts.
#
#   * Thu Jun  5 2014 George Hacker <ghacker@redhat.com>
#   - added support for ILT/VT/ROL disk device differences

# 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)
            print_FAIL && echo "The setup script needs to be run on server"
            ;;
        server)
            ;;
        *)
            print_FAIL && echo "The setup script needs to be run on server"
            ;;
    esac

}

function check_fs {

    MNTDIR=/backup
    FS=xfs
    PART1MIN=1800000
    PART1MAX=2200000

    if ! (mount | grep -q "^${DEV}[0-9][0-9]* on $MNTDIR type $FS "); then
        print_FAIL
        echo "Did not find partition on $DEV mount at $MNTDIR with $FS filesystem."
        return
    fi

    BACKUPDEV=$(mount | grep "^$DEV[0-9][0-9]*" | awk '{print $1}')
    LABPART1SIZE=$(df -P /backup | grep -w ^$BACKUPDEV | awk '{print $2}')
    echo "LABPART1SIZE: $LABPART1SIZE"
    
    if [ $LABPART1SIZE -gt $PART1MIN ] && [ $LABPART1SIZE -lt $PART1MAX ]; then
        print_PASS
    else
        print_FAIL
        echo "$BACKUPDEV size incorrect."
    fi

}

function check_swap {

    if [ "$(grep "^${DEV}[0-9][0-9]*[[:space:]]\{1,\}partition[[:space:]]\{1,\}5[0-9]\{5\}[[:space:]]\{1,\}0[[:space:]]\{1,\}" /proc/swaps | wc -l)" -eq 2 ]; then
        print_PASS
    else
        print_FAIL
        echo "Did not find 2 512MB swap partitions ${DEV}."
    fi

    PRI=$(cat /proc/swaps | grep ^${DEV}[0-9][0-9]* | awk '{print $5}' | xargs)

    if [ "$PRI" = "-1 1" -o "$PRI" = "1 -1" ]; then
        print_PASS
    else
        print_FAIL
        echo "Did not find swap priorities of -1 and 1."
    fi
}

function lab_grade {
    VMNAME=$1
    case $VMNAME in
        desktop)
            print_FAIL && echo "The grade script needs to be run on server"
            ;;
        server)
            echo "Starting grading..."

	    # Call the library function that sets SDISKDEV
	    get_disk_devices
	    if [[ -z "${SDISKDEV}" ]]; then
	      echo "No secondary disk device recognized."
	      exit 15
	    fi
	    DEV=${SDISKDEV}

            echo -n "Checking mounted filesystem... "
            check_fs

            echo -n "Checking swap spaces... "
            check_swap
            ;;
        *)
            # Should never get here, but what the hey....
            print_FAIL && echo "The grade script needs to be run on server"
            ;;
    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
    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

