gitclone 1.8 KB

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