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.

196 lines
6.3 KiB

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