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.

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