use std::env;
use std::path::PathBuf;
use clap::Args;
use color_eyre::eyre::Result;
use fluent_templates::Loader;
use crate::{LANG_ID, LOCALES, utils};
#[must_use]
#[derive(Args)]
#[command(arg_required_else_help = true,
about = LOCALES.lookup(&LANG_ID, "zip_command"))]
pub struct Zip {
#[arg(help = LOCALES.lookup(&LANG_ID, "epub_dir_path"))]
pub epub_dir_path: PathBuf,
#[arg(short, long, default_value_t = false,
help = LOCALES.lookup(&LANG_ID, "delete"))]
pub delete: bool,
}
pub fn execute(config: Zip) -> Result<()> {
utils::ensure_epub_dir(&config.epub_dir_path)?;
let epub_file_path = env::current_dir()?
.join(config.epub_dir_path.file_stem().unwrap())
.with_extension("epub");
if epub_file_path.try_exists()? {
tracing::warn!("The epub output file already exists and will be deleted");
utils::remove_file_or_dir(&epub_file_path)?;
}
novel_api::zip_dir(&config.epub_dir_path, &epub_file_path)?;
if config.delete {
utils::remove_file_or_dir(&config.epub_dir_path)?;
}
Ok(())
}