#!/bin/bash
#
# Copyright 2014 Red Hat, Inc.
#
# NAME
#     lab-dnsunboundissues - setup/grading script for RH254/RH299 DNS lab
#
# SYNOPSIS
#     lab-dnsunboundissues {setup|grade}
#
#     The setup function only works on serverX.example.com.
#     The grading function only works on desktopX.example.com.
#
# DESCRIPTION
#     This script performs the setup and grading for the DNS lab
#     for RH254.
#
# CHANGELOG
#   * Thu Jun 12 2014 Chen Chang <cchang@redhat.com>
#   - fix for Jira RH254-146
#   * Tue May 27 2014 Chen Chang <cchang@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_grade {
    VMNAME=$1
    case $VMNAME in
        desktop)
            if diff <(dig +short @172.25.254.254 A example.com 2> /dev/null) <(dig +short @server${X}.example.com A example.com 2> /dev/null) &> /dev/null
            then
              print_PASS
            else
             print_FAIL
             echo "Failed name resolution for example.com."
            fi
            ;;

        server)
            print_FAIL && echo "The grade script needs to be run on desktop"
            ;;
        *)
            print_FAIL && echo "The grade script needs to be run on desktop"
            ;;
    esac
}

function lab_setup {
    VMNAME=$1
    case $VMNAME in
        desktop)
            print_FAIL && echo "The setup script needs to be run on server"
            ;;
        server)
            echo "Executing setup.  This may take a minute..."

            # Install bind
            yum install -y unbound &> /dev/null

            if [ $? -ne 0 ]; then
              print_FAIL && echo "Failed to install bind."
            fi

            # Configure unbound server
            sed -i 's/# domain-insecure/domain-insecure/' /etc/unbound/unbound.conf

            if [ $? -ne 0 ]; then
              print_FAIL && echo "Failed populating /etc/unbound/local.d/server.conf."
            fi

            # Configure unbound forward
            cat << FWDEOF >> /etc/unbound/unbound.conf 2> /dev/null

forward-zone:
 name: "."
 forward-addr: 172.25.254.254
FWDEOF

            if [ $? -ne 0 ]; then
              print_FAIL && echo "Failed populating /etc/unbound/conf.d/forward.conf."
            fi

            # Add firewall rule
            firewall-cmd --permanent --add-service=dns &> /dev/null

            if [ $? -ne 0 ]; then
              print_FAIL && echo "Failed adding firewall rule."
            fi

            # Reload firewall rule
            firewall-cmd --reload &> /dev/null

            if [ $? -ne 0 ]; then
              print_FAIL && echo "Failed reloading firewall rule."
            fi

            # Enable unbound
            systemctl enable unbound &> /dev/null

            if [ $? -ne 0 ]; then
              print_FAIL && echo "Failed enabling unbound service."
            fi

            # Start unbound
            systemctl start unbound > /dev/null

            if [ $? -ne 0 ]; then
              print_FAIL && echo "Failed starting unbound service."
            fi
            ;;
        *)
            print_FAIL && echo "The setup 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
    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

