# Library of functions useful for lab scripts (feel free to add more!)
# by: Joshua M. Hoffman (20100902)
#
# To use, just add this line at the top of your script:
# . /path/to/labtool.shlib
#
# Use log to send a message to syslog.
# Example:
# log "something happened"
#
# Even better, use debug to send messages to the syslog. The difference is that
# debug messages are also sent to stdout if DEBUG=true is set in your script.
# Example:
# debug "something happened"
#
# If a script must be run as root, use the check_ root function and if the
# script is not running as root, it will tell the user and exit.
# To use, add this line right below the trap line:
# check_root
#
# To use the error handling, simply call exit with a non zero value. The script
# will exit and display the value of ERROR_MESSAGE to the user. If you do not
# set an ERROR_MESSAGE, the default below will be used.
# Example:
# shutdown -r now || exit 2
# rm -rf /etc || ERROR_MESSAGE="Failed to delete /etc ; exit 3
#
# Also, it is a good idea to override the default LOG_TAG with the name of your
# script. To override the default LOG_TAG (or any of the default below) just
# change the value in your script.
# Example:
# LOG_TAG="banhammer"
#
# CHANGELOG
#   * Thu Jul 10 2014 George Hacker <ghacker@redhat.com>
#   - modified get_X function to use /etc/rht values when possible
#   * Thu Jun  5 2014 George Hacker <ghacker@redhat.com>
#   - added get_disk_devices function

# defaults
LOG_FACILITY=local0
LOG_PRIORITY=info
LOG_TAG="${0##*/}"
DEBUG=false
ERROR_MESSAGE="Error running script. Contact your instructor if you continue \
to see this message."
PACKAGES=( bash )

# paths
LOGGER='/usr/bin/logger'
RPM='/bin/rpm'
SUDO='/usr/bin/sudo'

# Export LANG so we get consistent results
# For instance, fr_FR uses comma (,) as the decimal separator.
export LANG=en_US.UTF-8

# Read in GLS parameters if available
[ -r /etc/rht ] && . /etc/rht

# Set up exit handler (no need for user to do this)
trap on_exit EXIT

function log {

if [[ ${#1} -gt 0 ]] ; then
    $LOGGER -p ${LOG_FACILITY}.${LOG_PRIORITY} -t $LOG_TAG -- "$1"
else
    while read data ; do
        $LOGGER -p ${LOG_FACILITY}.${LOG_PRIORITY} -t $LOG_TAG -- "$1" "$data"   
    done
fi

}


function debug {

if [[ ${#1} -gt 0 ]] ; then
    msg="$1"

    if [[ "$DEBUG" = "true" ]] ; then
        echo "$msg"
    fi

    log "$msg"

else

    while read data ; do

        if [[ "$DEBUG" = "true" ]] ; then
            echo "$data"
        fi

        log "$data"

    done

fi

}


function on_exit {

status="$?"

if [[ "$status" -eq "0" ]] ; then
    exit 0
else
    DEBUG=true
    debug "$ERROR_MESSAGE"
    exit "$status"
fi

}


function check_root {

if [[ "$EUID" -gt "0" ]] ; then
    log 'Not running run as root = Fail'
    ERROR_MESSAGE='This script must be run as root!'
    exit 1
fi

}


function check_packages {

for package in ${PACKAGES[@]} ; do

    if $RPM -q $package &>/dev/null ; then
        continue
    else
        ERROR_MESSAGE="Please install $package and try again."
        exit 2

    fi
done

}


function confirm {

read -p "Is this ok [y/N]: " userInput

case "${userInput:0:1}" in
    "y" | "Y")
        return
        ;;
    *)
        ERROR_MESSAGE="Script aborted."
        exit 3
        ;;
esac

}


function check_host {

if [[ ${#1} -gt 0 ]]; then
    if [[ "$1" == "${HOSTNAME:0:${#1}}" ]]; then
        return
    else
        ERROR_MESSAGE="This script must be run on ${1}."
        exit 4
    fi
fi

}


function check_tcp_port {

if [[ ${#1} -gt 0 && ${#2} -gt 0 ]]; then
    # Sending it to the log always returns 0
    ($(echo "brain" >/dev/tcp/$1/$2)) && return 0
fi
return 1

}


function wait_tcp_port {

if [[ ${#1} -gt 0 && ${#2} -gt 0 ]]; then
    # Make sure it is pingable before we attempt the port check
    echo
    echo -n "Pinging $1"
    until `ping -c1 -w1 $1 &> /dev/null`;do
        echo -n "."
        sleep 3
    done

    iterations=0
    echo
    echo 'You may see a few "Connection refused" errors before it connects...'
    sleep 10
    until [[ "$remote_port" == "smart" || $iterations -eq 30 ]]; do
        ($(echo "brain" >/dev/tcp/$1/$2) ) && remote_port="smart" || remote_port="dumb"
        sleep 3
        iterations=$(expr $iterations + 1)
    done
    [[ $remote_port == "smart" ]] && return 0
fi
return 1

}


function push_sshkey {

if [[ ${#1} -gt 0 ]]; then
    rm -f /root/.ssh/known_hosts
    rm -f /root/.ssh/.labtoolkey
    rm -f /root/.ssh/.labtoolkey.pub
    (ssh-keygen -q -N "" -f /root/.ssh/.labtoolkey) || return 1
    (/usr/local/lib/labtool-installkey /root/.ssh/.labtoolkey.pub $1) && return 0
fi
return 1
    
}


function get_X {

  if [[ -n "${RHT_ENROLLMENT}" ]] ; then
    X="${RHT_ENROLLMENT}"
    MYHOST="${RHT_ROLE}"
  elif hostname -s | grep -q '[0-9]' ; then
    X="$(hostname -s | grep -o '[0-9]*')"
    MYHOST="$(hostname -s | grep -o '[^0-9]*')"
  else
    # If the short hostname does not have a number, it is probably localhost.
    return 1
  fi
  SERVERX="server${X}.example.com"
  DESKTOPX="desktop${X}.example.com"

  # *** The following variables are deprecated. Do not use them.
# TWO_DIGIT_X="$(printf %02i ${X})"
# TWO_DIGIT_HEX="$(printf %02x ${X})"
# LASTIPOCTET="$(hostname -i | cut -d. -f4)"
# # IPOCTETX should match X
# IPOCTETX="$(hostname -i | cut -d. -f3)"

  return 0

}


function get_disk_devices {

  # This functions assumes / is mounted on a physical partition,
  #   and that the secondary disk is of the same type.
  PDISK=$(df | grep '/$' | sed 's:/dev/\([a-z]*\).*:\1:')
  SDISK=$(grep -v "${PDISK}" /proc/partitions | sed '1,2d; s/.* //' |
          grep "${PDISK:0:${#PDISK}-1}.$" | sort | head -n 1)

  PDISKDEV=/dev/${PDISK}
  SDISKDEV=/dev/${SDISK}

}


function print_PASS() {
  echo -e '\033[1;32mPASS\033[0;39m'
}


function print_FAIL() {
  echo -e '\033[1;31mFAIL\033[0;39m'
}


function print_SUCCESS() {
  echo -e '\033[1;36mSUCCESS\033[0;39m'
}


# vim: ai ts=4 sts=4 sw=4 et
