#Packager: George Vlahavas <vlahavas~at~gmail~dot~com>
#
# imagemagick should be installed to build this

pkgname=calibre
pkgver=0.8.64
pkgrel=2gv
source=("https://downloads.sourceforge.net/project/calibre/$pkgver/calibre-$pkgver.tar.xz" "desktop_integration.patch" "calibre-mount-helper" "noupdate.patch")
docs=("authors" "install" "readme*" "copying" "changelog*" "license*")
url=http://calibre-ebook.com/
options=('noautodotnew')

doinst() {
if [ -x /usr/bin/update-desktop-database ]; then
	/usr/bin/update-desktop-database -q
fi
if [ -x /usr/bin/update-mime-database ]; then
	/usr/bin/update-mime-database usr/share/mime >/dev/null 2>&1
fi
}

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"calibre (the one stop solution to all you ebook needs)"
"Calibre is a free and open source e-book library management"
"application developed by users of e-books for users of e-books. It has"
"a cornucopia of features divided into the following main categories:"
"Library Management, e-book conversion, syncing to e-book reader"
"devices, downloading news from the web and converting it into e-book"
"form, comprehensive e-book viewer, content server for online access to"
"your book collection."
)

build() {
	cd $startdir/src/$pkgname

	patch -p1 < $startdir/src/noupdate.patch || exit 1
	
	sed -i -e "s/ldflags = shlex.split(ldflags)/ldflags = shlex.split(ldflags) + ['-fPIC']/" setup/extensions.py
	# This hasn't been fixed yet
	sed -i -e "s:#!usr:#!/usr:g" src/calibre/ebooks/markdown/extensions/meta.py

	python setup.py build
	python setup.py translations

	patch -p1 < $startdir/src/desktop_integration.patch || exit 1

	# More on desktop integration
	sed -i -e "/self.create_uninstaller()/,/os.rmdir(config_dir)/d" \
		-e "s|self.opts.staging_sharedir, 'man/man1'|self.opts.staging_root, 'usr/man/man1'|" \
		-e "s|manpath, prog+'.1'+__appname__+'.bz2'|manpath, prog+'.1'+'.bz2'|" \
		-e "s|old_udev = '/etc|old_udev = '${pkgdir}/etc|" \
		-e "s/^Name=calibre/Name=Calibre/g" src/calibre/linux.py

	# Fix the environment module location
	sed -i -e "s|(prefix=.*)|(prefix='$startdir/pkg/usr')|g" setup/install.py

	mkdir -p $startdir/pkg/usr/lib${LIBDIRSUFFIX}/python2.7/site-packages

	python setup.py install --prefix=/usr \
		--libdir=/usr/lib${LIBDIRSUFFIX} \
		--root=$startdir/pkg \
		--staging-root=$startdir/pkg/usr \
		--staging-libdir=$startdir/pkg/usr/lib${LIBDIRSUFFIX} \
		--staging-bindir=$startdir/pkg/usr/bin \
		--staging-sharedir=$startdir/pkg/usr/share

	# The bash completion files need to be in /etc
	mv $startdir/pkg/usr/etc $startdir/pkg
	
	# these fonts are already in the liberation-fonts-ttf package
	( 
	 cd $startdir/pkg/usr/share/calibre/fonts/liberation/
	 for FONT in *.ttf ; do
	 	rm $FONT
	 	ln -sf /usr/share/fonts/TTF/$FONT
	 done
	)

	# See http://lwn.net/SubscriberLink/465311/7c299471a5399167/
	rm -rf $startdir/pkg/usr/bin/calibre-mount-helper
	install -m 755 $startdir/src/calibre-mount-helper $startdir/pkg/usr/bin

	# Add a Mi0meType icon for lrf, and add an application icon:
	for i in 16 24 32 48 64 96 128; do
		mkdir -p $startdir/pkg/usr/share/icons/hicolor/${i}x${i}/{apps,mimetypes}
		convert imgsrc/mimetypes/lrf.svg -scale $i \
		  $startdir/pkg/usr/share/icons/hicolor/${i}x${i}/mimetypes/application-x-sony-bbeb.png
		convert resources/images/library.png -resize ${i}x${i} \
		  $startdir/pkg/usr/share/icons/hicolor/${i}x${i}/apps/calibre-gui.png
		convert imgsrc/viewer.svg -scale $i \
		  $startdir/pkg/usr/share/icons/hicolor/${i}x${i}/apps/calibre-viewer.png
 	 done
}
