// Copyright (C) 2022-2023 Enrico Guiraud
//
// This file is part of Kobold, a static site generator with a friendly CLI.
//
// Kobold is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Kobold is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kobold. If not, see <http://www.gnu.org/licenses/>.

mod build;
mod cli;
mod init;
mod log;
mod utils;
use clap::Parser;

use cli::Commands::{Build, Init};
use log::Lvl;

fn main() {
    human_panic::setup_panic!();

    let cli_cmd = cli::Cli::parse().command;

    let ret = match cli_cmd {
        Init { dir } => init::init(&dir).map(|_| {
            log::log(
                Lvl::Info,
                &format!(
                    "Kobold website successfully initialized at `{}`!",
                    dir.display()
                ),
            );
        }),
        Build {
            dir,
            no_tags,
            with_search_bar,
        } => build::build(&dir, no_tags, with_search_bar),
    };

    if let Err(msg) = ret {
        log::log(Lvl::Error, &msg);
        std::process::exit(exitcode::USAGE);
    } else {
        std::process::exit(exitcode::OK);
    }
}
