OSDN Git Service

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