#!/bin/bash # Copyright 2010-2011, Asio Software Technologies # Distributed under the terms of the GNU General Public License v3 #------------------------------------------------------------------------------- # Tests for a minimum version level by comparing version numbers # Parameters: %min_version% %test_version% #------------------------------------------------------------------------------- function checkVersion() { local BEST REF TST REF=$(echo "${1}" | \ sed -u -e "s/-r/_z/g" -e "s/_p/_z/g" -e "s/_zre/_pre/" -e "s/$/_z0/") TST=$(echo "${2}" | \ sed -u -e "s/-r/_z/g" -e "s/_p/_z/g" -e "s/_zre/_pre/" -e "s/$/_z0/") BEST=$(echo -e "${REF}\n${TST}" | sed "/^$/d" | sort -Vr | head -1) [[ "${BEST}" = "${TST}" ]] && return 0 return 1 } #------------------------------------------------------------------------------- # Outputs formatted error message and aborts script execution with given code # Parameters: %message% %code% #------------------------------------------------------------------------------- function die() { if isEnabled ${LOGGING} && isSet NOTIFY; then local LOGS="Full output from logs:\n\n$(cat ${LOGFILE})" local MESG="The ${EZNAME} has failed with code: ${2} and message: ${1}" local TOPIC="The ${EZNAME} process has failed!" echo -e "${MESG}\n${LOGS}" | mailx -s "${TOPIC}" "${NOTIFY}" fi printError "${1}" exit ${2} } #------------------------------------------------------------------------------- # Checks whether a defined variable contain specified element or not # Parameters: %variable% %element% #------------------------------------------------------------------------------- function hasElement() { local NEEDLE=${1} shift local X for X in "${@}"; do [ "${X}" = "${NEEDLE}" ] && return 0 done return 1 } #------------------------------------------------------------------------------- # Checks whether a supplied variable is defined or not # Parameters: %variable% #------------------------------------------------------------------------------- function isDefined() { [[ ${!1-X} == ${!1-Y} ]] } #------------------------------------------------------------------------------- # Checks whether a supplied variable is enabled or not # Parameters: %variable% #------------------------------------------------------------------------------- function isEnabled() { case "${1}" in [Ee][Nn][Aa][Bb][Ll][Ee][Dd]) return 0 ;; [Tt][Rr][Uu][Ee]) return 0 ;; [Yy][Ee][Ss]) return 0 ;; 1) return 0 ;; esac return 1 } #------------------------------------------------------------------------------- # Checks whether a variable is defined and if value's length > 0 # Parameters: %variable% #------------------------------------------------------------------------------- function isSet() { if isDefined ${1}; then if [[ -n ${!1} ]]; then return 0 fi fi return 1 } #------------------------------------------------------------------------------- # Loads EzBuild configuration #------------------------------------------------------------------------------- function loadConfiguration() { source ${EZROOT}/config/${EZNAME}.conf &> /dev/null || panic source ${EZROOT}/config/layout.conf &> /dev/null || panic source /etc/ezbuild/${EZNAME}.conf &> /dev/null || panic source /etc/ezbuild/layout.conf &> /dev/null || panic } #------------------------------------------------------------------------------- # Loads all EzBuild Libraries #------------------------------------------------------------------------------- function loadLibraries() { source ${EZROOT}/libraries/colors &> /dev/null || panic source ${EZROOT}/libraries/filesystem &> /dev/null || panic } #------------------------------------------------------------------------------- # Saves specified message into a log file # Parameters: %message% %level% #------------------------------------------------------------------------------- function logMessage() { local DATE MESSAGE TYPE if isEnabled ${LOGGING}; then MESSAGE="${1}" TYPE="${2}" DATE=$(date +"${LOGDATEFORMAT}") if [ -z ${TYPE} ]; then TYPE=" - " fi echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE} fi } #------------------------------------------------------------------------------- # Saves executed command multiline output into a log file # Parameters: %command_output% #------------------------------------------------------------------------------- function logOutput() { if isEnabled ${LOGGING}; then echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE} fi } #------------------------------------------------------------------------------- # Outputs error message and aborts program execution #------------------------------------------------------------------------------- function panic() { echo -e "FATAL ERROR: Unable to load necessary files!" echo -e "Your EzBuild installation seems to be broken..." exit 1 } #------------------------------------------------------------------------------- # Outputs formatted error message to both display and log file # Parameters: %message% #------------------------------------------------------------------------------- function printError() { logMessage "${1}" "ERROR" echo -e " ${MESSAGE_ERROR} ${@}" } #------------------------------------------------------------------------------- # Outputs formatted information to both display and log file # Parameters: %message% #------------------------------------------------------------------------------- function printInfo() { logMessage "${1}" "INFO" echo -e " ${MESSAGE_INFO} ${@}" } #------------------------------------------------------------------------------- # Outputs formatted warning to both display and log file # Parameters: %message% #------------------------------------------------------------------------------- function printWarn() { logMessage "${1}" "WARN" echo -e " ${MESSAGE_WARN} ${@}" } #------------------------------------------------------------------------------- # Restores original Internal Field Separator (IFS) #------------------------------------------------------------------------------- function restoreIFS() { if [ "${ORGIFS:-unset}" != "unset" ]; then IFS="${ORGIFS}" unset ORGIFS else unset IFS fi } #------------------------------------------------------------------------------- # Silently executes given command and saves its output to log file if enabled # Parameters: %command% #------------------------------------------------------------------------------- function run() { local COMMAND OUTPUT RESULT COMMAND="${1}" logMessage "Executing: \"${COMMAND}\"" "DEBUG" OUTPUT=$(${COMMAND} 2>&1) RESULT=${?} logOutput "${OUTPUT}" return ${RESULT} } #------------------------------------------------------------------------------- # Saves original Internal Field Separator (IFS) and optionally sets new value # Parameters: %new_ifs% #------------------------------------------------------------------------------- function saveIFS() { local NEWIFS if [ "${IFS:-unset}" != "unset" ]; then ORGIFS="${IFS}" fi NEWIFS="${1}" if isSet NEWIFS; then IFS="${NEWIFS}" fi } #------------------------------------------------------------------------------- # Returns a UNIX timestamp #------------------------------------------------------------------------------- function timestamp() { echo $(date +%s) } #------------------------------------------------------------------------------- # Returns lowercase string # Parameters: %string% #------------------------------------------------------------------------------- function toLower() { echo "${@}" | tr '[:upper:]' '[:lower:]' } #------------------------------------------------------------------------------- # Returns uppercase string # Parameters: %string% #------------------------------------------------------------------------------- function toUpper() { echo "${@}" | tr '[:lower:]' '[:upper:]' } #------------------------------------------------------------------------------- # Returns only the first part of a string, delimited by tabs or spaces # Parameters: %string% #------------------------------------------------------------------------------- function trim() { echo ${1} }