reGen2 Linux Release Metatool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
8.0 KiB

9 years ago
  1. #!/bin/bash
  2. # Copyright 2010-2011, Asio Software Technologies
  3. # Distributed under the terms of the GNU General Public License v3
  4. #-------------------------------------------------------------------------------
  5. # Tests for a minimum version level by comparing version numbers
  6. # Parameters: %min_version% %test_version%
  7. #-------------------------------------------------------------------------------
  8. function checkVersion() {
  9. local REF=$(echo "${1}" | \
  10. sed -u -e "s/-r/_z/g" -e "s/_p/_z/g" -e "s/_zre/_pre/" -e "s/$/_z0/")
  11. local TST=$(echo "${2}" | \
  12. sed -u -e "s/-r/_z/g" -e "s/_p/_z/g" -e "s/_zre/_pre/" -e "s/$/_z0/")
  13. local BEST=$(echo -e "${REF}\n${TST}" | sed "/^$/d" | sort -Vr | head -1)
  14. [[ "${BEST}" = "${TST}" ]] && return 0
  15. return 1
  16. }
  17. #-------------------------------------------------------------------------------
  18. # Outputs formatted error message and aborts script execution with given code
  19. # Parameters: %message% %code%
  20. #-------------------------------------------------------------------------------
  21. function die() {
  22. if isEnabled ${LOGGING} && isSet NOTIFY; then
  23. local LOGS="Full output from logs:\n\n$(cat ${LOGFILE})"
  24. local MESG="The ${EZNAME} has failed with code: ${2} and message: ${1}"
  25. local TOPIC="The ${EZNAME} process has failed!"
  26. echo -e "${MESG}\n${LOGS}" | mailx -s "${TOPIC}" "${NOTIFY}"
  27. fi
  28. printError "${1}"
  29. exit ${2}
  30. }
  31. #-------------------------------------------------------------------------------
  32. # Checks whether a defined variable contain specified element or not
  33. # Parameters: %variable% %element%
  34. #-------------------------------------------------------------------------------
  35. function hasElement() {
  36. local NEEDLE=${1}
  37. shift
  38. local X
  39. for X in "${@}"; do
  40. [ "${X}" = "${NEEDLE}" ] && return 0
  41. done
  42. return 1
  43. }
  44. #-------------------------------------------------------------------------------
  45. # Checks whether a supplied variable is defined or not
  46. # Parameters: %variable%
  47. #-------------------------------------------------------------------------------
  48. function isDefined() {
  49. [[ ${!1-X} == ${!1-Y} ]]
  50. }
  51. #-------------------------------------------------------------------------------
  52. # Checks whether a supplied variable is enabled or not
  53. # Parameters: %variable%
  54. #-------------------------------------------------------------------------------
  55. function isEnabled() {
  56. case "${1}" in
  57. [Ee][Nn][Aa][Bb][Ll][Ee][Dd])
  58. return 0
  59. ;;
  60. [Tt][Rr][Uu][Ee])
  61. return 0
  62. ;;
  63. [Yy][Ee][Ss])
  64. return 0
  65. ;;
  66. 1)
  67. return 0
  68. ;;
  69. esac
  70. return 1
  71. }
  72. #-------------------------------------------------------------------------------
  73. # Checks whether a variable is defined and if value's length > 0
  74. # Parameters: %variable%
  75. #-------------------------------------------------------------------------------
  76. function isSet() {
  77. if isDefined ${1}; then
  78. if [[ -n ${!1} ]]; then
  79. return 0
  80. fi
  81. fi
  82. return 1
  83. }
  84. #-------------------------------------------------------------------------------
  85. # Loads EzBuild configuration
  86. #-------------------------------------------------------------------------------
  87. function loadConfiguration() {
  88. source ${EZROOT}/config/${EZNAME}.conf &> /dev/null || panic
  89. source ${EZROOT}/config/layout.conf &> /dev/null || panic
  90. source /etc/ezbuild/${EZNAME}.conf &> /dev/null || panic
  91. source /etc/ezbuild/layout.conf &> /dev/null || panic
  92. }
  93. #-------------------------------------------------------------------------------
  94. # Loads all EzBuild Libraries
  95. #-------------------------------------------------------------------------------
  96. function loadLibraries() {
  97. source ${EZROOT}/libraries/colors &> /dev/null || panic
  98. source ${EZROOT}/libraries/filesystem &> /dev/null || panic
  99. }
  100. #-------------------------------------------------------------------------------
  101. # Saves specified message into a log file
  102. # Parameters: %message% %level%
  103. #-------------------------------------------------------------------------------
  104. function logMessage() {
  105. if isEnabled ${LOGGING}; then
  106. local MESSAGE="${1}"
  107. local TYPE="${2}"
  108. local DATE=$(date +"${LOGDATEFORMAT}")
  109. if [ -z ${TYPE} ]; then
  110. local TYPE=" - "
  111. fi
  112. echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}
  113. fi
  114. }
  115. #-------------------------------------------------------------------------------
  116. # Saves executed command multiline output into a log file
  117. # Parameters: %command_output%
  118. #-------------------------------------------------------------------------------
  119. function logOutput() {
  120. if isEnabled ${LOGGING}; then
  121. echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
  122. fi
  123. }
  124. #-------------------------------------------------------------------------------
  125. # Outputs error message and aborts program execution
  126. #-------------------------------------------------------------------------------
  127. function panic() {
  128. echo -e "FATAL ERROR: Unable to load necessary files!"
  129. echo -e "Your EzBuild installation seems to be broken..."
  130. exit 1
  131. }
  132. #-------------------------------------------------------------------------------
  133. # Outputs formatted error message to both display and log file
  134. # Parameters: %message%
  135. #-------------------------------------------------------------------------------
  136. function printError() {
  137. logMessage "${1}" "ERROR"
  138. echo -e " ${MESSAGE_ERROR} ${@}"
  139. }
  140. #-------------------------------------------------------------------------------
  141. # Outputs formatted information to both display and log file
  142. # Parameters: %message%
  143. #-------------------------------------------------------------------------------
  144. function printInfo() {
  145. logMessage "${1}" "INFO"
  146. echo -e " ${MESSAGE_INFO} ${@}"
  147. }
  148. #-------------------------------------------------------------------------------
  149. # Outputs formatted warning to both display and log file
  150. # Parameters: %message%
  151. #-------------------------------------------------------------------------------
  152. function printWarn() {
  153. logMessage "${1}" "WARN"
  154. echo -e " ${MESSAGE_WARN} ${@}"
  155. }
  156. #-------------------------------------------------------------------------------
  157. # Restores original Internal Field Separator (IFS)
  158. #-------------------------------------------------------------------------------
  159. function restoreIFS() {
  160. if [ "${ORGIFS:-unset}" != "unset" ]; then
  161. IFS="${ORGIFS}"
  162. unset ORGIFS
  163. else
  164. unset IFS
  165. fi
  166. }
  167. #-------------------------------------------------------------------------------
  168. # Silently executes given command and saves its output to log file if enabled
  169. # Parameters: %command%
  170. #-------------------------------------------------------------------------------
  171. function run() {
  172. local COMMAND="${1}"
  173. logMessage "Executing: \"${COMMAND}\"" "DEBUG"
  174. local OUTPUT=$(${COMMAND} 2>&1)
  175. local RESULT=${?}
  176. logOutput "${OUTPUT}"
  177. return ${RESULT}
  178. }
  179. #-------------------------------------------------------------------------------
  180. # Saves original Internal Field Separator (IFS) and optionally sets new value
  181. # Parameters: %new_ifs%
  182. #-------------------------------------------------------------------------------
  183. function saveIFS() {
  184. if [ "${IFS:-unset}" != "unset" ]; then
  185. ORGIFS="${IFS}"
  186. fi
  187. local NEWIFS="${1}"
  188. if isSet NEWIFS; then
  189. IFS="${NEWIFS}"
  190. fi
  191. }
  192. #-------------------------------------------------------------------------------
  193. # Returns a UNIX timestamp
  194. #-------------------------------------------------------------------------------
  195. function timestamp() {
  196. echo $(date +%s)
  197. }
  198. #-------------------------------------------------------------------------------
  199. # Returns lowercase string
  200. # Parameters: %string%
  201. #-------------------------------------------------------------------------------
  202. function toLower() {
  203. echo "${@}" | tr '[:upper:]' '[:lower:]'
  204. }
  205. #-------------------------------------------------------------------------------
  206. # Returns uppercase string
  207. # Parameters: %string%
  208. #-------------------------------------------------------------------------------
  209. function toUpper() {
  210. echo "${@}" | tr '[:lower:]' '[:upper:]'
  211. }
  212. #-------------------------------------------------------------------------------
  213. # Returns only the first part of a string, delimited by tabs or spaces
  214. # Parameters: %string%
  215. #-------------------------------------------------------------------------------
  216. function trim() {
  217. echo ${1}
  218. }