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

EAPI=7

inherit rpm

DL_UUID="a6ae8171-6d05-4d46-82db-395524b10875"

MY_PN="epson-inkjet-printer-escpr2"
MY_PV="${PV}-1"

DESCRIPTION="Epson ESC/P-R 2 generic Inkjet Printer Driver binary package"
HOMEPAGE="https://support.epson.net/linux/Printer/LSB_distribution_pages/en/escpr2.php"
# Go to the Epson Download Center https://download-center.epson.com/search/?language=en
# in order to get the binary package. Select any supported printer, e.g. "XP-970", select
# "Linux RPM(x64)", any country, and under "Drivers" click "Proceed to download".
SRC_URI="https://download-center.epson.com/f/module/${DL_UUID}/${MY_PN}-${MY_PV}.x86_64.rpm"
LICENSE="EPSON-EULA LGPL-2.1"
SLOT="0"
KEYWORDS="~amd64"
IUSE=""

RESTRICT="fetch"
RDEPEND="net-print/cups
         !net-print/epson-inkjet-printer-escpr2
         !net-print/epson-inkjet-printer-escpr:2"
DEPEND="${RDEPEND}"

S="${WORKDIR}/opt/${MY_PN}"

pkg_nofetch() {
    local UA_STRING="Mozilla/5.0 (KHTML, like Gecko)"
    einfo
    einfo "Epson restricted download by blocking User Agent strings like the one wget uses."
    einfo "This means you have to fetch the source file manually."
    einfo
    einfo "Please either visit the Epson Download Center to get the source file using your Internet browser:"
    einfo "  1. Visit https://download-center.epson.com/search/?language=en"
    einfo "  2. Search for your or any other supported printer, e.g. \"XP-970\"."
    einfo "  3. Select \"Linux RPM(x64)\" and any country."
    einfo "  4. Click \"Search\", a new page with the results will be loaded."
    einfo "  5. Under \"Drivers\" there should be \"Epson Inkjet Printer Driver 2 (ESC/P-R) for Linux\"."
    einfo "     Simply click \"Proceed to download\" on the right."
    einfo
    einfo "Alternatively you may specify a browser User Agent string for the download:"
    einfo "    wget --user-agent=\"${UA_STRING}\" \\"
    einfo "    ${SRC_URI}"
    einfo
    einfo "Place the source file into your DISTDIR directory."
    einfo
}

src_unpack() {
    rpm_src_unpack ${MY_PN}-${MY_PV}.x86_64.rpm
    cd ${S}
}

src_install() {
# libraries
    dolib.so lib64/libescpr2.so.1.0.0 lib64/libescpr2.so.1.0.0 lib64/libescpr2.so.1.0.0
# cups executables
    exeinto /opt/${MY_PN}/cups/lib/filter
    doexe cups/lib/filter/epson-escpr-wrapper2 cups/lib/filter/epson-escpr2
# docs
    dodoc doc/*
# ppds
    insinto /usr/share/ppd/${MY_PN}/
    doins ppds/Epson/*
}

pkg_postinst() {
        elog "The online user manual is available at:"
        elog "    https://download.ebz.epson.net/man/linux/escpr2.html"
}