OSDN Git Service

[fix] : Fixed pkgbuild depends (readarray -t)
[alterlinux/alterlinux.git] / system / pkgbuild.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
10
11 build_username="pkgbuild"
12 pacman_debug=false
13 pacman_args=()
14
15 _help() {
16     echo "usage ${0} [option]"
17     echo
18     echo "Build and install PKGBUILD" 
19     echo
20     echo " General options:"
21     echo "    -d                       Enable pacman debug message"
22     echo "    -u [user]                Set the user name to build packages"
23     echo "    -x                       Enable bash debug message"
24     echo "    -h                       This help message"
25 }
26
27 while getopts "du:xh" arg; do
28     case "${arg}" in
29         d) pacman_debug=true ;;
30         u) build_username="${OPTARG}" ;;
31         x) set -xv ;;
32         h) 
33             _help
34             exit 0
35             ;;
36         *)
37             _help
38             exit 1
39             ;;
40     esac
41 done
42
43 shift "$((OPTIND - 1))"
44
45 # Show message when file is removed
46 # remove <file> <file> ...
47 remove() {
48     local _file
49     for _file in "${@}"; do echo "Removing ${_file}" >&2; rm -rf "${_file}"; done
50 }
51
52 # user_check <name>
53 function user_check () {
54     if [[ ! -v 1 ]]; then return 2; fi
55     getent passwd "${1}" > /dev/null
56 }
57
58 # 一般ユーザーで実行します
59 function run_user () {
60     sudo -u "${build_username}" "${@}"
61 }
62
63 # 引数を確認
64 if [[ -z "${1}" ]]; then
65     echo "Please specify the directory that contains PKGBUILD." >&2
66     exit 1
67 else
68     pkgbuild_dir="${1}"
69 fi
70
71 # Creating a user for makepkg
72 if ! user_check "${build_username}"; then
73     useradd -m -d "${pkgbuild_dir}" "${build_username}"
74 fi
75 mkdir -p "${pkgbuild_dir}"
76 chmod 700 -R "${pkgbuild_dir}"
77 chown -R "${build_username}" "${pkgbuild_dir}"
78 echo "${build_username} ALL=(ALL) NOPASSWD:ALL" > "/etc/sudoers.d/pkgbuild"
79
80 # Setup keyring
81 pacman-key --init
82 pacman-key --populate
83
84 # Un comment the mirror list.
85 #sed -i "s/#Server/Server/g" "/etc/pacman.d/mirrorlist"
86
87 # Set pacman args
88 pacman_args=("--config" "/etc/alteriso-pacman.conf" "--noconfirm")
89 if [[ "${pacman_debug}" = true ]]; then
90     pacman_args+=("--debug")
91 fi
92
93 # Update datebase
94 pacman -Syy "${pacman_args[@]}"
95
96 # Parse SRCINFO
97 cd "${pkgbuild_dir}"
98 readarray -t pkgbuild_dirs < <(ls "${pkgbuild_dir}" 2> /dev/null)
99 if (( "${#pkgbuild_dirs[@]}" != 0 )); then
100     for _dir in "${pkgbuild_dirs[@]}"; do
101         cd "${_dir}"
102         readarray -t depends < <(source "${pkgbuild_dir}/${_dir}/PKGBUILD"; printf "%s\n" "${depends[@]}")
103         readarray -t makedepends < <(source "${pkgbuild_dir}/${_dir}/PKGBUILD"; printf "%s\n" "${makedepends[@]}")
104         if (( ${#depends[@]} + ${#makedepends[@]} != 0 )); then
105             for _pkg in "${depends[@]}" "${makedepends[@]}"; do
106                 if pacman -Ssq "${_pkg}" | grep -x "${_pkg}" 1> /dev/null; then
107                     pacman -S --asdeps --needed "${pacman_args[@]}" "${_pkg}"
108                 fi
109             done
110         fi
111         run_user makepkg -fACcs --noconfirm --skippgpcheck
112         for pkg in $(run_user makepkg -f --packagelist); do
113             pacman --needed "${pacman_args[@]}" -U "${pkg}"
114         done
115         cd - >/dev/null
116     done
117 fi
118
119 if readarray deletepkg < <(pacman -Qtdq) &&  (( "${#deletepkg[@]}" != 0 )); then
120     pacman -Rsnc "${deletepkg[@]}" "${pacman_args[@]}"
121 fi
122
123 pacman -Sccc "${pacman_args[@]}"
124
125 # remove user and file
126 userdel "${build_username}"
127 remove "${pkgbuild_dir}"
128 remove "/etc/sudoers.d/pkgbuild"
129 remove "/etc/alteriso-pacman.conf"
130 remove "/var/cache/pacman/pkg/"