# Copyright 2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=7

inherit bash-completion-r1

DESCRIPTION="basez - base 16/32/64 encode/decode data to standard output"
HOMEPAGE="http://www.quarkline.net/basez/"
SRC_URI="http://www.quarkline.net/basez/download/${P}.tar.gz"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="+man +doc bash-completion"

RESTRICT="strip"

src_unpack() {
	unpack ${A}
}

src_configure() {
	if [[ -x ${ECONF_SOURCE:-.}/configure ]] ; then
		econf --with-bash-completion-dir="$(get_bashcompdir)"
	fi
}

src_compile() {
	if [[ -f Makefile ]] ; then
		emake || die
	fi
}

src_install() {
	local BIN_D=/usr/bin
	local MAN_D=/usr/share/man/man1
	dobin basez

	if use man ; then
		doman "basez.1"
	fi

	if use doc ; then
		dodoc LICENSE TODO
	fi

	if use bash-completion ; then
		local BIN="$BIN_D/basez"
		dosym "$BIN" "$BIN_D/base16"
		dosym "$BIN" "$BIN_D/base32"
		dosym "$BIN" "$BIN_D/base32hex"
		dosym "$BIN" "$BIN_D/base32plain"
		dosym "$BIN" "$BIN_D/base64"
		dosym "$BIN" "$BIN_D/base64mime"
		dosym "$BIN" "$BIN_D/base64pem"
		dosym "$BIN" "$BIN_D/base64plain"
		dosym "$BIN" "$BIN_D/base64url"
		dosym "$BIN" "$BIN_D/hex"
		dosym "$BIN" "$BIN_D/unhex"
		
		emake DESTDIR="${D}" "${PN}-completion"
		newbashcomp "${PN}-completion" "${PN}"
		bashcomp_alias "${PN}" base16
		bashcomp_alias "${PN}" base32
		bashcomp_alias "${PN}" base32hex
		bashcomp_alias "${PN}" base32plain
		bashcomp_alias "${PN}" base64
		bashcomp_alias "${PN}" base64mime
		bashcomp_alias "${PN}" base64pem
		bashcomp_alias "${PN}" base64plain
		bashcomp_alias "${PN}" base64url
		bashcomp_alias "${PN}" hex
		bashcomp_alias "${PN}" unhex
	fi

	if use bash-completion && use man ; then
		local MAN="$MAN_D/basez.1"
		dosym "$MAN" "$MAN_D/base16.1"
		dosym "$MAN" "$MAN_D/base32.1"
		dosym "$MAN" "$MAN_D/base32hex.1"
		dosym "$MAN" "$MAN_D/base32plain.1"
		dosym "$MAN" "$MAN_D/base64.1"
		dosym "$MAN" "$MAN_D/base64mime.1"
		dosym "$MAN" "$MAN_D/base64pem.1"
		dosym "$MAN" "$MAN_D/base64plain.1"
		dosym "$MAN" "$MAN_D/base64url.1"
		dosym "$MAN" "$MAN_D/hex.1"
		dosym "$MAN" "$MAN_D/unhex.1"
	fi
}