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.

157 lines
4.6 KiB

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. ##################################
  36. function check_database() {
  37. for GITBASE in ${2}; do
  38. if [[ "$1" != "$AUR_DOMAINURL" ]]; then
  39. BASEURL="$1/svntogit/$GITBASE/tree/trunk?h=packages/$CURDIR"
  40. DOMAINURL=$ARCH_DOMAINURL
  41. else
  42. BASEURL="$1/cgit/$GITBASE/snapshot/$CURDIR.tar.gz"
  43. DOMAINURL=$AUR_DOMAINURL
  44. fi
  45. wget -q -T 5 "$BASEURL" -o -
  46. if [[ $? -eq 0 ]]; then
  47. wget -q "$BASEURL"
  48. if [[ "$1" != "$AUR_DOMAINURL" ]]; then
  49. mv ./trunk?h=packages%2F$CURDIR ./baseurl.html
  50. else
  51. tar xf "$CURDIR.tar.gz"
  52. fi
  53. break
  54. fi
  55. done
  56. if [[ ! -f ./baseurl.html ]]; then
  57. return 1
  58. fi
  59. }
  60. ##################################
  61. function arch_repos_deepscan() {
  62. ARCH_DATABASES=(core extra community community-testing)
  63. for ARCH_DB in ${ARCH_DATABASES[*]}; do
  64. ARCH_DB_URL="https://www.archlinux.org/packages/$ARCH_DB/x86_64/$CURDIR"
  65. wget -q -T 5 "$ARCH_DB_URL" -o -
  66. if [[ $? -eq 0 ]]; then
  67. wget -q "$ARCH_DB_URL"
  68. mv ./$CURDIR ./baseurl_2.html
  69. break
  70. fi
  71. done
  72. if [[ -f baseurl_2.html ]]; then
  73. echo -e "Selecting another package name:\n"
  74. CURDIR=$(grep "Source Files" baseurl_2.html | sed "s/.*href=[\"'].*packages\///g; s/[\"'].*//g")
  75. echo -e "Package name is $CURDIR"
  76. rm baseurl_2.html
  77. check_database "$ARCH_DOMAINURL" "${ARCH_GITBASES[*]}"
  78. arch_dl_files
  79. else
  80. echo -e "\nCouldn't find package $CURDIR\n"
  81. exit 1
  82. fi
  83. }
  84. ##################################
  85. function arch_dl_files() {
  86. if [[ -f baseurl.html ]]; then
  87. FILELIST=$(cat baseurl.html | grep -E "ls-mode" | sed "s/.*href=[\"']//g; s/[\"']>plain.*//g")
  88. for file in $FILELIST; do
  89. if [[ ! -f $file ]]; then
  90. # Wget only if file doesn't exist
  91. wget -q $DOMAINURL/$file
  92. mv $(echo "$file" | sed 's/.*trunk//g; s/\///1' | sed 's/\//%2F/g') $(echo $file | sed 's/.*trunk//g; s/?.*//g; s/\///g')
  93. fi
  94. done
  95. rm baseurl.html
  96. echo -e "\nSource files for $CURDIR downloaded\n"
  97. elif [[ -f "$CURDIR.tar.gz" ]]; then
  98. mv ./$CURDIR/* ./
  99. rm -Rf {"$CURDIR.tar.gz",$CURDIR}
  100. echo -e "\nSource files for $CURDIR downloaded\n"
  101. else
  102. arch_repos_deepscan
  103. fi
  104. }
  105. ##################################
  106. check_database "$ARCH_DOMAINURL" "${ARCH_GITBASES[*]}"
  107. if [[ ! $? -eq 0 ]]; then
  108. check_database "$AUR_DOMAINURL" "${AUR_GITBASES[*]}"
  109. fi
  110. arch_dl_files
  111. ##################################
  112. # Check if we are raspberry pi (ARM 7) or not
  113. if [[ $(cat /proc/cpuinfo | grep -i armv7 -m1 | wc -l) -eq 1 ]]; then
  114. if [[ -f PKGBUILD ]]; then
  115. cat PKGBUILD | grep arch= | grep -E "any|armv7h" > /dev/null
  116. if [[ $? -ne 0 ]]; then
  117. sed -i "s/arch=.*/arch=('any')/" PKGBUILD
  118. echo -e "Modified architecture in PKGBUILD to 'any'\n"
  119. fi
  120. fi
  121. fi
  122. ##################################
  123. rm -Rf ./{,*trunk*} 2>/dev/null