use scallop::builtins::ExecStatus;

use crate::pkgsh::get_build_mut;
use crate::pkgsh::phase::PhaseKind::SrcInstall;
use crate::pkgsh::utils::get_libdir;

use super::{emake::run as emake, make_builtin, Scopes::Phase};

const LONG_DOC: &str = "Run `emake install` for a package.";

#[doc = stringify!(LONG_DOC)]
pub(crate) fn run(args: &[&str]) -> scallop::Result<ExecStatus> {
    let destdir = get_build_mut().destdir();
    let paths: &[&str] = &[
        &format!("prefix={destdir}/usr"),
        &format!("datadir={destdir}/usr/share"),
        &format!("mandir={destdir}/usr/share/man"),
        &format!("infodir={destdir}/usr/share/info"),
        // Note that the additional complexity for determining libdir described in PMS is
        // ignored in favor of using the more modern and simple value from get_libdir().
        &format!("libdir={destdir}/usr/{}", get_libdir(Some("lib")).unwrap()),
        &format!("localstatedir={destdir}/var/lib"),
        &format!("sysconfdir={destdir}/etc"),
    ];

    emake(&[paths, &["-j1"], args, &["install"]].concat())
}

const USAGE: &str = "einstall";
make_builtin!(
    "einstall",
    einstall_builtin,
    run,
    LONG_DOC,
    USAGE,
    &[("0..6", &[Phase(SrcInstall)])]
);

#[cfg(test)]
mod tests {
    use super::super::builtin_scope_tests;
    use super::*;

    builtin_scope_tests!(USAGE);

    // TODO: add usage tests
}
