# Maintainer: George Rawlinson <george@rawlinson.net.nz>

pkgname=rutorrent
pkgver=4.2.1
pkgrel=1
pkgdesc='Yet another web front-end for rTorrent'
arch=('any')
url='https://github.com/Novik/ruTorrent'
license=('GPL')
depends=(
  'php-fpm'
  'php-geoip'
  'curl'
  'gzip'
  'coreutils'
)
makedepends=('git' 'perl')
backup=( 
  'etc/rutorrent/config.php'
  'etc/rutorrent/plugins.ini'
  'etc/rutorrent/access.ini'
)
options=('emptydirs' '!strip')
_commit='32db41ddbaaa2c91d595162c40e08b6eab4284ef'
source=(
  "$pkgname::git+$url#commit=$_commit"
  'tmpfiles.conf'
  'config.patch'
)
b2sums=('SKIP'
        'f762fe34e415632f4a458f83ab305a7c5fe231c04ed3cce7b3a7ab99e00aa4a767bab8212edcff80b954d974cf347919b36052dcea5caf0504fb79b745456349'
        'd3f6a9ce2553462673b34549ef0e77666ea53e07c1625dbaebc927a5b8ddc43733876bc7a49e3502ff97657d105f14e402c5f4a53340afeabef7755334a41be0')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed -e 's/^v//' -e 's/-hotfix$//'
}

prepare() {
	cd "$pkgname"

	# move config & log location
  patch -p1 -i "$srcdir/config.patch"

	# move config/settings to /var/lib/rutorrent
	rm -rf share

	# delete git files
	find . -type f -name ".git*" -delete

	# delete POSIX-incompatible files
	cd plugins/tracklabels/labels
	find . -print0 | perl -MFile::Path=remove_tree -n0e 'chomp; remove_tree($_, {verbose=>1}) if /[[:^ascii:][:cntrl:]]/'
}

package() {
	cd "$pkgname"

	# create directories
	install -vd "$pkgdir/usr/share/$pkgname/conf" "$pkgdir/etc/$pkgname"

	# copy files over
	cp -r . "$pkgdir/usr/share/$pkgname"

	# link config files to /etc/$pkgname
	for conf in config.php plugins.ini access.ini; do
		mv "conf/$conf" "$pkgdir/etc/$pkgname"
		ln -sf "/etc/$pkgname/$conf" "$pkgdir/usr/share/$pkgname/conf/$conf"
	done

	# license & documentation
	install -Dm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE.md
	install -Dm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md htaccess-example

	# remove unnecessary files
	rm -rf "$pkgdir/usr/share/$pkgname/"{LICENSE.md,README.md,htaccess-example,.git,.github,tests}

	# systemd integration
	install -Dm644 "$srcdir/tmpfiles.conf" "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"

	# change ownership
	chown -R http:http "$pkgdir/usr/share/$pkgname"
}