pkgname=bzip2
pkgver=1.0.8
pkgdesc="A high-quality data compression program"
license=('BSD')
homepage="https://sourceware.org/bzip2/"
sources=("bzip2-1.0.8.tar.gz"
         "bzip2-1.0.8-install_docs-1.patch"
)
urls=("https://www.sourceware.org/pub/bzip2/bzip2-1.0.8.tar.gz"
      "https://www.linuxfromscratch.org/patches/lfs/12.2/bzip2-1.0.8-install_docs-1.patch"
)
md5sums=("67e051268d0c475ea773822f7500d0e5"
         "6a5ac7e89b791aae556de0f745916f7f"
)


src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  patch -Np1 -i "${distdir}/${sources[1]}"
}

src_build() {
  sed -i 's@\(ln -s -f \)$(PREFIX)/bin/@\1@' Makefile
  sed -i "s@(PREFIX)/man@(PREFIX)/share/man@g" Makefile

  make -f Makefile-libbz2_so CC="gcc $CFLAGS $CPPFLAGS $LDFLAGS"
  make clean
  make CC="gcc $CFLAGS $CPPFLAGS $LDFLAGS"
}

src_check() {
  make check
}

src_install() {
  make PREFIX="${pkgdir}/usr" install
  cp -av libbz2.so.* "${pkgdir}/usr/lib"
  ln -sv libbz2.so.1.0.8 "${pkgdir}/usr/lib/libbz2.so"
  cp -v bzip2-shared "${pkgdir}/usr/bin/bzip2"
  for i in usr/bin/{bzcat,bunzip2}; do
    ln -sfv bzip2 "${pkgdir}/$i"
  done
  rm -fv "${pkgdir}/usr/lib/libbz2.a"
}
