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.

239 lines
7.9 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. }
  99. #-------------------------------------------------------------------------------
  100. # Saves specified message into a log file
  101. # Parameters: %message% %level%
  102. #-------------------------------------------------------------------------------
  103. function logMessage() {
  104. if isEnabled ${LOGGING}; then
  105. local MESSAGE="${1}"
  106. local TYPE="${2}"
  107. local DATE=$(date +"${LOGDATEFORMAT}")
  108. if [ -z ${TYPE} ]; then
  109. local TYPE=" - "
  110. fi
  111. echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}
  112. fi
  113. }
  114. #-------------------------------------------------------------------------------
  115. # Saves executed command multiline output into a log file
  116. # Parameters: %command_output%
  117. #-------------------------------------------------------------------------------
  118. function logOutput() {
  119. if isEnabled ${LOGGING}; then
  120. echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
  121. fi
  122. }
  123. #-------------------------------------------------------------------------------
  124. # Outputs error message and aborts program execution
  125. #-------------------------------------------------------------------------------
  126. function panic() {
  127. echo -e "FATAL ERROR: Unable to load necessary files!"
  128. echo -e "Your EzBuild installation seems to be broken..."
  129. exit 1
  130. }
  131. #-------------------------------------------------------------------------------
  132. # Outputs formatted error message to both display and log file
  133. # Parameters: %message%
  134. #-------------------------------------------------------------------------------
  135. function printError() {
  136. logMessage "${1}" "ERROR"
  137. echo -e " ${MESSAGE_ERROR} ${@}"
  138. }
  139. #-------------------------------------------------------------------------------
  140. # Outputs formatted information to both display and log file
  141. # Parameters: %message%
  142. #-------------------------------------------------------------------------------
  143. function printInfo() {
  144. logMessage "${1}" "INFO"
  145. echo -e " ${MESSAGE_INFO} ${@}"
  146. }
  147. #-------------------------------------------------------------------------------
  148. # Outputs formatted warning to both display and log file
  149. # Parameters: %message%
  150. #-------------------------------------------------------------------------------
  151. function printWarn() {
  152. logMessage "${1}" "WARN"
  153. echo -e " ${MESSAGE_WARN} ${@}"
  154. }
  155. #-------------------------------------------------------------------------------
  156. # Restores original Internal Field Separator (IFS)
  157. #-------------------------------------------------------------------------------
  158. function restoreIFS() {
  159. if [ "${ORGIFS:-unset}" != "unset" ]; then
  160. IFS="${ORGIFS}"
  161. unset ORGIFS
  162. else
  163. unset IFS
  164. fi
  165. }
  166. #-------------------------------------------------------------------------------
  167. # Silently executes given command and saves its output to log file if enabled
  168. # Parameters: %command%
  169. #-------------------------------------------------------------------------------
  170. function run() {
  171. local COMMAND="${1}"
  172. logMessage "Executing: \"${COMMAND}\"" "DEBUG"
  173. local OUTPUT=$(${COMMAND} 2>&1)
  174. local RESULT=${?}
  175. logOutput "${OUTPUT}"
  176. return ${RESULT}
  177. }
  178. #-------------------------------------------------------------------------------
  179. # Saves original Internal Field Separator (IFS) and optionally sets new value
  180. # Parameters: %new_ifs%
  181. #-------------------------------------------------------------------------------
  182. function saveIFS() {
  183. if [ "${IFS:-unset}" != "unset" ]; then
  184. ORGIFS="${IFS}"
  185. fi
  186. local NEWIFS="${1}"
  187. if isSet NEWIFS; then
  188. IFS="${NEWIFS}"
  189. fi
  190. }
  191. #-------------------------------------------------------------------------------
  192. # Returns a UNIX timestamp
  193. #-------------------------------------------------------------------------------
  194. function timestamp() {
  195. echo $(date +%s)
  196. }
  197. #-------------------------------------------------------------------------------
  198. # Returns lowercase string
  199. # Parameters: %string%
  200. #-------------------------------------------------------------------------------
  201. function toLower() {
  202. echo "${@}" | tr '[:upper:]' '[:lower:]'
  203. }
  204. #-------------------------------------------------------------------------------
  205. # Returns uppercase string
  206. # Parameters: %string%
  207. #-------------------------------------------------------------------------------
  208. function toUpper() {
  209. echo "${@}" | tr '[:lower:]' '[:upper:]'
  210. }
  211. #-------------------------------------------------------------------------------
  212. # Returns only the first part of a string, delimited by tabs or spaces
  213. # Parameters: %string%
  214. #-------------------------------------------------------------------------------
  215. function trim() {
  216. echo ${1}
  217. }