2011-12-27 16:34:26 +01:00
|
|
|
#!/bin/bash
|
2012-01-03 11:29:52 +01:00
|
|
|
# Copyright 2010-2012, Asio Software Technologies
|
2011-12-29 14:29:24 +01:00
|
|
|
# Distributed under the terms of the GNU General Public License v3
|
|
|
|
|
2011-12-27 16:34:26 +01:00
|
|
|
|
2012-01-03 11:24:11 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Checks if system running EzBuild meets its requirements
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function checkPrerequisites() {
|
|
|
|
if [ "$(uname -s)" != "Linux" ]; then
|
|
|
|
printError "You are trying to launch ${EZNAME} on non-Linux system!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ "${EUID}" != "0" ] && [ "${USER}" != "root" ]; then
|
|
|
|
printError "The ${EZNAME} needs to be launched from root account!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ ! -e /dev/null ]; then
|
|
|
|
printError "The /dev directory seems to be not mounted!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ ! -e /proc/mounts ]; then
|
|
|
|
printError "The /proc directory seems to be not mounted!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
ping -c 1 google.com &> /dev/null
|
|
|
|
if [ ${?} -ne 0 ]; then
|
|
|
|
printError "There seems to be no Internet conectivity!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-12-31 11:59:56 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Tests for a minimum version level by comparing version numbers
|
|
|
|
# Parameters: %min_version% %test_version%
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function checkVersion() {
|
2012-01-02 23:09:58 +01:00
|
|
|
local BEST REF TST
|
|
|
|
REF=$(echo "${1}" | \
|
2012-01-01 12:03:43 +01:00
|
|
|
sed -u -e "s/-r/_z/g" -e "s/_p/_z/g" -e "s/_zre/_pre/" -e "s/$/_z0/")
|
2012-01-02 23:09:58 +01:00
|
|
|
TST=$(echo "${2}" | \
|
2012-01-01 12:03:43 +01:00
|
|
|
sed -u -e "s/-r/_z/g" -e "s/_p/_z/g" -e "s/_zre/_pre/" -e "s/$/_z0/")
|
2012-01-02 23:09:58 +01:00
|
|
|
BEST=$(echo -e "${REF}\n${TST}" | sed "/^$/d" | sort -Vr | head -1)
|
2012-01-01 12:03:43 +01:00
|
|
|
[[ "${BEST}" = "${TST}" ]] && return 0
|
2011-12-31 11:59:56 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-12-30 21:37:46 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# 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}"
|
2012-01-03 14:19:06 +01:00
|
|
|
echo -ne "\n"
|
2011-12-30 21:37:46 +01:00
|
|
|
exit ${2}
|
|
|
|
}
|
|
|
|
|
2012-01-01 12:28:20 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
2011-12-27 16:34:26 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
2011-12-28 13:45:31 +01:00
|
|
|
# Loads EzBuild configuration
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function loadConfiguration() {
|
2011-12-28 14:57:22 +01:00
|
|
|
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
|
2011-12-28 13:45:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Loads all EzBuild Libraries
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function loadLibraries() {
|
2012-01-03 13:03:20 +01:00
|
|
|
source ${EZROOT}/libraries/constants &> /dev/null || panic
|
2012-01-02 15:38:47 +01:00
|
|
|
source ${EZROOT}/libraries/filesystem &> /dev/null || panic
|
2011-12-28 14:35:43 +01:00
|
|
|
}
|
|
|
|
|
2011-12-29 23:58:25 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Saves specified message into a log file
|
2012-01-07 16:00:21 +01:00
|
|
|
# Parameters: %message% [%level%]
|
2011-12-29 23:58:25 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function logMessage() {
|
2012-01-02 23:09:58 +01:00
|
|
|
local DATE MESSAGE TYPE
|
2011-12-29 23:58:25 +01:00
|
|
|
if isEnabled ${LOGGING}; then
|
2012-01-02 23:09:58 +01:00
|
|
|
MESSAGE="${1}"
|
|
|
|
TYPE="${2}"
|
|
|
|
DATE=$(date +"${LOGDATEFORMAT}")
|
2011-12-29 23:58:25 +01:00
|
|
|
if [ -z ${TYPE} ]; then
|
2012-01-02 23:09:58 +01:00
|
|
|
TYPE=" - "
|
2011-12-29 23:58:25 +01:00
|
|
|
fi
|
2012-01-04 14:22:34 +01:00
|
|
|
{ echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}; } 2> /dev/null
|
2011-12-29 23:58:25 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Saves executed command multiline output into a log file
|
|
|
|
# Parameters: %command_output%
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function logOutput() {
|
|
|
|
if isEnabled ${LOGGING}; then
|
2012-01-07 13:56:57 +01:00
|
|
|
if [ -z "${@}" ]; then
|
|
|
|
echo -e " )> (NO OUTPUT)" >> ${LOGFILE}
|
|
|
|
else
|
|
|
|
echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
|
|
|
|
fi
|
2011-12-29 23:58:25 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2011-12-28 14:35:43 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Outputs error message and aborts program execution
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function panic() {
|
2011-12-28 14:57:22 +01:00
|
|
|
echo -e "FATAL ERROR: Unable to load necessary files!"
|
|
|
|
echo -e "Your EzBuild installation seems to be broken..."
|
2011-12-28 14:35:43 +01:00
|
|
|
exit 1
|
2011-12-28 13:45:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
2011-12-29 23:58:25 +01:00
|
|
|
# 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} ${@}"
|
|
|
|
}
|
|
|
|
|
2012-01-03 14:19:06 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Outputs formatted information about EzBuild version and copyrights
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function printNotice() {
|
|
|
|
echo -e "\n${COLOR_BOLD}EzBuild v${EZVERSION} (${EZNAME} launched)"
|
|
|
|
echo -e "${EZCOPYRIGHT}${COLOR_NORMAL}"
|
|
|
|
echo -e "${BORDER_STAR}\n"
|
|
|
|
}
|
|
|
|
|
2011-12-29 23:58:25 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Outputs formatted warning to both display and log file
|
|
|
|
# Parameters: %message%
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function printWarn() {
|
|
|
|
logMessage "${1}" "WARN"
|
|
|
|
echo -e " ${MESSAGE_WARN} ${@}"
|
|
|
|
}
|
|
|
|
|
2012-01-03 14:19:06 +01:00
|
|
|
function quit() {
|
|
|
|
echo -e "${COLOR_BOLD}ALL DONE!\n${COLOR_NORMAL}"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2011-12-30 12:45:38 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Restores original Internal Field Separator (IFS)
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function restoreIFS() {
|
|
|
|
if [ "${ORGIFS:-unset}" != "unset" ]; then
|
|
|
|
IFS="${ORGIFS}"
|
|
|
|
unset ORGIFS
|
|
|
|
else
|
|
|
|
unset IFS
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2011-12-29 23:58:25 +01:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Silently executes given command and saves its output to log file if enabled
|
|
|
|
# Parameters: %command%
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function run() {
|
2012-01-02 23:09:58 +01:00
|
|
|
local COMMAND OUTPUT RESULT
|
2012-01-02 23:35:25 +01:00
|
|
|
COMMAND="${@}"
|
2011-12-29 23:58:25 +01:00
|
|
|
logMessage "Executing: \"${COMMAND}\"" "DEBUG"
|
2012-01-02 23:09:58 +01:00
|
|
|
OUTPUT=$(${COMMAND} 2>&1)
|
|
|
|
RESULT=${?}
|
2011-12-29 23:58:25 +01:00
|
|
|
logOutput "${OUTPUT}"
|
|
|
|
return ${RESULT}
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
2011-12-30 12:45:38 +01:00
|
|
|
# Saves original Internal Field Separator (IFS) and optionally sets new value
|
|
|
|
# Parameters: %new_ifs%
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function saveIFS() {
|
2012-01-02 23:09:58 +01:00
|
|
|
local NEWIFS
|
2011-12-30 12:45:38 +01:00
|
|
|
if [ "${IFS:-unset}" != "unset" ]; then
|
|
|
|
ORGIFS="${IFS}"
|
|
|
|
fi
|
2012-01-02 23:09:58 +01:00
|
|
|
NEWIFS="${1}"
|
2011-12-30 12:45:38 +01:00
|
|
|
if isSet NEWIFS; then
|
|
|
|
IFS="${NEWIFS}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
2012-01-04 11:38:55 +01:00
|
|
|
# Prints EzSync usage information
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
function showEzsyncUsage() {
|
|
|
|
echo -e "\tEzSync is a part of EzBuild release building metatool used by"
|
|
|
|
echo -e "\treGen2 Linux for synchronizing Portage Tree with mainline.\n"
|
|
|
|
echo -e "USAGE: ezsync [parameters]"
|
2012-01-04 14:02:36 +01:00
|
|
|
echo -e " -c\t--cleanlog\tclears log file; not working with -p and -P"
|
|
|
|
echo -e " -h\t--help\t\tdisplays this screen and exits immediately"
|
2012-01-04 11:38:55 +01:00
|
|
|
echo -e " -p\t--purge\t\tclears temporary folder"
|
|
|
|
echo -e " -P\t--purgeonly\tclears temporary folder and exits"
|
|
|
|
echo -e " -s\t--syscheck\tperforms prerequisites check only"
|
|
|
|
echo -e "\n"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
2011-12-27 16:34:26 +01:00
|
|
|
# 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}
|
|
|
|
}
|