# Unpack native or upstream source # # Copyright (C) 2012 Patrick McDermott # # This file is part of opkbuild. # # opkbuild 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. # # opkbuild 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 opkbuild. If not, see . set -eu upstream_ar_base= upstream_ar= upstream_ar_z= upstream_ar_dir= unpack_native() { ob_info "$(ob_get_msg 'unpacking_native')" if ! cp -Rp '../src' 'src'; then ob_error "$(ob_get_msg 'cant_unpack_native')" return 1 fi return 0 } get_upstream_archive() { upstream_ar_base="${OPK_SOURCE}-${OPK_SOURCE_VERSION_UPSTREAM}.orig.tar" upstream_ar="$(printf '%s\n' "../${upstream_ar_base}"*)" case "${upstream_ar}" in "../${upstream_ar_base}*") ob_info "$(ob_get_msg 'no_sources')" return 1 ;; *"${OB_LF}"*) ob_error "$(ob_get_msg 'multiple_upstream_sources')" return 1 ;; *) return 0 ;; esac } get_upstream_compression() { local z_ext= z_ext="${upstream_ar#../${upstream_ar_base}}" case "${z_ext}" in '') upstream_ar_z='' ;; '.gz') upstream_ar_z='z' ;; '.bz2') upstream_ar_z='j' ;; '.lz') upstream_ar_z='a' ;; '.xz') upstream_ar_z='J' ;; '.Z') upstream_ar_z='Z' ;; *) ob_error "$(ob_get_msg 'unsupported_archive_compression')" \ "${z_ext}" return 1 ;; esac return 0 } get_upstream_dir() { local dir_count= upstream_ar_dir="$(tar "-t${upstream_ar_z}f" "${upstream_ar}" | \ sed -n 's|^\(\./\)\{0,1\}\([^/]\{1,\}\).*$|\2|p' | sort | uniq)" dir_count="$(printf '%s\n' "${upstream_ar_dir}" | wc -l)" case "${dir_count}" in 0) ob_error "$(ob_get_msg 'no_upstream_dirs')" return 1 ;; 1) return 0 ;; *) ob_error "$(ob_get_msg 'multiple_top_upstream_dirs')" return 1 esac } extract_upstream() { ob_info "$(ob_get_msg 'unpacking_upstream')" if ! tar "-x${upstream_ar_z}f" "${upstream_ar}"; then ob_error "$(ob_get_msg 'cant_unpack_upstream')" return 1 fi if ! mv -- "${upstream_ar_dir}" 'src'; then ob_error "$(ob_get_msg 'cant_move_native')" return 1 fi return 0 } unpack_upstream() { get_upstream_compression || return 1 get_upstream_dir || return 1 extract_upstream || return 1 return 0 } main() { if ! ob_set_text_domain 'opkbuild'; then printf '%s: Error: Failed to load locale messages\n' \ "${0##*/}" >&2 return 1 fi ob_init_package '..' || return 1 if [ -d 'src' ]; then ob_info "$(ob_get_msg 'already_unpacked')" else if [ -d '../src' ]; then unpack_native || return 1 elif get_upstream_archive; then unpack_upstream || return 1 fi fi return 0 }