diff options
Diffstat (limited to 'src/ob-gencontrol.sh')
-rw-r--r-- | src/ob-gencontrol.sh | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/src/ob-gencontrol.sh b/src/ob-gencontrol.sh new file mode 100644 index 0000000..b09b993 --- /dev/null +++ b/src/ob-gencontrol.sh @@ -0,0 +1,165 @@ +#!@@SH@@ +# +# opkbuild +# src/ob-gencontrol +# Generates a control directory with a control file and scripts. +# +# Copyright (C) 2012 Patrick "P. J." McDermott +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +. '@@LIBOPKBUILD@@/load.sm' + +ob_use locale +ob_use output +ob_use package + +main() +{ + ob_set_text_domain 'opkbuild' + + ob_init_package '..' + ob_parse_package_metadata -c '.opkbuild.cache' + + if [ -n "${OB_DO_SOURCE}" ]; then + gen_control "src:${OB_SOURCE}" "${OB_SOURCE_VERSION}" 'src' \ + "${OB_SOURCE} source package" 'false' + gen_md5sums "src:${OB_SOURCE}" + else + for pkg in $(ob_get_binary_packages); do + arch="$(ob_get_binary_parameter "${pkg}" 'Architecture')" + [ "${arch}" != 'all' ] && arch="${OB_HOST_ARCH}" + gen_control "${pkg}" "${OB_BINARY_VERSION}" "${arch}" \ + "$(ob_get_binary_parameter "${pkg}" 'Description')" 'true' + install_maintainer_scripts "${pkg}" + gen_conffiles "${pkg}" + gen_md5sums "${pkg}" + done + fi +} + +gen_control() +{ + binary="${1}" + version="${2}" + arch="${3}" + desc="${4}" + gen_rel="${5}" + + # Calculate installed size. + # Don't use du, since that considers the allocated size of files, symbolic + # links, and directories (i.e. actual file sizes plus the filesystem + # overhead on the build system). + # See the following for more information: + # <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/du.html> + # <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=630533> + # <https://www.gnu.org/software/coreutils/manual/html_node/du-invocation.html> + sizes="$(find "${binary}.data" -type f -exec wc -c '{}' ';' | \ + cut -d ' ' -f 1)" + inst_size=0 + for size in ${sizes}; do + inst_size=$(($inst_size + $size)) + done + # Convert bytes to kibibytes and round up. + # Note: There is an inconsistency between the Debian Policy Manual and opkg + # in the units of this field. Debian Policy defines this field in units of + # kibibytes: + # The disk space is given as the integer value of the estimated installed + # size in bytes, divided by 1024 and rounded up. + # However, opkg apparently attempts to convert this value from bytes to + # kibibytes in its determination of whether the package's data will fit on + # the system: + # pkg_size_kbs = (pkg->installed_size + 1023)/1024; + # TODO: Further investigate opkg's disk space calculation and, if necessary, + # patch opkg and submit a bug report. + inst_size=$((($inst_size + 1023) / 1024)) + + mkdir -p "${binary}.control" + + cat >"${binary}.control/control" <<EOF +Package: ${binary} +Source: ${OB_SOURCE} +Version: ${version} +Architecture: ${arch} +EOF + + if ${gen_rel}; then + for name in Essential Depends Recommends Suggests Pre-Depends \ + Conflicts Provides Replaces; do + value="$(ob_get_binary_parameter "${binary}" "${name}")" + if [ -n "${value}" ]; then + printf '%s: %s\n' "${name}" "${value}" \ + >>"${binary}.control/control" + fi + done + fi + + cat >>"${binary}.control/control" <<EOF +Installed-Size: ${inst_size} +Description:$(echo "${desc}" | sed 's/^/ /') +EOF + + homepage="$(ob_get_source_parameter 'Homepage')" + if [ -n "${homepage}" ]; then + printf '%s: %s\n' 'Homepage' "${homepage}" \ + >>"${binary}.control/control" + fi +} + +install_maintainer_scripts() +{ + binary="${1}" + + for script in preinst postinst prerm postrm; do + if [ -L "../${binary}.pkg/${script}" ]; then + target="$(ls -l "../${binary}.pkg/${script}")" + target="${target#* -> }" + ln -s "${target}" "${binary}.control/${script}" + elif [ -r "../${binary}.pkg/${script}" ]; then + cp "../${binary}.pkg/${script}" "${binary}.control/${script}" + chmod 755 "${binary}.control/${script}" + fi + done +} + +gen_conffiles() +{ + binary="${1}" + + if [ -d "${binary}.data/etc" ]; then + find "${binary}.data/etc" -type f | sed "s@^${binary}.data@@" \ + >"${binary}.control/conffiles" + if [ -z "$(head -n 1 "${binary}.control/conffiles")" ]; then + rm -f "${binary}.control/conffiles" + else + chmod 644 "${binary}.control/conffiles" + fi + fi +} + +gen_md5sums() +{ + binary="${1}" + + find "${binary}.data" -type f | sort | xargs md5sum | \ + sed "s@ ${binary}.data@ @" \ + >"${binary}.control/md5sums" + if [ -z "$(head -n 1 "${binary}.control/md5sums")" ]; then + rm -f "${binary}.control/md5sums" + else + chmod 644 "${binary}.control/md5sums" + fi +} + +main "${@}" |