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.

225 lines
7.5 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 supplied variable is defined or not
  33. # Parameters: %variable%
  34. #-------------------------------------------------------------------------------
  35. function isDefined() {
  36. [[ ${!1-X} == ${!1-Y} ]]
  37. }
  38. #-------------------------------------------------------------------------------
  39. # Checks whether a supplied variable is enabled or not
  40. # Parameters: %variable%
  41. #-------------------------------------------------------------------------------
  42. function isEnabled() {
  43. case "${1}" in
  44. [Ee][Nn][Aa][Bb][Ll][Ee][Dd])
  45. return 0
  46. ;;
  47. [Tt][Rr][Uu][Ee])
  48. return 0
  49. ;;
  50. [Yy][Ee][Ss])
  51. return 0
  52. ;;
  53. 1)
  54. return 0
  55. ;;
  56. esac
  57. return 1
  58. }
  59. #-------------------------------------------------------------------------------
  60. # Checks whether a variable is defined and if value's length > 0
  61. # Parameters: %variable%
  62. #-------------------------------------------------------------------------------
  63. function isSet() {
  64. if isDefined ${1}; then
  65. if [[ -n ${!1} ]]; then
  66. return 0
  67. fi
  68. fi
  69. return 1
  70. }
  71. #-------------------------------------------------------------------------------
  72. # Loads EzBuild configuration
  73. #-------------------------------------------------------------------------------
  74. function loadConfiguration() {
  75. source ${EZROOT}/config/${EZNAME}.conf &> /dev/null || panic
  76. source ${EZROOT}/config/layout.conf &> /dev/null || panic
  77. source /etc/ezbuild/${EZNAME}.conf &> /dev/null || panic
  78. source /etc/ezbuild/layout.conf &> /dev/null || panic
  79. }
  80. #-------------------------------------------------------------------------------
  81. # Loads all EzBuild Libraries
  82. #-------------------------------------------------------------------------------
  83. function loadLibraries() {
  84. source ${EZROOT}/libraries/colors &> /dev/null || panic
  85. }
  86. #-------------------------------------------------------------------------------
  87. # Saves specified message into a log file
  88. # Parameters: %message% %level%
  89. #-------------------------------------------------------------------------------
  90. function logMessage() {
  91. if isEnabled ${LOGGING}; then
  92. local MESSAGE="${1}"
  93. local TYPE="${2}"
  94. local DATE=$(date +"${LOGDATEFORMAT}")
  95. if [ -z ${TYPE} ]; then
  96. local TYPE=" - "
  97. fi
  98. echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}
  99. fi
  100. }
  101. #-------------------------------------------------------------------------------
  102. # Saves executed command multiline output into a log file
  103. # Parameters: %command_output%
  104. #-------------------------------------------------------------------------------
  105. function logOutput() {
  106. if isEnabled ${LOGGING}; then
  107. echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
  108. fi
  109. }
  110. #-------------------------------------------------------------------------------
  111. # Outputs error message and aborts program execution
  112. #-------------------------------------------------------------------------------
  113. function panic() {
  114. echo -e "FATAL ERROR: Unable to load necessary files!"
  115. echo -e "Your EzBuild installation seems to be broken..."
  116. exit 1
  117. }
  118. #-------------------------------------------------------------------------------
  119. # Outputs formatted error message to both display and log file
  120. # Parameters: %message%
  121. #-------------------------------------------------------------------------------
  122. function printError() {
  123. logMessage "${1}" "ERROR"
  124. echo -e " ${MESSAGE_ERROR} ${@}"
  125. }
  126. #-------------------------------------------------------------------------------
  127. # Outputs formatted information to both display and log file
  128. # Parameters: %message%
  129. #-------------------------------------------------------------------------------
  130. function printInfo() {
  131. logMessage "${1}" "INFO"
  132. echo -e " ${MESSAGE_INFO} ${@}"
  133. }
  134. #-------------------------------------------------------------------------------
  135. # Outputs formatted warning to both display and log file
  136. # Parameters: %message%
  137. #-------------------------------------------------------------------------------
  138. function printWarn() {
  139. logMessage "${1}" "WARN"
  140. echo -e " ${MESSAGE_WARN} ${@}"
  141. }
  142. #-------------------------------------------------------------------------------
  143. # Restores original Internal Field Separator (IFS)
  144. #-------------------------------------------------------------------------------
  145. function restoreIFS() {
  146. if [ "${ORGIFS:-unset}" != "unset" ]; then
  147. IFS="${ORGIFS}"
  148. unset ORGIFS
  149. else
  150. unset IFS
  151. fi
  152. }
  153. #-------------------------------------------------------------------------------
  154. # Silently executes given command and saves its output to log file if enabled
  155. # Parameters: %command%
  156. #-------------------------------------------------------------------------------
  157. function run() {
  158. local COMMAND="${1}"
  159. logMessage "Executing: \"${COMMAND}\"" "DEBUG"
  160. local OUTPUT=$(${COMMAND} 2>&1)
  161. local RESULT=${?}
  162. logOutput "${OUTPUT}"
  163. return ${RESULT}
  164. }
  165. #-------------------------------------------------------------------------------
  166. # Saves original Internal Field Separator (IFS) and optionally sets new value
  167. # Parameters: %new_ifs%
  168. #-------------------------------------------------------------------------------
  169. function saveIFS() {
  170. if [ "${IFS:-unset}" != "unset" ]; then
  171. ORGIFS="${IFS}"
  172. fi
  173. local NEWIFS="${1}"
  174. if isSet NEWIFS; then
  175. IFS="${NEWIFS}"
  176. fi
  177. }
  178. #-------------------------------------------------------------------------------
  179. # Returns a UNIX timestamp
  180. #-------------------------------------------------------------------------------
  181. function timestamp() {
  182. echo $(date +%s)
  183. }
  184. #-------------------------------------------------------------------------------
  185. # Returns lowercase string
  186. # Parameters: %string%
  187. #-------------------------------------------------------------------------------
  188. function toLower() {
  189. echo "${@}" | tr '[:upper:]' '[:lower:]'
  190. }
  191. #-------------------------------------------------------------------------------
  192. # Returns uppercase string
  193. # Parameters: %string%
  194. #-------------------------------------------------------------------------------
  195. function toUpper() {
  196. echo "${@}" | tr '[:lower:]' '[:upper:]'
  197. }
  198. #-------------------------------------------------------------------------------
  199. # Returns only the first part of a string, delimited by tabs or spaces
  200. # Parameters: %string%
  201. #-------------------------------------------------------------------------------
  202. function trim() {
  203. echo ${1}
  204. }