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.

272 lines
8.9 KiB

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