OSDN Git Service

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