5 # Email : hayao@fascode.net
7 # (c) 2019-2021 Fascode Network.
11 build_username="pkgbuild"
17 echo "usage ${0} [option]"
19 echo "Build and install PKGBUILD"
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"
28 while getopts "du:xh" arg; do
30 d) pacman_debug=true ;;
31 u) build_username="${OPTARG}" ;;
44 shift "$((OPTIND - 1))"
46 # Show message when file is removed
47 # remove <file> <file> ...
50 for _file in "${@}"; do echo "Removing ${_file}" >&2; rm -rf "${_file}"; done
54 function user_check () {
55 if [[ ! -v 1 ]]; then return 2; fi
56 getent passwd "${1}" > /dev/null
60 function run_user () {
61 sudo -u "${build_username}" "${@}"
65 if [[ -z "${1}" ]]; then
66 echo "Please specify the directory that contains PKGBUILD." >&2
72 # Creating a user for makepkg
73 if ! user_check "${build_username}"; then
74 useradd -m -d "${pkgbuild_dir}" "${build_username}"
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"
85 # Un comment the mirror list.
86 #sed -i "s/#Server/Server/g" "/etc/pacman.d/mirrorlist"
89 pacman_args=("--config" "/etc/alteriso-pacman.conf" "--noconfirm")
90 if [[ "${pacman_debug}" = true ]]; then
91 pacman_args+=("--debug")
95 pacman -Syy "${pacman_args[@]}"
99 readarray -t pkgbuild_dirs < <(ls "${pkgbuild_dir}" 2> /dev/null)
100 if (( "${#pkgbuild_dirs[@]}" != 0 )); then
101 for _dir in "${pkgbuild_dirs[@]}"; do
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}"
112 run_user makepkg -fACcs --noconfirm --skippgpcheck
113 for pkg in $(run_user makepkg -f --packagelist); do
114 pacman --needed "${pacman_args[@]}" -U "${pkg}"
121 readarray -t -O "${#remove_list[@]}" remove_list < <(pacman -Qtdq)
122 (( "${#remove_list[@]}" != 0 )) && pacman -Rsnc "${remove_list[@]}" "${pacman_args[@]}"
124 pacman -Sccc "${pacman_args[@]}"
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/"