OSDN Git Service

[fix] : Fixed -e and -a not being applied
[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     # Update database
104     _oldpwd="$(pwd)"
105     pacman -Syy "${pacman_args[@]}"
106
107     # Install depends
108     for _pkg in "${aur_helper_depends[@]}"; do
109         if ! pacman -Qq "${_pkg}" > /dev/null 2>&1 | grep -q "${_pkg}"; then
110             # --asdepsをつけているのでaur.shで削除される --neededをつけているので明示的にインストールされている場合削除されない
111             pacman -S --asdeps --needed "${pacman_args[@]}" "${_pkg}"
112             #remove_list+=("${_pkg}")
113         fi
114     done
115
116     # Build
117     sudo -u "${aur_username}" git clone "https://aur.archlinux.org/${aur_helper_package}.git" "/tmp/${aur_helper_package}"
118     cd "/tmp/${aur_helper_package}"
119     sudo -u "${aur_username}" makepkg --ignorearch --clean --cleanbuild --force --skippgpcheck --noconfirm
120
121     # Install
122     for _pkg in $(sudo -u "${aur_username}" makepkg --packagelist); do
123         pacman "${pacman_args[@]}" -U "${_pkg}"
124     done
125
126     # Remove debtis
127     cd ..
128     remove "/tmp/${aur_helper_package}"
129     cd "${_oldpwd}"
130 fi
131
132 if ! type -p "${aur_helper_command}" > /dev/null; then
133     echo "Failed to install ${aur_helper_package}"
134     exit 1
135 fi
136
137 installpkg(){
138     yes | sudo -u "${aur_username}" \
139         "${aur_helper_command}" -Sy \
140             --color always \
141             --cachedir "/var/cache/pacman/pkg/" \
142             "${pacman_args[@]}" \
143             "${aur_helper_args[@]}" \
144             "${@}" || true
145 }
146
147
148 # Build and install
149 chmod +s /usr/bin/sudo
150 for _pkg in "${@}"; do
151     pacman -Qq "${_pkg}" > /dev/null 2>&1  && continue
152     installpkg "${_pkg}"
153
154     if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
155         echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
156         failedpkg+=("${_pkg}")
157     fi
158 done
159
160 # Reinstall failed package
161 for _pkg in "${failedpkg[@]}"; do
162     installpkg "${_pkg}"
163     if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
164         echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
165         exit 1
166     fi
167 done
168
169 # Remove packages
170 readarray -t -O "${#remove_list[@]}" remove_list < <(pacman -Qttdq)
171 (( "${#remove_list[@]}" != 0 )) && pacman -Rsnc "${remove_list[@]}" "${pacman_args[@]}"
172
173 # Clean up
174 "${aur_helper_command}" -Sccc "${pacman_args[@]}"
175
176 # remove user and file
177 userdel "${aur_username}"
178 remove /aurbuild_temp
179 remove /etc/sudoers.d/aurbuild
180 remove "/etc/alteriso-pacman.conf"
181 remove "/var/cache/pacman/pkg/"