# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=p7zip
pkgver=17.06
pkgrel=1
pkgdesc='Command-line version of the 7zip compressed file archiver'
url="https://github.com/p7zip-project/p7zip"
msys2_references=(
  "cpe: cpe:/a:7-zip:p7zip"
)
license=('LGPL' 'custom:unRAR')
arch=('i686' 'x86_64')
depends=('gcc-libs' 'bash')
makedepends=('nasm' 'gcc' 'make')
groups=('compression')
options=('emptydirs' '!makeflags')
source=("https://github.com/p7zip-project/p7zip/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz"
        03_respect-umask-partial_9.20.1.patch
        p7zip-9.20.1-1.src.patch)
sha256sums=('c35640020e8f044b425d9c18e1808ff9206dc7caf77c9720f57eb0849d714cd1'
            'd57c05631e84abd924fb13afc617673106373f53c274711e06ca7ff12982c11c'
            '2980374c6cc9369f73a96b2681e5e63f9f8b9e359ddaf6e09483db6cebe57b4f')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  patch -p1 -i ${srcdir}/03_respect-umask-partial_9.20.1.patch
  patch -p1 -i ${srcdir}/p7zip-9.20.1-1.src.patch

  [[ $CARCH = x86_64 ]] \
    && cp makefile.cygwin64_asm makefile.machine \
    || cp makefile.cygwin_asm makefile.machine
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make all3 OPTFLAGS="${CXXFLAGS}"
}

check() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make test
  make test_7z
  make test_7zr
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make install \
    DEST_DIR="${pkgdir}" \
    DEST_HOME="/usr" \
    DEST_MAN="/usr/share/man"

  # Licenses
  #install -d "${pkgdir}"/usr/share/licenses/p7zip
  #ln -s -t "${pkgdir}"/usr/share/licenses/p7zip \
  #  /usr/share/doc/p7zip/DOCS/License.txt \
  #  /usr/share/doc/p7zip/DOCS/unRarLicense.txt

  # Remove documentation for the GUI file manager
  rm -r "${pkgdir}/usr/share/doc/p7zip/DOC/MANUAL/fm"

  install -Dm755 contrib/gzip-like_CLI_wrapper_for_7z/p7zip ${pkgdir}/usr/bin/p7zip
  install -Dm644 contrib/gzip-like_CLI_wrapper_for_7z/man1/p7zip.1 ${pkgdir}/usr/share/man/man1/p7zip.1

  chmod -R a+r,u+w,a+X "${pkgdir}/usr"
}
