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.

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