use crate::config::get_config;
use crate::utils::{get_installed_packages, is_installed, run_command};

/// Installs a single built package using the configured `installpkg` or `reinstall` command.
///
/// Uses `reinstall` when `action` is `"reinstall"` or when the package is already installed
/// (same version), otherwise uses `installpkg`.
///
/// # Exits
///
/// Exits with a non-zero code if the install command fails.
pub fn install(prgnam: &str, action: &str, pkg_path: &str) {
    let cfg = get_config();
    let installed = get_installed_packages();

    let cmd = if action == "reinstall" || is_installed(prgnam, &installed) {
        cfg.reinstall.clone()
    } else {
        cfg.installpkg.clone()
    };

    // Install or reinstall package
    let args = vec!["-c".to_string(), format!("{} {}", cmd, pkg_path)];
    run_command("bash", &args);
}
