OSDN Git Service

[fix] : Use alteriso pacman.conf
[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     remove_yay=false
63 else
64     (
65         _oldpwd="$(pwd)"
66         pacman -Syy --noconfirm
67         pacman --noconfirm -S --asdeps --needed go --config "/etc/alteriso-pacman.conf"
68         sudo -u aurbuild git clone "https://aur.archlinux.org/yay.git" "/tmp/yay"
69         cd "/tmp/yay"
70         sudo -u aurbuild makepkg --ignorearch --clean --cleanbuild --force --skippgpcheck --noconfirm
71         pacman --noconfirm --config "/etc/alteriso-pacman.conf" -U $(makepkg --packagelist)
72         cd ..
73         rm -rf "/tmp/yay"
74         cd "${_oldpwd}"
75     )
76 fi
77
78 if ! type -p yay > /dev/null; then
79     echo "Failed to install yay"
80     exit 1
81 fi
82
83
84 # Build and install
85 chmod +s /usr/bin/sudo
86 for _pkg in "${@}"; do
87     yes | sudo -u aurbuild \
88         yay -Sy \
89             --mflags "-AcC" \
90             --aur \
91             --noconfirm \
92             --nocleanmenu \
93             --nodiffmenu \
94             --noeditmenu \
95             --noupgrademenu \
96             --noprovides \
97             --removemake \
98             --useask \
99             --color always \
100             --config "/etc/alteriso-pacman.conf" \
101             --cachedir "/var/cache/pacman/pkg/" \
102             "${_pkg}"
103
104     if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
105         echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
106         exit 1
107     fi
108 done
109
110 yay -Sccc --noconfirm --config "/etc/alteriso-pacman.conf"
111
112 # remove user and file
113 userdel aurbuild
114 remove /aurbuild_temp
115 remove /etc/sudoers.d/aurbuild
116 remove "/etc/alteriso-pacman.conf"
117 remove "/var/cache/pacman/pkg/"