libreboot-sources 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env bash
  2. # Copyright (C) 2016 Paul Kocialkowski <contact@paulk.fr>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. BUILD_SYSTEM="libreboot"
  17. SOURCES="sources"
  18. ARCHIVE="tar.xz"
  19. usage() {
  20. printf 1>&2 '%s\n' "$executable [action] [sources path] (extract path)"
  21. printf 1>&2 '\n%s\n' 'Actions:'
  22. printf 1>&2 '%s\n' ' extract - Extract build system sources'
  23. printf 1>&2 '%s\n' ' copy - Copy projects sources'
  24. printf 1>&2 '%s\n' ' prepare - Extract and copy sources'
  25. printf '\n%s\n' ' When no extract path is provided, sources are extracted in the current'
  26. printf '%s\n' ' directory.'
  27. }
  28. extract() {
  29. local sources_path=$1
  30. local extract_path=$2
  31. local build_system_path
  32. local archive
  33. build_system_path="$extract_path/$BUILD_SYSTEM"
  34. if [ -d "$build_system_path" ]
  35. then
  36. return
  37. fi
  38. archive=$( find $sources_path -name "$BUILD_SYSTEM-sources.$ARCHIVE" || true )
  39. if [ -z "$archive" ]
  40. then
  41. printf 1>&2 '%s\n' "Finding $BUILD_SYSTEM sources archive failed!"
  42. usage
  43. exit 1
  44. fi
  45. tar -xf "$archive" -ps -C "$extract_path"
  46. printf '\n%s\n' "Extracted $BUILD_SYSTEM sources from $sources_path to $extract_path"
  47. }
  48. copy() {
  49. local sources_path=$1
  50. local extract_path=$2
  51. local build_system_path
  52. local build_system_sources_path
  53. local archives
  54. local file
  55. build_system_path="$extract_path/$BUILD_SYSTEM"
  56. build_system_sources_path="$build_system_path/$SOURCES"
  57. if ! [ -d "$build_system_path" ]
  58. then
  59. return
  60. fi
  61. mkdir -p "$build_system_path/$SOURCES"
  62. ( find "$sources_path" -type f -not -name "$BUILD_SYSTEM*" || true ) | while read file
  63. do
  64. cp "$file" "$build_system_sources_path"
  65. done
  66. printf '\n%s\n' "Copied $BUILD_SYSTEM sources from $sources_path to $extract_path"
  67. }
  68. requirements() {
  69. local requirement
  70. local requirement_path
  71. for requirement in "$@"
  72. do
  73. requirement_path=$( which "$requirement" || true )
  74. if [ -z "$requirement_path" ]
  75. then
  76. printf 1>&2 '%s\n' "Missing requirement: $requirement"
  77. exit 1
  78. fi
  79. done
  80. }
  81. setup() {
  82. root=$(readlink -f "$( dirname "$0" )" )
  83. executable=$( basename "$0" )
  84. }
  85. libreboot_sources() {
  86. local action=$1
  87. local sources_path=$2
  88. local extract_path=$3
  89. set -e
  90. setup "$@"
  91. if [ -z "$sources_path" ]
  92. then
  93. usage
  94. exit 1
  95. fi
  96. if [ -z "$extract_path" ]
  97. then
  98. extract_path=$root
  99. fi
  100. requirements "tar" "sha256sum" "gpg"
  101. case $action in
  102. "extract")
  103. extract "$sources_path" "$extract_path"
  104. ;;
  105. "copy")
  106. copy "$sources_path" "$extract_path"
  107. ;;
  108. "prepare")
  109. extract "$sources_path" "$extract_path"
  110. copy "$sources_path" "$extract_path"
  111. ;;
  112. esac
  113. }
  114. libreboot_sources "$@"