OSDN Git Service

[fix] : Fixed pacman output
[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 pacman_debug=false
13 pacman_args=()
14 failedpkg=()
15 remove_list=()
16 yay_depends=("go")
17
18 trap 'exit 1' 1 2 3 15
19
20 _help() {
21     echo "usage ${0} [option]"
22     echo
23     echo "Install aur packages with yay" 
24     echo
25     echo " General options:"
26     echo "    -d                       Enable pacman debug message"
27     echo "    -u [user]                Set the user name to build packages"
28     echo "    -x                       Enable bash debug message"
29     echo "    -h                       This help message"
30 }
31
32 while getopts "du:xh" arg; do
33     case "${arg}" in
34         d) pacman_debug=true ;;
35         u) aur_username="${OPTARG}" ;;
36         x) set -xv ;;
37         h) 
38             _help
39             exit 0
40             ;;
41         *)
42             _help
43             exit 1
44             ;;
45     esac
46 done
47
48 shift "$((OPTIND - 1))"
49
50 # Show message when file is removed
51 # remove <file> <file> ...
52 remove() {
53     local _file
54     for _file in "${@}"; do echo "Removing ${_file}" >&2; rm -rf "${_file}"; done
55 }
56
57 # user_check <name>
58 function user_check () {
59     if [[ ! -v 1 ]]; then return 2; fi
60     getent passwd "${1}" > /dev/null
61 }
62
63 # Creating a aur user.
64 if ! user_check "${aur_username}"; then
65     useradd -m -d "/aurbuild_temp" "${aur_username}"
66 fi
67 mkdir -p "/aurbuild_temp"
68 chmod 700 -R "/aurbuild_temp"
69 chown "${aur_username}:${aur_username}" -R "/aurbuild_temp"
70 echo "${aur_username} ALL=(ALL) NOPASSWD:ALL" > "/etc/sudoers.d/aurbuild"
71
72 # Setup keyring
73 pacman-key --init
74 pacman-key --populate
75
76 # Un comment the mirror list.
77 #sed -i "s/#Server/Server/g" "/etc/pacman.d/mirrorlist"
78
79 # Set pacman args
80 pacman_args=("--config" "/etc/alteriso-pacman.conf" "--noconfirm")
81 if [[ "${pacman_debug}" = true ]]; then
82     pacman_args+=("--debug")
83 fi
84
85 # Install yay
86 if ! pacman -Qq yay 1> /dev/null 2>&1; then
87     # Update database
88     _oldpwd="$(pwd)"
89     pacman -Syy "${pacman_args[@]}"
90
91     # Install depends
92     for _pkg in "${yay_depends[@]}"; do
93         if ! pacman -Qq "${_pkg}" > /dev/null 2>&1 | grep -q "${_pkg}"; then
94             pacman -S --asdeps --needed "${pacman_args[@]}" "${_pkg}"
95             remove_list+=("${_pkg}")
96         fi
97     done
98
99     # Build yay
100     sudo -u "${aur_username}" git clone "https://aur.archlinux.org/yay.git" "/tmp/yay"
101     cd "/tmp/yay"
102     sudo -u "${aur_username}" makepkg --ignorearch --clean --cleanbuild --force --skippgpcheck --noconfirm
103
104     # Install yay
105     for _pkg in $(sudo -u "${aur_username}" makepkg --packagelist); do
106         pacman "${pacman_args[@]}" -U "${_pkg}"
107     done
108
109     # Remove debtis
110     cd ..
111     remove "/tmp/yay"
112     cd "${_oldpwd}"
113 fi
114
115 if ! type -p yay > /dev/null; then
116     echo "Failed to install yay"
117     exit 1
118 fi
119
120 installpkg(){
121     yes | sudo -u "${aur_username}" \
122         yay -Sy \
123             --mflags "-AcC" \
124             --aur \
125             --nocleanmenu \
126             --nodiffmenu \
127             --noeditmenu \
128             --noupgrademenu \
129             --noprovides \
130             --removemake \
131             --useask \
132             --color always \
133             --mflags "--skippgpcheck" \
134             "${pacman_args[@]}" \
135             --cachedir "/var/cache/pacman/pkg/" \
136             "${@}" || true
137 }
138
139
140 # Build and install
141 chmod +s /usr/bin/sudo
142 for _pkg in "${@}"; do
143     pacman -Qq "${_pkg}" > /dev/null 2>&1  && continue
144     installpkg "${_pkg}"
145
146     if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
147         echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
148         failedpkg+=("${_pkg}")
149     fi
150 done
151
152 # Reinstall failed package
153 for _pkg in "${failedpkg[@]}"; do
154     installpkg "${_pkg}"
155     if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
156         echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
157         exit 1
158     fi
159 done
160
161 # Remove packages
162 readarray -t -O "${#remove_list[@]}" remove_list < <(pacman -Qttdq)
163 (( "${#remove_list[@]}" != 0 )) && pacman -Rsnc "${remove_list[@]}" "${pacman_args[@]}"
164
165 # Clean up
166 yay -Sccc "${pacman_args[@]}"
167
168 # remove user and file
169 userdel "${aur_username}"
170 remove /aurbuild_temp
171 remove /etc/sudoers.d/aurbuild
172 remove "/etc/alteriso-pacman.conf"
173 remove "/var/cache/pacman/pkg/"