OSDN Git Service

Merge branch 'dev' into dev-stable
[alterlinux/alterlinux.git] / system / aur.sh
1 #!/usr/bin/env bash
2 #
3 # Yamada Hayao
4 # Twitter: @Hayao0819
5 # Email  : hayao@fascode.net
6 #
7 # (c) 2019-2021 Fascode Network.
8 #
9 set -e -u
10
11 aur_username="aurbuild"
12
13 trap 'exit 1' 1 2 3 15
14
15 # Show message when file is removed
16 # remove <file> <file> ...
17 remove() {
18     local _file
19     for _file in "${@}"; do echo "Removing ${_file}" >&2; rm -rf "${_file}"; done
20 }
21
22 # user_check <name>
23 function user_check () {
24     if [[ ! -v 1 ]]; then return 2; fi
25     getent passwd "${1}" > /dev/null
26 }
27
28 # Creating a aur user.
29 if user_check "${aur_username}"; then
30     useradd -m -d "/aurbuild_temp" "${aur_username}"
31 fi
32 mkdir -p "/aurbuild_temp"
33 chmod 700 -R "/aurbuild_temp"
34 chown ${aur_username}:${aur_username} -R "/aurbuild_temp"
35 echo "${aur_username} ALL=(ALL) NOPASSWD:ALL" > "/etc/sudoers.d/aurbuild"
36
37 # Setup keyring
38 pacman-key --init
39 pacman-key --populate
40
41 # Un comment the mirror list.
42 #sed -i "s/#Server/Server/g" "/etc/pacman.d/mirrorlist"
43
44 # Install yay
45 if ! pacman -Qq yay 1> /dev/null 2>&1; then
46     (
47         _oldpwd="$(pwd)"
48         pacman -Syy --noconfirm --config "/etc/alteriso-pacman.conf"
49         pacman --noconfirm -S --asdeps --needed go --config "/etc/alteriso-pacman.conf"
50         sudo -u "${aur_username}" git clone "https://aur.archlinux.org/yay.git" "/tmp/yay"
51         cd "/tmp/yay"
52         sudo -u "${aur_username}" makepkg --ignorearch --clean --cleanbuild --force --skippgpcheck --noconfirm
53         pacman --noconfirm --config "/etc/alteriso-pacman.conf" -U $(sudo -u "${aur_username}" makepkg --packagelist)
54         cd ..
55         rm -rf "/tmp/yay"
56         cd "${_oldpwd}"
57     )
58 fi
59
60 if ! type -p yay > /dev/null; then
61     echo "Failed to install yay"
62     exit 1
63 fi
64
65
66 # Build and install
67 chmod +s /usr/bin/sudo
68 for _pkg in "${@}"; do
69     yes | sudo -u "${aur_username}" \
70         yay -Sy \
71             --mflags "-AcC" \
72             --aur \
73             --noconfirm \
74             --nocleanmenu \
75             --nodiffmenu \
76             --noeditmenu \
77             --noupgrademenu \
78             --noprovides \
79             --removemake \
80             --useask \
81             --color always \
82             --mflags "--skippgpcheck" \
83             --config "/etc/alteriso-pacman.conf" \
84             --cachedir "/var/cache/pacman/pkg/" \
85             "${_pkg}"
86
87     if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
88         echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
89         exit 1
90     fi
91 done
92
93 yay -Sccc --noconfirm --config "/etc/alteriso-pacman.conf"
94
95 # remove user and file
96 userdel "${aur_username}"
97 remove /aurbuild_temp
98 remove /etc/sudoers.d/aurbuild
99 remove "/etc/alteriso-pacman.conf"
100 remove "/var/cache/pacman/pkg/"