mod gitea;
mod packaging;

use std::{env, error::Error, path::Path};

fn main() -> Result<(), Box<dyn Error>> {
    let args = env::args().skip(1).collect::<Vec<_>>();
    match args.as_slice() {
        [command] if command == "prepare" => gitea::prepare(),
        [command] if command == "publish" => gitea::publish(),
        [command, directory] if command == "macos-linker-alias" => {
            packaging::prepare_macos_linker_alias(Path::new(directory))
        }
        [command, directory] if command == "macos-x64-runtime" => {
            packaging::prepare_macos_x64_runtime(Path::new(directory))
        }
        [command, directory, sdk_libraries] if command == "windows-runtime" => {
            packaging::prepare_windows_runtime(Path::new(directory), Path::new(sdk_libraries))
        }
        [command, paths @ ..] if command == "upload" && !paths.is_empty() => {
            gitea::upload(paths.iter().map(Path::new))
        }
        [command, tag, release_dir, rust_lib_dir, platform, dist_dir]
            if command == "package" =>
        {
            packaging::package(
                tag,
                Path::new(release_dir),
                Path::new(rust_lib_dir),
                platform,
                Path::new(dist_dir),
            )?;
            Ok(())
        }
        [command, tag, release_dir, rust_lib_dir, platform, dist_dir, upload]
            if command == "package" && upload == "--upload" =>
        {
            let assets = packaging::package(
                tag,
                Path::new(release_dir),
                Path::new(rust_lib_dir),
                platform,
                Path::new(dist_dir),
            )?;
            gitea::upload(assets.iter().map(|path| path.as_path()))
        }
        _ => Err(
            "usage: bds-release prepare|publish|macos-linker-alias <directory>|macos-x64-runtime <directory>|windows-runtime <directory> <sdk-libraries>|upload <asset>...|package <tag> <release-dir> <rust-lib-dir> <platform> <dist-dir> [--upload]"
                .into(),
        ),
    }
}
