OSDN Git Service

[update] : Skip pgp check
[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 # Delete file only if file exists
16 # remove <file1> <file2> ...
17 function remove () {
18     local _list
19     local _file
20     _list=($(echo "$@"))
21     for _file in "${_list[@]}"; do
22         if [[ -f ${_file} ]]; then
23             rm -f "${_file}"
24         elif [[ -d ${_file} ]]; then
25             rm -rf "${_file}"
26         fi
27         echo "${_file} was deleted."
28     done
29 }
30
31 # user_check <name>
32 function user_check () {
33     if [[ $(getent passwd $1 > /dev/null ; printf $?) = 0 ]]; then
34         if [[ -z $1 ]]; then
35             echo -n "false"
36         fi
37         echo -n "true"
38     else
39         echo -n "false"
40     fi
41 }
42
43 # Creating a aur user.
44 if [[ $(user_check ${aur_username}) = false ]]; then
45     useradd -m -d "/aurbuild_temp" "${aur_username}"
46 fi
47 mkdir -p "/aurbuild_temp"
48 chmod 700 -R "/aurbuild_temp"
49 chown ${aur_username}:${aur_username} -R "/aurbuild_temp"
50 echo "${aur_username} ALL=(ALL) NOPASSWD:ALL" > "/etc/sudoers.d/aurbuild"
51
52 # Setup keyring
53 pacman-key --init
54 #eval $(cat "/etc/systemd/system/pacman-init.service" | grep 'ExecStart' | sed "s|ExecStart=||g" )
55 ls "/usr/share/pacman/keyrings/"*".gpg" | sed "s|.gpg||g" | xargs | pacman-key --populate
56
57 # Un comment the mirror list.
58 #sed -i "s/#Server/Server/g" "/etc/pacman.d/mirrorlist"
59
60 # Install yay
61 if ! pacman -Qq yay 1> /dev/null 2>&1; then
62     (
63         _oldpwd="$(pwd)"
64         pacman -Syy --noconfirm --config "/etc/alteriso-pacman.conf"
65         pacman --noconfirm -S --asdeps --needed go --config "/etc/alteriso-pacman.conf"
66         sudo -u "${aur_username}" git clone "https://aur.archlinux.org/yay.git" "/tmp/yay"
67         cd "/tmp/yay"
68         sudo -u "${aur_username}" makepkg --ignorearch --clean --cleanbuild --force --skippgpcheck --noconfirm
69         pacman --noconfirm --config "/etc/alteriso-pacman.conf" -U $(sudo -u "${aur_username}" makepkg --packagelist)
70         cd ..
71         rm -rf "/tmp/yay"
72         cd "${_oldpwd}"
73     )
74 fi
75
76 if ! type -p yay > /dev/null; then
77     echo "Failed to install yay"
78     exit 1
79 fi
80
81
82 # Build and install
83 chmod +s /usr/bin/sudo
84 for _pkg in "${@}"; do
85     yes | sudo -u "${aur_username}" \
86         yay -Sy \
87             --mflags "-AcC" \
88             --aur \
89             --noconfirm \
90             --nocleanmenu \
91             --nodiffmenu \
92             --noeditmenu \
93             --noupgrademenu \
94             --noprovides \
95             --removemake \
96             --useask \
97             --color always \
98             --mflags "--skippgpcheck" \
99             --config "/etc/alteriso-pacman.conf" \
100             --cachedir "/var/cache/pacman/pkg/" \
101             "${_pkg}"
102
103     if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
104         echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
105         exit 1
106     fi
107 done
108
109 yay -Sccc --noconfirm --config "/etc/alteriso-pacman.conf"
110
111 # remove user and file
112 userdel "${aur_username}"
113 remove /aurbuild_temp
114 remove /etc/sudoers.d/aurbuild
115 remove "/etc/alteriso-pacman.conf"
116 remove "/var/cache/pacman/pkg/"