OSDN Git Service

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