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.

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