Useful CLI tools (bash) for Arch Linux administration
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.

159 lines
4.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/bin/bash
  2. # getsource - Get build files for Arch/AUR packages on Arch Linux
  3. # Copyright (C) 2018 Pekka Helenius
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ###########################################################
  18. # TODO: Add support for wider range of processor architectures (below)
  19. # TODO: Add directory support (getsource wine ~/winesource)
  20. # TODO: create subdir for source files automatically to the current main dir
  21. ###########################################################
  22. ARCH_DOMAINURL="https://git.archlinux.org"
  23. ARCH_GITBASES=('packages.git' 'community.git')
  24. AUR_DOMAINURL="https://aur.archlinux.org"
  25. AUR_GITBASES=('aur.git')
  26. CURDIR=$(pwd | awk '{print $NF}' FS=/)
  27. if [[ -n "$1" ]]; then
  28. CURDIR="$1"
  29. else
  30. read -r -p "Source package name? [Default: $CURDIR] " response
  31. if [[ -n $response ]]; then
  32. CURDIR=$response
  33. fi
  34. fi
  35. INPUT="${CURDIR}"
  36. ##################################
  37. function check_database() {
  38. for GITBASE in ${2}; do
  39. if [[ "$1" != "$AUR_DOMAINURL" ]]; then
  40. BASEURL="$1/svntogit/$GITBASE/tree/trunk?h=packages/$CURDIR"
  41. DOMAINURL=$ARCH_DOMAINURL
  42. else
  43. BASEURL="$1/cgit/$GITBASE/snapshot/$CURDIR.tar.gz"
  44. DOMAINURL=$AUR_DOMAINURL
  45. fi
  46. wget -q -T 10 "$BASEURL" -o -
  47. if [[ $? -eq 0 ]]; then
  48. wget -q "$BASEURL"
  49. if [[ "$1" != "$AUR_DOMAINURL" ]]; then
  50. mv ./trunk?h=packages%2F$CURDIR ./baseurl.html
  51. else
  52. tar xf "$CURDIR.tar.gz"
  53. fi
  54. break
  55. fi
  56. done
  57. if [[ ! -f ./baseurl.html ]]; then
  58. return 1
  59. fi
  60. }
  61. ##################################
  62. function arch_repos_deepscan() {
  63. ARCH_DATABASES=(core extra community community-testing)
  64. for ARCH_DB in ${ARCH_DATABASES[*]}; do
  65. ARCH_DB_URL="https://www.archlinux.org/packages/$ARCH_DB/x86_64/$CURDIR"
  66. wget -q -T 10 "$ARCH_DB_URL" -o -
  67. if [[ $? -eq 0 ]]; then
  68. wget -q "$ARCH_DB_URL"
  69. mv ./$CURDIR ./baseurl_2.html
  70. break
  71. fi
  72. done
  73. if [[ -f baseurl_2.html ]]; then
  74. echo -e "Selecting another package name:\n"
  75. CURDIR=$(grep "Source Files" baseurl_2.html | sed "s/.*href=[\"'].*packages\///g; s/[\"'].*//g")
  76. echo -e "Package name is $CURDIR"
  77. rm baseurl_2.html
  78. check_database "$ARCH_DOMAINURL" "${ARCH_GITBASES[*]}"
  79. arch_dl_files
  80. else
  81. echo -e "\nCouldn't find package $CURDIR\n"
  82. exit 1
  83. fi
  84. }
  85. ##################################
  86. function arch_dl_files() {
  87. if [[ -f baseurl.html ]]; then
  88. FILELIST=$(cat baseurl.html | grep -E "ls-mode" | sed "s/.*href=[\"']//g; s/[\"']>plain.*//g")
  89. for file in $FILELIST; do
  90. if [[ ! -f $file ]]; then
  91. # Wget only if file doesn't exist
  92. wget -q $DOMAINURL/$file
  93. mv $(echo "$file" | sed 's/.*trunk//g; s/\///1' | sed 's/\//%2F/g') $(echo $file | sed 's/.*trunk//g; s/?.*//g; s/\///g')
  94. fi
  95. done
  96. rm baseurl.html
  97. echo -e "\nSource files for $CURDIR downloaded\n"
  98. elif [[ -f "$CURDIR.tar.gz" ]]; then
  99. mv ./$CURDIR/* ./
  100. rm -Rf {"$CURDIR.tar.gz",$CURDIR}
  101. echo -e "\nSource files for $CURDIR downloaded\n"
  102. else
  103. arch_repos_deepscan
  104. fi
  105. }
  106. ##################################
  107. check_database "$ARCH_DOMAINURL" "${ARCH_GITBASES[*]}"
  108. if [[ ! $? -eq 0 ]]; then
  109. check_database "$AUR_DOMAINURL" "${AUR_GITBASES[*]}"
  110. fi
  111. arch_dl_files
  112. ##################################
  113. # Check if we are raspberry pi (ARM 7) or not
  114. if [[ $(cat /proc/cpuinfo | grep -i armv7 -m1 | wc -l) -eq 1 ]]; then
  115. if [[ -f PKGBUILD ]]; then
  116. cat PKGBUILD | grep arch= | grep -E "any|armv7h" > /dev/null
  117. if [[ $? -ne 0 ]]; then
  118. sed -i "s/arch=.*/arch=('any')/" PKGBUILD
  119. echo -e "Modified architecture in PKGBUILD to 'any'\n"
  120. fi
  121. fi
  122. fi
  123. ##################################
  124. rm -rf ./{"${INPUT}"*.1,*trunk*} 2>/dev/null