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

pkgname=p7zip
pkgver=16.02
pkgrel=1
pkgdesc='Command-line version of the 7zip compressed file archiver'
url='https://p7zip.sourceforge.io/'
license=('GPL' 'custom')
arch=('i686' 'x86_64')
depends=('gcc-libs' 'bash')
makedepends=('nasm')
groups=('compression')
options=('emptydirs')
source=("https://downloads.sourceforge.net/project/${pkgname}/${pkgname}/${pkgver}/${pkgname}_${pkgver}_src_all.tar.bz2"
        01_makefile_9.20.1.patch
        02_man_9.20.1.patch
        03_respect-umask-partial_9.20.1.patch
        p7zip-9.20.1-1.src.patch)
sha256sums=('5eb20ac0e2944f6cb9c2d51dd6c4518941c185347d4089ea89087ffdd6e2341f'
            'a8471d8c3ccc534820259b1dd23fbbb8adc0e99c6e015e415da0f696bd74837b'
            '7283923fecd2c9b5ea23c65017fbd529e6f59bd945466f3576be88333444a6dd'
            'd57c05631e84abd924fb13afc617673106373f53c274711e06ca7ff12982c11c'
            '2980374c6cc9369f73a96b2681e5e63f9f8b9e359ddaf6e09483db6cebe57b4f')

options=('!makeflags')

prepare() {
  cd "${srcdir}/${pkgname}_${pkgver}"
  
  patch -p1 -i ${srcdir}/01_makefile_9.20.1.patch
  patch -p1 -i ${srcdir}/02_man_9.20.1.patch
  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.cygwin makefile.machine \
    || cp makefile.cygwin_asm makefile.machine

  rm GUI/kde4/p7zip_compress.desktop # FS#43766
}

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}"
  
  #local sedexp1="s,^DEST_DIR=.*\$,DEST_DIR=${D},";
  #local sedexp2="s,^EXEEXT=.*\$,EXEEXT=.exe,";

  #mv -f install.sh install.sh.orig
  #cat install.sh.orig | sed -e "$sedexp1" -e "$sedexp2" > ./install.sh
  #chmod +x install.sh
  #./install.sh
  
  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
  
  #cp contrib/gzip-like_CLI_wrapper_for_7z/README ${B}/p7zip-script.README
  #dodoc p7zip-script.README

  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"
}
