gitmodule 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env bash
  2. # SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  3. # SPDX-License-Identifier: GPL-3.0-only
  4. Print_help(){
  5. cat <<- EOF
  6. Usage: ./download gitmodule [name]
  7. Options:
  8. name: The name of the module as specified in resources/git/revisions file
  9. EOF
  10. }
  11. Fail(){
  12. printf "${@}\n"
  13. Print_help
  14. exit 1
  15. }
  16. Check_vars(){
  17. if [ -z "${revision+x}" ]; then
  18. Fail 'Error: revision not set'
  19. fi
  20. if [ -z "${location+x}" ]; then
  21. Fail 'Error: location not set'
  22. fi
  23. if [ -z "${url+x}" ]; then
  24. Fail 'Error: url not set'
  25. fi
  26. }
  27. Patch(){
  28. for patchfile in ${PWD}/${patchdir}/*.patch ; do
  29. ( cd ${tmp_dir}
  30. git am ${patchfile} || return 1
  31. )
  32. done
  33. }
  34. Run(){
  35. git clone ${url} ${tmp_dir} || git clone ${bkup_url} ${tmp_dir} || Fail "ERROR: couldn't download ${name}\n Check Network connection"
  36. ( cd ${tmp_dir} && git reset --hard ${revision} )
  37. patchdir="resources/${name}/patches"
  38. if [ -d "${patchdir}" ]; then
  39. Patch || Fail "ERROR: Faild to patch ${name}"
  40. fi
  41. mv ${tmp_dir} ${location} || Fail "ERROR: couldn't copy temp to destination\n ${tmp_dir} > ${location} check permissions"
  42. }
  43. if [ -z "${1+x}" ]; then
  44. Fail 'Error: name not set'
  45. else
  46. name=${1}
  47. fi
  48. while read -r line ; do
  49. set ${line} >/dev/null 2>&1
  50. case ${line} in
  51. rev:*)
  52. revision=${2}
  53. ;;
  54. loc:*)
  55. location=${2}
  56. ;;
  57. url:*)
  58. url=${2}
  59. ;;
  60. bkup_url:*)
  61. bkup_url=${2}
  62. ;;
  63. esac
  64. done <<< $(eval "awk ' /\{.*${name}.*}{/ {flag=1;next} /\}/{flag=0} flag { print }' resources/git/revisions")
  65. Check_vars
  66. tmp_dir=$(mktemp -dt "${name}_XXXXX")
  67. # clean out old version just in case
  68. if [ -d "${location}" ]; then
  69. rm -rf ${location}
  70. fi
  71. Run
  72. # clean in case of failure
  73. rm -rf ${tmp_dir} >/dev/null 2>&1