# Maintainer: AndyRTR <andyrtr@archlinux.org>

# Contributor: Alexander Rødseth <rodseth@gmail.com>
# Contributor: Mateusz Herych <heniekk@gmail.com>
# Contributor: royrocks <royrocks13@gmail.com>

pkgname=libpaper
pkgver=2.2.5
pkgrel=1.1
cross=nf
pkgdesc="Library for handling paper characteristics"
url="https://github.com/rrthomas/libpaper"
# https://github.com/rrthomas/libpaper/pull/37
# libpaper is LGPL-2.1+
# bundled libgnu is LGPL-2.1+, LGPL-2+ and GPL-3+
# paperspecs is Public Domain
# localepaper.c is FSFAP
# paper.c is GPL-3.0-or-later
# paperconf.c is GPL 2.0 only
# localepaper.c is FSFAP (except it is missing the warranty disclaimer... but the intent is clear)
license=(
    LGPL-2.1-or-later
    LicenseRef-Public-Domain
    GPL-3.0-or-later
    LGPL-2.0-or-later
    FSFAP
    MIT
)
ncxfat_makedepends=('glibc')
ncxfab_makedepends=('base-devel')
backup=("$MPKG_BACKUPETCPATH/papersize")
source=(https://github.com/rrthomas/libpaper/releases/download/v$pkgver/libpaper-$pkgver.tar.gz
        localepaper.c)
sha256sums=('7be50974ce0df0c74e7587f10b04272cd53fd675cb6a1273ae1cc5c9cc9cab09'
            '7e49c6ce67fbaea77929ab5849026412d0f91f692a902805c0134a071cccde22')

prepare() {
  cd "libpaper-$pkgver"
  cp ../localepaper.c src/
  #autoreconf -vfi
}

build() {
  cd "libpaper-$pkgver"
  ./configure \
    --prefix=$MPKG_USRPATH \
    --sysconfdir=$MPKG_ETCPATH \
    --sbindir=$MPKG_USRPATH/bin
  make
  # localepaper
  pushd src
  gcc $CFLAGS $LDFLAGS -I.. -Ilibgnu -o localepaper localepaper.c libgnu/.libs/libgnupaper.a
  popd
}

check() {
  cd "libpaper-$pkgver"
  make -k check
}
package() {
  cd "libpaper-$pkgver"
  ncxfat_depends=('glibc')
  provides=('paper')
  replaces=('paper')
  conflicts=('paper')
  make DESTDIR="$pkgdir" install
  
  # localepaper
  install -Dt "$pkgdir$MPKG_USRPATH/lib" -m0755 src/localepaper 
  

  # add systemwide default papersize read by many office applications
  install -dm 755 "$pkgdir"$MPKG_ETCPATH
  echo '# Simply write the paper name. See man 1 paper and "paper --no-size --all" for possible values' > "$pkgdir"$MPKG_ETCPATH/papersize

  # add libpaper.d directory other packages can use to store files
  install -dm 755 "$pkgdir"$MPKG_ETCPATH/libpaper.d

  # https://github.com/rrthomas/libpaper/commit/b4f6846a3a9ae052a515ac0db913e5a68f947adf
  # reintroduced deprecated paperconf binary
#  # add paperconf executable, needed by libreoffice
#  cat <<EOF > "${pkgdir}"/usr/bin/paperconf
#exec paper --no-size "\$@"
#EOF
#  chmod 755 "${pkgdir}"/usr/bin/paperconf

  # currently no localisation available
#  pushd debian/po
#  for i in `ls *.po`; do
#	install -dm 755 "${pkgdir}"/usr/share/locale/${i%.po}/LC_MESSAGES/;
#	msgfmt $i -o "${pkgdir}"/usr/share/locale/${i%.po}/LC_MESSAGES/${pkgname}.mo;
#  done
#  popd

  # license
  install -Dm644 {COPYING,COPYING-GPL-3,COPYING-MIT} -t "${pkgdir}/usr/share/licenses/${pkgname}"
}
