#!/bin/bash
#
# Copyright 2014 Red Hat, Inc.
#
# NAME
#     lab-nfs - grading script for RH134/RH199/RH299 NFS lab
#
#     This script only works on desktopX.example.com.
#
# SYNOPSIS
#     lab-nfs {setup|grade}
#
# DESCRIPTION
#     This script is the lab grading script for the RH134/RH199 NFS lab.
#
# CHANGELOG
#   * Thu Jun 12 2014 Wander Boessenkool <wboessen@redhat.com>
#   - original code

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

CLNT_PKG="sssd"
CLNT_ERR="Error running script. Did you reset the desktop before running this script? \
Contact your instructor if you continue to see this message."

# Source library of functions
source /usr/local/bin/labtool.shlib
trap on_exit EXIT
LOG_TAG=$(basename $0)

# Additional functions for this shell script
function srv_exit {
  # call debug with msg and exit with code
  DEBUG=true
  debug "$1"
  exit $2
}

function install_pkgs {
  # install the package that provides the service
  echo "Installing packages ..."
  yum -y install $@ &> /dev/null || \
    srv_exit "yum failed: $?" 150
}

function update_authconfig {
  # update the authconfig for ldap & krb5
  echo "Updating authconfig for ldap & krb5 ..."
  authconfig \
    --enableldap \
    --enableldaptls \
    --ldapserver=ldaps://classroom.example.com \
    --ldapbasedn=dc=example,dc=com \
    --ldaploadcacert=http://classroom.example.com/pub/example-ca.crt \
    --enablekrb5 \
    --krb5realm=EXAMPLE.COM \
    --krb5kdc=classroom.example.com \
    --krb5adminserver=classroom.example.com \
    --enablesssd \
    --enablesssdauth \
    --update &> /dev/null || \
    srv_exit "authconfig failed: $?" 151
}

# Grading scripts
function check_enabled {
  echo -n "Checking autofs service is enabled... "
  if systemctl is-enabled autofs &> /dev/null
  then
    print_PASS
  else
    print_FAIL
    echo "Service autofs is not enabled."
    exit 0
  fi
}

function grade_autofs {
  echo -n "Checking automounted home directory... "
  TESTUSER=ldapuser${X}
  TESTHOME=/home/guests/${TESTUSER}
  DATA="$(su - ${TESTUSER} -c pwd 2>/dev/null)"
  if [ "${DATA}" != "${TESTHOME}" ]; then
    print_FAIL
    echo "Home directory not available for ${TESTUSER}"
    exit 0
  fi
  if ! mount | grep '/home/guests' | grep -q nfs; then
    print_FAIL
    echo "${TESTHOME} not mounted over NFS"
    exit 0
  fi
  if grep -q '/home/guests' /etc/fstab; then
    print_FAIL
    echo "Guest home directories mounted from /etc/fstab"
    exit 0
  fi
  print_PASS
}

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)
            ERROR_MESSAGE="$CLNT_ERR"
            install_pkgs $CLNT_PKG
            update_authconfig
            print_SUCCESS
            ;;
        server)
            # Should never get here, but what the hey....
            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 lab_grade {
    VMNAME=$1
    case $VMNAME in
        desktop)
            check_enabled
            grade_autofs
            print_SUCCESS
            ;;
        server)
            # Obviously adjust this to actual commands if script needs
            # to run on server - and adjust the catchall message with *)
            print_FAIL && echo "Please run the grading for this lab on the desktop."
            ;;
        *)
            # Should never get here, but what the hey....
            print_FAIL && echo "Please run the grading for this lab on the 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

