lumer 0.1.0

Lumer is a tool for managing Lumi projects
use dirs::{self, home_dir};
use lumer::run;
use miette::{miette, IntoDiagnostic, Result};
use std::env;
use std::fs::create_dir_all;
use std::fs::{copy, read_dir};
use std::io;
use std::path::Path;
use std::process::ExitCode;

fn ensure_stdlib() -> Result<(), io::Error> {
    let home_dir =
        home_dir().ok_or(io::Error::new(io::ErrorKind::NotFound, "no home directory"))?;

    let stdlib_dir = home_dir
        .join(".local")
        .join("lib")
        .join("lumi")
        .join("lumi-std");

    if stdlib_dir.exists() {
        return Ok(());
    }

    let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
    let source_stdlib = manifest_dir.join("lumi-std");

    if !source_stdlib.exists() {
        eprintln!(
            "Warning: lumi-std not found at {}, skipping",
            source_stdlib.display()
        );
        return Ok(());
    }

    create_dir_all(&stdlib_dir)
        .into_diagnostic()
        .expect("create stdlib dir");

    for entry in read_dir(&source_stdlib)? {
        let entry = entry?;
        let src = entry.path();
        let dest = stdlib_dir.join(
            src.file_name()
                .ok_or_else(|| miette!("invalid file name in lumi-std"))
                .expect("file name"),
        );

        if src.is_file() {
            copy(&src, &dest).into_diagnostic().expect("copy file");
        }
    }

    println!("✅ Installed lumi-std to {}", stdlib_dir.display());
    Ok(())
}

#[tokio::main]
async fn main() -> Result<ExitCode> {
    ensure_stdlib().expect("ensure_stdlib");
    run().await
}