# Copyright 1999-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=7

CRATES="
aho-corasick-0.7.18
ansi_term-0.12.1
async-trait-0.1.52
atty-0.2.14
autocfg-1.1.0
bindgen-0.58.1
bindgen-0.59.2
bit-set-0.5.2
bit-vec-0.6.3
bitflags-1.3.2
block-buffer-0.9.0
byteorder-1.4.3
cc-1.0.72
cexpr-0.4.0
cexpr-0.6.0
cfg-if-0.1.10
cfg-if-1.0.0
chrono-0.4.19
clang-sys-1.3.1
clap-2.34.0
cpufeatures-0.2.1
crc-2.1.0
crc-catalog-1.1.1
data-encoding-2.3.2
dbus-0.9.5
dbus-tree-0.9.2
devicemapper-sys-0.1.2
digest-0.9.0
either-1.6.1
env_logger-0.8.4
env_logger-0.9.0
errno-0.2.8
errno-dragonfly-0.1.2
fastrand-1.7.0
fnv-1.0.7
futures-0.3.21
futures-channel-0.3.21
futures-core-0.3.21
futures-executor-0.3.21
futures-io-0.3.21
futures-macro-0.3.21
futures-sink-0.3.21
futures-task-0.3.21
futures-util-0.3.21
generic-array-0.14.5
getrandom-0.2.4
glob-0.3.0
hermit-abi-0.1.19
humantime-2.1.0
instant-0.1.12
itertools-0.10.3
itoa-1.0.1
lazy_static-1.4.0
lazycell-1.3.0
libblkid-rs-0.1.1
libblkid-rs-sys-0.1.3
libc-0.2.117
libcryptsetup-rs-0.5.0
libcryptsetup-rs-sys-0.1.6
libdbus-sys-0.2.2
libloading-0.7.3
libmount-0.1.15
libudev-0.2.0
libudev-sys-0.1.4
log-0.4.14
loopdev-0.2.2
matches-0.1.9
memchr-2.4.1
memoffset-0.6.5
minimal-lexical-0.2.1
mio-0.7.14
miow-0.3.7
nix-0.14.1
nix-0.22.3
nix-0.23.1
nom-5.1.2
nom-7.1.0
ntapi-0.3.7
num-integer-0.1.44
num-traits-0.2.14
num_cpus-1.13.1
once_cell-1.9.0
opaque-debug-0.3.0
peeking_take_while-0.1.2
pin-project-lite-0.2.8
pin-utils-0.1.0
pkg-config-0.3.24
ppv-lite86-0.2.16
pretty-hex-0.2.1
proc-macro2-1.0.36
proptest-1.0.0
quick-error-1.2.3
quick-error-2.0.1
quote-1.0.15
rand-0.8.4
rand_chacha-0.3.1
rand_core-0.6.3
rand_hc-0.3.1
rand_xorshift-0.3.0
redox_syscall-0.2.10
regex-1.5.4
regex-syntax-0.6.25
remove_dir_all-0.5.3
rpassword-5.0.1
rustc-hash-1.1.0
rusty-fork-0.3.0
ryu-1.0.9
semver-1.0.5
serde-1.0.136
serde_derive-1.0.136
serde_json-1.0.78
sha2-0.9.9
shlex-1.1.0
signal-hook-registry-1.4.0
slab-0.4.5
strsim-0.8.0
syn-1.0.86
tempfile-3.3.0
termcolor-1.1.2
textwrap-0.11.0
tokio-1.16.1
tokio-macros-1.7.0
typenum-1.15.0
unicode-width-0.1.9
unicode-xid-0.2.2
uuid-0.8.2
vec_map-0.8.2
version_check-0.9.4
void-1.0.2
wait-timeout-0.2.0
wasi-0.10.2+wasi-snapshot-preview1
which-3.1.1
which-4.2.4
winapi-0.3.9
winapi-i686-pc-windows-gnu-0.4.0
winapi-util-0.1.5
winapi-x86_64-pc-windows-gnu-0.4.0
"

inherit cargo systemd udev

DESCRIPTION="Daemon that manages block devices to create filesystems"
HOMEPAGE="https://stratis-storage.github.io/"
SRC_URI="https://github.com/stratis-storage/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz
		$(cargo_crate_uris ${CRATES})"

LICENSE="MPL-2.0 BSD MIT"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="elibc_musl extra dracut systemd"

BDEPEND="
	>=virtual/rust-1.49.0
	sys-devel/clang
"
DEPEND="
	dracut? ( 
		>=sys-kernel/dracut-051
		sys-boot/plymouth
	)
	systemd? ( sys-apps/systemd )
	app-crypt/clevis
	sys-apps/dbus
	virtual/udev
	dev-libs/libpcre
	>=sys-fs/cryptsetup-2.3
	>=sys-apps/util-linux-2.32
"
RDEPEND="
	${DEPEND}
	sys-fs/xfsprogs
	sys-block/thin-provisioning-tools
"

#QA_FLAGS_IGNORED="/usr/libexec/stratisd "

RESTRICT="network-sandbox"

REQUIRED_USE="dracut? ( systemd )"

PATCHES=(
	# Until feature will be released. https://github.com/stratis-storage/project/issues/161
	${FILESDIR}/0001-disable-xfs-uuid-change-after-snapshot-create.patch
)

src_prepare() {
	sed -i "s@/usr/lib/udev@$(get_udevdir)@g" "${S}"/dracut/90stratis/module-setup.sh
	sed -i "s@/usr/lib/systemd@$(systemd_get_utildir)@g" "${S}"/src/bin/generators/stratis_clevis_setup_generator.rs
	sed -i "s@/usr/lib/systemd@$(systemd_get_utildir)@g" "${S}"/src/bin/generators/stratis_setup_generator.rs
	sed -i "s@/usr/lib/systemd@$(systemd_get_utildir)@g" "${S}"/systemd/stratis-fstab-setup@.service
	
	default
}

src_compile() {
		# Disable offline mode for device-mapper library
		sed -i 's/^offline.*/offline = false/g' ${WORKDIR}/cargo_home/config
		
	    # allow building on musl with dynamic linking support
        # https://github.com/BurntSushi/rust-pcre2/issues/7
        use elibc_musl && export PCRE2_SYS_STATIC=0
        #export RUSTFLAGS="-D warnings -D future-incompatible -D unused -D bare-trait-objects -D ellipsis-inclusive-range-patterns"

		# Compile main daemon
		cargo_src_compile --bin=stratisd --no-default-features --features dbus_enabled,min

		if use dracut;then
			# Compile min binaries
			cargo_src_compile --bin=stratis-min --bin=stratisd-min --bin=stratis-utils --no-default-features --features min,systemd_compat
		fi

		if use extra;then
			cargo_src_compile --bin=stratis_dumpmetadata --no-default-features --features extras,min
		fi
}

src_install() {
	einstalldocs
	dodoc -r docs/.

	insinto /etc/dbus-1/system.d
	doins ${S}/stratisd.conf
	
	udev_dorules ${S}/udev/61-stratisd.rules
	
	dosym "$(get_udevdir)"/stratis-str-cmp "$(get_udevdir)"/stratis-base32-decode
	dosym "$(get_udevdir)"/stratis-str-cmp "${EPREFIX}"/bin/stratis-predict-usage

	exeinto /usr/libexec
	doexe ${S}/target/release/stratisd 
	
	if use dracut; then
		doexe ${S}/target/release/stratisd-min

		dobin  ${S}/target/release/stratis-min

		exeinto "$(get_udevdir)"
		newexe ${S}/target/release/stratis-utils stratis-str-cmp

		insinto /usr/lib/dracut/modules.d/
		doins -r ${S}/dracut/90stratis
		doins -r ${S}/dracut/90stratis-clevis
		chmod +x /usr/lib/dracut/modules.d/90stratis/stratis-rootfs-setup
		
		insinto /usr/lib/dracut/dracut.conf.d
		doins ${S}/dracut/90-stratis.conf
		
		dosym "$(get_udevdir)"/stratis-str-cmp "$(systemd_get_systemgeneratordir)"/stratis-clevis-setup-generator
		dosym "$(get_udevdir)"/stratis-str-cmp "$(systemd_get_systemgeneratordir)"/stratis-setup-generator
	fi
	
	if use systemd; then
		systemd_dounit "${S}/systemd/stratisd.service"
		systemd_dounit "${S}/systemd/stratis-fstab-setup@.service"
		
		exeinto "$(systemd_get_utildir)"
		doexe "${S}/systemd/stratis-fstab-setup"
	else
		newinitd "${FILESDIR}/init.d.stratisd-r3" stratisd
	fi
	
	if use extra; then
		dobin ${S}/target/release/stratis_dumpmetadata
	fi
}

pkg_postinst() {
	udev_reload
}
