5 # Email : hayao@fascode.net
7 # (c) 2019-2021 Fascode Network.
11 build_username="pkgbuild"
16 echo "usage ${0} [option]"
18 echo "Build and install PKGBUILD"
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"
27 while getopts "du:xh" arg; do
29 d) pacman_debug=true ;;
30 u) build_username="${OPTARG}" ;;
43 shift "$((OPTIND - 1))"
45 # Show message when file is removed
46 # remove <file> <file> ...
49 for _file in "${@}"; do echo "Removing ${_file}" >&2; rm -rf "${_file}"; done
53 function user_check () {
54 if [[ ! -v 1 ]]; then return 2; fi
55 getent passwd "${1}" > /dev/null
59 function run_user () {
60 sudo -u "${build_username}" "${@}"
64 if [[ -z "${1}" ]]; then
65 echo "Please specify the directory that contains PKGBUILD." >&2
71 # Creating a user for makepkg
72 if ! user_check "${build_username}"; then
73 useradd -m -d "${pkgbuild_dir}" "${build_username}"
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"
84 # Un comment the mirror list.
85 #sed -i "s/#Server/Server/g" "/etc/pacman.d/mirrorlist"
88 pacman_args=("--config" "/etc/alteriso-pacman.conf" "--noconfirm")
89 if [[ "${pacman_debug}" = true ]]; then
90 pacman_args+=("--debug")
94 pacman -Syy "${pacman_args[@]}"
98 readarray -t pkgbuild_dirs < <(ls "${pkgbuild_dir}" 2> /dev/null)
99 if (( "${#pkgbuild_dirs[@]}" != 0 )); then
100 for _dir in "${pkgbuild_dirs[@]}"; do
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}"
111 run_user makepkg -fACcs --noconfirm --skippgpcheck
112 for pkg in $(run_user makepkg -f --packagelist); do
113 pacman --needed "${pacman_args[@]}" -U "${pkg}"
119 if readarray deletepkg < <(pacman -Qtdq) && (( "${#deletepkg[@]}" != 0 )); then
120 pacman -Rsnc "${deletepkg[@]}" "${pacman_args[@]}"
123 pacman -Sccc "${pacman_args[@]}"
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/"