107 lines
2.5 KiB
Bash
Executable File
107 lines
2.5 KiB
Bash
Executable File
#!/usr/bin/bash
|
|
|
|
# declare known fields
|
|
base_fields=(pkgver pkgrel epoch)
|
|
section_scalar_fields=(pkgdesc url install changelog)
|
|
section_arch_fields=(source depends checkdepends makedepends optdepends provides conflicts replaces md5sums sha1sums sha224sums sha256sums sha384sums sha512sums)
|
|
section_array_fields=(arch groups license noextract options backup validpgpkeys "${section_arch_fields[@]}")
|
|
section_fields=("${section_scalar_fields[@]}" "${section_array_fields[@]}")
|
|
|
|
# load PKGBUILD
|
|
set -e
|
|
cat "${1:-./PKGBUILD}"
|
|
source "${1:-./PKGBUILD}"
|
|
set +e
|
|
|
|
# check if a variable is an array
|
|
is_array() {
|
|
[[ "$(declare -p $1)" =~ "declare -a" ]]
|
|
}
|
|
|
|
# unset all field variables
|
|
clear_fields() {
|
|
for field in "${section_fields[@]}"; do
|
|
eval "unset $field"
|
|
done
|
|
}
|
|
|
|
# print a field with indentation
|
|
print_field() {
|
|
printf "\t$1 = %s\n" "$2"
|
|
}
|
|
|
|
# print a scalar field
|
|
print_scalar_field() {
|
|
eval "[[ -v "$1" ]] && print_field '$1' \"\${$1}\""
|
|
}
|
|
|
|
# print all values of an array field
|
|
print_array_field() {
|
|
if ! [[ -v "$1" ]]; then return; fi
|
|
if ! is_array "$1"; then
|
|
print_scalar_field "$1"
|
|
fi
|
|
local f
|
|
eval "for f in \"\${$1[@]}\"; do print_field "$1" \"\$f\"; done"
|
|
}
|
|
|
|
# load section content from its package function
|
|
load_section_content() {
|
|
if [[ -n $1 ]]; then
|
|
suffix="_$1"
|
|
else
|
|
suffix=""
|
|
fi
|
|
if [[ ! "$(type -t "package$suffix")" = function ]]; then return; fi
|
|
# eval only variable assignments in package function
|
|
while read -r line; do
|
|
eval "$line"
|
|
done < <(declare -f "package$suffix" | grep -E '^[[:space:]]*(declare +)?[[:alnum:]_]*=')
|
|
}
|
|
|
|
# print fields for a section
|
|
print_section_content() {
|
|
for field in "${section_scalar_fields[@]}"; do
|
|
print_scalar_field "$field"
|
|
done
|
|
|
|
for field in "${section_array_fields[@]}"; do
|
|
print_array_field "$field"
|
|
done
|
|
|
|
for a in "${arch[@]}"; do
|
|
for field in "${section_arch_fields[@]}"; do
|
|
print_array_field "${field}_$a"
|
|
done
|
|
done
|
|
}
|
|
|
|
# main
|
|
|
|
# determine pkgbase
|
|
if ! is_array pkgname; then
|
|
pkgname=($pkgname)
|
|
fi
|
|
printf "pkgbase = %s\n" "${pkgbase:-${pkgname[0]}}"
|
|
|
|
# run pkgver function if needed
|
|
if [[ ! -v pkgver ]] && [[ "$(type -t pkgver)" = function ]]; then
|
|
pkgver="$(pkgver)"
|
|
fi
|
|
|
|
# load base section content and print it
|
|
load_section_content
|
|
for field in "${base_fields[@]}"; do
|
|
print_scalar_field "$field"
|
|
done
|
|
print_section_content
|
|
|
|
# load section content for each split package and print it
|
|
for splitname in "${pkgname[@]}"; do
|
|
printf "\npkgname = %s\n" "$splitname"
|
|
clear_fields
|
|
load_section_content "$splitname"
|
|
print_section_content
|
|
done
|
|
|