OSDN Git Service

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