# Contributor: John Coyle <dx9err@gmail.com>
# Maintainer: John Coyle <dx9err@gmail.com>

: ${WITH_TESTS:=OFF}

pkgname=ceph
pkgver=11.0.1
pkgrel=5
pkgdesc="User space components of the Ceph file system"
pkgusers="ceph"
pkggroups="ceph"
url="http://ceph.com"
arch="x86_64"
license="LGPL-2.1 and CC-BY-SA-1.0 and GPL-2.0 and BSL-1.0 and GPL-2.0-with-autoconf-exception and BSD-3-Clause and MIT"
depends="ceph-osd ceph-mds ceph-mon"

makedepends="
	acl-dev
	argp-standalone
	boost-dev
	btrfs-progs
	bzip2-dev
	cmake
	coreutils
	cryptsetup
	curl-dev
	cython
	cython-dev
	diffutils
	eudev-dev
	expat-dev
	fcgi-dev
	findutils
	flex
	fuse
	fuse-dev
	git
	jq
	keyutils-dev
	leveldb-dev
	libaio-dev
	libatomic_ops-dev
	libedit-dev
	libtirpc-dev
	libtool
	libxml2-dev
	linux-headers
	lvm2-dev
	nss-dev
	openldap-dev
	libressl-dev
	parted
	procps
	python-dev
	py2-pip
	py-nose
	py-sphinx
	py-virtualenv
	readline-dev
	rpcgen
	snappy-dev
	userspace-rcu-dev
	xfsprogs-dev
	xmlstarlet
	yasm
"

#source="$pkgname-$pkgver.tar.gz::https://github.com/ceph/ceph/archive/v$pkgver.tar.gz"
source="https://github.com/dx9/ceph/releases/download/11.0.1/ceph-$pkgver-beta1.tar.bz2
	make-sure-PATH_MAX-is-defined.patch"

subpackages="
	$pkgname-base
	$pkgname-common
	$pkgname-mds
	$pkgname-mon
	$pkgname-fuse:ceph_fuse
	$pkgname-radosgw
	$pkgname-osd
	$pkgname-doc
	$pkgname-bash-completion:bash_completion
	rbd-fuse:rbd_fuse
	rbd-mirror:rbd_mirror
	rbd-nbd:rbd_nbd
	librbd
	librbd-dev:librbd_dev
	libcephfs
	libcephfs-dev:libcephfs_dev
	librados
	librados-dev:librados_dev
	librgw
	librgw-dev:librgw_dev
	py-rados:py_rados
	libradosstriper
	libradosstriper-dev:libradosstriper_dev
	py-rbd:py_rbd
	py-cephfs:py_cephfs
"

if [ "$WITH_TESTS" = ON ]; then
	subpackages="$subpackages $pkgname-test:ceph_test"
fi

_prefix=/usr
_bindir=$_prefix/bin
_datadir=$_prefix/share
_docdir=$_datadir/doc
_includedir=$_prefix/include
_libdir=$_prefix/lib
_libexecdir=$_prefix/libexec
_localstatedir=/var
_mandir=$_datadir/man
_sbindir=/usr/sbin
_sysconfdir=/etc

_udevrulesdir=/etc/udev/rules.d
_python_sitelib=/usr/lib/python2.7/site-packages


_builddir="$srcdir"/$pkgname-$pkgver

build() {
   cd "$_builddir"
   mkdir build
   cd build
   cmake .. \
	   -DCMAKE_INSTALL_PREFIX=$_prefix \
	   -DCMAKE_INSTALL_LIBDIR=$_libdir \
	   -DCMAKE_INSTALL_LIBEXECDIR=$_libexecdir \
	   -DCMAKE_INSTALL_LOCALSTATEDIR=$_localstatedir \
	   -DCMAKE_INSTALL_SYSCONFDIR=$_sysconfdir \
	   -DCMAKE_INSTALL_DOCDIR=$_docdir/ceph \
	   -DCMAKE_INSTALL_MANDIR=$_mandir \
	   -DWITH_REENTRANT_STRSIGNAL=ON \
	   -DWITH_THREAD_SAFE_RES_QUERY=ON \
	   -DWITH_MANPAGE=OFF \
	   -DWITH_PYTHON3=OFF \
	   -DWITH_TESTS=$WITH_TESTS \
	   || return 1
   make
}

package() {
	cd "$_builddir"
	cd build
	make DESTDIR="$pkgdir" install
	cd ..

	rm -f "$pkgdir"$_sysconfdir/init.d/ceph

	install -D src/etc-rbdmap "$pkgdir"$_sysconfdir/ceph/rbdmap

	install -m 0644 -D src/logrotate.conf "$pkgdir"$_sysconfdir/logrotate.d/ceph

	chmod 0644 "$pkgdir"$_docdir/ceph/sample.ceph.conf
	chmod 0644 "$pkgdir"$_docdir/ceph/sample.fetch_config

	# udev rules
	install -m 0644 -D udev/50-rbd.rules "$pkgdir"$_udevrulesdir/50-rbd.rules
	install -m 0644 -D udev/60-ceph-by-parttypeuuid.rules  \
		"$pkgdir"$_udevrulesdir/60-ceph-by-parttypeuuid.rules
	install -m 0644 -D udev/95-ceph-osd.rules \
		"$pkgdir"$_udevrulesdir/95-ceph-osd.rules
}

base() {
	pkgdesc="Base is the package that includes all the files shared amongst ceph servers"
	depends="
		ceph-common
		cryptsetup
		findutils
		librbd
		librados
		libcephfs
		librgw
		logrotate
		py-requests
		py-setuptools
		util-linux
		xfsprogs
	"

	mkdir -p "$subpkgdir"$_docdir/ceph
	mv "$pkgdir"$_docdir/ceph/sample.ceph.conf \
		"$subpkgdir"$_docdir/ceph/sample.ceph.conf
	mv "$pkgdir"$_docdir/ceph/sample.fetch_config \
		"$subpkgdir"$_docdir/ceph/sample.fetch_config

	mkdir -p "$subpkgdir"$_bindir
	for file in crushtool monmaptool osdmaptool ceph-run ceph-detect-init; do
		mv "$pkgdir"$_bindir/$file "$subpkgdir"$_bindir
	done

	mkdir -p "$subpkgdir"$_sbindir
	mv "$pkgdir"$_sbindir/ceph-create-keys "$subpkgdir"$_sbindir/ceph-create-keys
	mv "$pkgdir"$_sbindir/mount.ceph "$subpkgdir"$_sbindir/mount.ceph

	mkdir -p "$subpkgdir"$_libexecdir/ceph
	mv "$pkgdir"$_libexecdir/ceph/ceph_common.sh "$subpkgdir"$_libexecdir/ceph

	mkdir -p "$subpkgdir"$_libdir/rados-classes
	mv "$pkgdir"$_libdir/rados-classes/* "$subpkgdir"$_libdir/rados-classes
	mkdir -p "$subpkgdir"$_libdir/ceph/erasure-code
	mv "$pkgdir"$_libdir/ceph/erasure-code/libec_*.so* \
		"$subpkgdir"$_libdir/ceph/erasure-code
	mkdir -p "$subpkgdir"$_libdir/ceph/compressor
	mv "$pkgdir"$_libdir/ceph/compressor/libceph_*.so* \
		"$subpkgdir"$_libdir/ceph/compressor

	mkdir -p "$subpkgdir"$_sysconfdir/logrotate.d
	mv "$pkgdir"$_sysconfdir/logrotate.d/ceph \
		"$subpkgdir"$_sysconfdir/logrotate.d/ceph

	mkdir -p "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/ceph_detect_init* "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/ceph_disk* "$subpkgdir"$_python_sitelib

	for dir in tmp bootstrap-osd bootstrap-mds bootstrap-rgw; do
		install -m 750 -o ceph -g ceph -d \
			"$subpkgdir"$_localstatedir/lib/ceph/$dir
	done
}

common() {
	pkgdesc="Common utilities to mount and interact with a ceph storage cluster."
	depends="py-rados py-rbd py-cephfs"
	install="$pkgname-common.pre-install"

	mkdir -p "$subpkgdir"$_bindir
	for file in \
		ceph \
		ceph-authtool \
		ceph-conf \
		ceph-dencoder \
		ceph-rbdnamer \
		ceph-syn \
		ceph-crush-location \
		cephfs-data-scan \
		cephfs-journal-tool \
		cephfs-table-tool \
		rados \
		rbd \
		rbd-replay \
		rbd-replay-many \
		rbdmap \
		ceph-post-file \
		ceph-brag \
		; do \
		mv "$pkgdir"$_bindir/$file "$subpkgdir"$_bindir
	done

	mkdir -p "$subpkgdir"$_datadir/ceph
	mv "$pkgdir"$_datadir/ceph/known_hosts_drop.ceph.com "$subpkgdir"$_datadir/ceph
	mv "$pkgdir"$_datadir/ceph/id_rsa_drop.ceph.com "$subpkgdir"$_datadir/ceph
	mv "$pkgdir"$_datadir/ceph/id_rsa_drop.ceph.com.pub "$subpkgdir"$_datadir/ceph

	mkdir -p "$subpkgdir"$_sysconfdir/ceph
	mv "$pkgdir"$_sysconfdir/ceph/rbdmap "$subpkgdir"$_sysconfdir/ceph

	mkdir -p "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/ceph_argparse.py* "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/ceph_daemon.py* "$subpkgdir"$_python_sitelib

	mkdir -p "$subpkgdir"$_udevrulesdir
	mv "$pkgdir"$_udevrulesdir/50-rbd.rules "$subpkgdir"$_udevrulesdir

	install -m 3770 -o ceph -g ceph -d "$subpkgdir"$_localstatedir/log/ceph
	install -m 750 -o ceph -g ceph -d "$subpkgdir"$_localstatedir/lib/ceph
}

mds() {
	pkgdesc="Metadata server daemon for the Ceph distributed file system."
	depends="ceph-base"

	mkdir -p "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/ceph-mds "$subpkgdir"$_bindir

	install -m 750 -o ceph -g ceph -d "$subpkgdir"$_localstatedir/lib/ceph/mds
}

mon() {
	pkgdesc="Cluster monitor daemon for the Ceph distributed file system."
	depends="ceph-base"

	mkdir -p "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/ceph-mon "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/ceph-rest-api "$subpkgdir"$_bindir

	mkdir -p "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/ceph_rest_api.py* "$subpkgdir"$_python_sitelib

	install -m 750 -o ceph -g ceph -d "$subpkgdir"$_localstatedir/lib/ceph/mon
}

ceph_fuse() {
	pkgdesc="FUSE based client for Ceph distributed network file system."

	mkdir -p "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/ceph-fuse "$subpkgdir"$_bindirn
	mkdir -p "$subpkgdir"$_sbindir
	mv "$pkgdir"$_sbindir/mount.fuse.ceph "$subpkgdir"$_sbindir
}

rbd_fuse() {
	pkgdesc="FUSE based client to map Ceph rbd images to files."
	depends="librados librbd"
	mkdir -p "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/rbd-fuse "$subpkgdir"$_bindir
}

rbd_mirror() {
	pkgdesc="Daemon for mirroring RBD images between Ceph clusters."
	depends="ceph-common librados"
	mkdir -p "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/rbd-mirror "$subpkgdir"$_bindir
}

rbd_nbd() {
	pkgdesc="NBD based client to map Ceph rbd images to local device."
	depends="librbd librados"
	mkdir -p "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/rbd-nbd "$subpkgdir"$_bindir
}

radosgw() {
	pkgdesc="Rados REST gateway which implements Amazon's S3 and OpenStack's Swift APIs."
	depends="ceph-common"
	mkdir -p "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/radosgw "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/radosgw-admin "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/radosgw-token "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/radosgw-object-expirer "$subpkgdir"$_bindir
	mkdir -p "$subpkgdir"$_localstatedir/lib/ceph/radosgw
}

osd() {
	pkgdesc="Object storage daemon for the Ceph distributed file system."
	depends="ceph-base parted gptfdisk hdparm"

	mkdir -p "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/ceph-clsinfo "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/ceph-bluefs-tool "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/ceph-objectstore-tool "$subpkgdir"$_bindir
	mv "$pkgdir"$_bindir/ceph-osd "$subpkgdir"$_bindir

	mkdir -p "$subpkgdir"$_sbindir
	mv "$pkgdir"$_sbindir/ceph-disk "$subpkgdir"$_sbindir
	mv "$pkgdir"$_sbindir/ceph-disk-udev "$subpkgdir"$_sbindir

	mkdir -p "$subpkgdir"$_libexecdir/ceph
	mv "$pkgdir"$_libexecdir/ceph/ceph-osd-prestart.sh "$subpkgdir"$_libexecdir/ceph

	mkdir -p "$subpkgdir"$_udevrulesdir
	mv "$pkgdir"$_udevrulesdir/60-ceph-by-parttypeuuid.rules "$subpkgdir"$_udevrulesdir
	mv "$pkgdir"$_udevrulesdir/95-ceph-osd.rules "$subpkgdir"$_udevrulesdir

	install -m 750 -o ceph -g ceph -d "$subpkgdir"$_localstatedir/lib/ceph/osd
}

librados() {
	pkgdesc="RADOS distributed object store client library"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/librados.so.* "$subpkgdir"/usr/lib
}

librados_dev() {
	pkgdesc="RADOS distributed object store client library headers"
	depends="librados"
	mkdir -p "$subpkgdir"$_includedir/rados
	mv "$pkgdir"$_includedir/rados/librados.h "$subpkgdir"$_includedir
	mv "$pkgdir"$_includedir/rados/librados.hpp "$subpkgdir"$_includedir
	mv "$pkgdir"$_includedir/rados/buffer.h "$subpkgdir"$_includedir
	mv "$pkgdir"$_includedir/rados/buffer_fwd.h "$subpkgdir"$_includedir
	mv "$pkgdir"$_includedir/rados/page.h "$subpkgdir"$_includedir
	mv "$pkgdir"$_includedir/rados/crc32c.h "$subpkgdir"$_includedir
	mv "$pkgdir"$_includedir/rados/rados_types.h "$subpkgdir"$_includedir
	mv "$pkgdir"$_includedir/rados/rados_types.hpp "$subpkgdir"$_includedir
	mv "$pkgdir"$_includedir/rados/memory.h "$subpkgdir"$_includedir
	mv "$pkgdir"$_libdir/librados.so "$subpkgdir"$_libdir
	mv "$pkgdir"$_bindir/librados-config "$subpkgdir"$_bindir
}

librgw() {
	pkgdesc="RADOS gateway client library"
	depends="librados"
	mkdir -p "$subpkgdir"$_libdir
	mv "$pkgdir"$_libdir/librgw.so.* "$subpkgdir"$_libdir
}

librgw_dev() {
	pkgdesc="RADOS gateway client library headers"
	depends="librados"

	mkdir -p "$subpkgdir"$_includedir/rados
	mv "$pkgdir"$_includedir/rados/librgw.h "$subpkgdir"$_includedir/rados
	mv "$pkgdir"$_includedir/rados/rgw_file.h "$subpkgdir"$_includedir/rados
	mv "$pkgdir"$_libdir/librgw.so "$subpkgdir"$_libdir
}

py_rados() {
	pkgdesc="Python libraries for the RADOS object store"
	depends="librados"
	mkdir -p "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/rados.so "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/rados-*.egg-info "$subpkgdir"$_python_sitelib
}

libradosstriper() {
	pkgdesc="RADOS striping library"
	depends="librados"
	mkdir -p "$subpkgdir"$_libdir
	mv "$pkgdir"$_libdir/libradosstriper.so.* "$subpkgdir"$_libdir
}

libradosstriper_dev() {
	pkgdesc="RADOS striping library headers"
	depends="libradosstriper librados-dev"
	mkdir -p "$subpkgdir"$_includedir/radosstriper
	mv "$pkgdir"$_includedir/radosstriper "$subpkgdir"$_includedir/radosstriper
	mv "$pkgdir"$_libdir/libradosstriper.so "$subpkgdir"$_libdir
}

librbd() {
	pkgdesc="RADOS block device client library"
	depends="librados"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/librbd.so.* "$subpkgdir"/usr/lib

	# TODO: look into this
	#mkdir -p /usr/lib64/qemu/
	#ln -sf %{_libdir}/librbd.so.1 /usr/lib64/qemu/librbd.so.1
}

librbd_dev() {
	pkgdesc="RADOS block device client library headers"
	depends="librbd librados-dev"
	mkdir -p "$subpkgdir"/usr/include/rbd
	mv "$pkgdir"/usr/include/rbd "$subpkgdir"/usr/include
	mv "$pkgdir"/usr/lib/librbd.so "$subpkgdir"/usr/lib
}

py_rbd() {
	pkgdesc="Python libraries for the RADOS block device"
	depends="librbd py-rados"
	mkdir -p "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/rbd.so "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/rbd-*.egg-info "$subpkgdir"$_python_sitelib
}

libcephfs() {
	pkgdesc="Ceph distributed file system client library"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libcephfs.so.* "$subpkgdir"/usr/lib
}

libcephfs_dev() {
	pkgdesc="Ceph distributed file system client library headers"
	depends="libcephfs librados-devel"
	mkdir -p "$subpkgdir"/usr/include/cephfs
	mv "$pkgdir"/usr/include/cephfs "$subpkgdir"/usr/include
	mv "$pkgdir"/usr/lib/libcephfs.so "$subpkgdir"/usr/lib
}

py_cephfs() {
	pkgdesc="Python libraries for Ceph distributed file system"
	depends="libcephfs py-rados"
	mkdir -p "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/cephfs.so "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/cephfs-*.egg-info "$subpkgdir"$_python_sitelib
	mv "$pkgdir"$_python_sitelib/ceph_volume_client.py* "$subpkgdir"$_python_sitelib
}

ceph_test() {
	depends="ceph-common"
	mkdir -p "$subpkgdir"$_bindir
	for file in \
		ceph_bench_log \
		ceph_kvstorebench \
		ceph_multi_stress_watch \
		ceph_erasure_code \
		ceph_erasure_code_benchmark \
		ceph_omapbench \
		ceph_objectstore_bench \
		ceph_perf_objectstore \
		ceph_perf_local \
		ceph_perf_msgr_client \
		ceph_perf_msgr_server \
		ceph_psim \
		ceph_radosacl \
		ceph_rgw_jsonparser \
		ceph_rgw_multiparser \
		ceph_scratchtool \
		ceph_scratchtoolpp \
		ceph_smalliobench \
		ceph_smalliobenchdumb \
		ceph_smalliobenchfs \
		ceph_smalliobenchrbd \
		ceph_test_* \
		ceph_tpbench \
		ceph_xattr_bench \
		ceph-coverage \
		ceph-monstore-tool \
		ceph-osdomap-tool \
		ceph-kvstore-tool \
		ceph-debugpack \
		ceph-client-debug \
		; do \
		mv "$pkgdir"$_bindir/$file "$subpkgdir"$_bindir
	done

	mkdir -p "$subpkgdir"$_libdir
	mv "$pkgdir"$_libdir/ceph/ceph-monstore-update-crush.sh "$subpkgdir"$_libdir
}

bash_completion() {
	depends="bash-completion"
	pkgdesc="Bash completions for Ceph"
	mkdir -p "$subpkgdir"$_sysconfdir/bash_completion.d
	mv "$pkgdir"$_sysconfdir/bash_completion.d/* "$subpkgdir"$_sysconfdir/bash_completion.d
}
sha512sums="d8bc28a25b9444134527f22856a6f0ea97b21a6100b0576a092d8988da0f191aca1e59bd14129361aee53f3032295169117ff02207e2ff361bb526461b9c5462  ceph-11.0.1-beta1.tar.bz2
a1843f78a2fa68d1a77e3b3c90581d5a96fb378267216584b2668fb4143ddaa4cd5c9d66ff2135aff44d488bca7a0cad23a8b3d782bd01342e515e3ec7c6403d  make-sure-PATH_MAX-is-defined.patch"
