use std::error::Error;
use std::io::{self, BufReader, BufWriter, Read, Write};
use std::path::PathBuf;
use factorio_blueprint::{BlueprintCodec, Container};
use structopt::StructOpt;
use factorio_bitpacker::pack_read;
#[derive(Debug, StructOpt)]
#[structopt(verbatim_doc_comment)]
#[structopt(after_help(r#"Copyright © 2021 "micycle".
Licensed GPLv3: GPL version 3 <https://gnu.org/licenses/gpl.html>.
This program is free software: you are free to change and redistribute it under
the terms of the License. There is NO WARRANTY, to the extent permitted by law."#))]
struct Opt {
#[structopt(parse(from_os_str))]
input: Option<PathBuf>,
#[structopt(parse(from_os_str))]
output: Option<PathBuf>,
}
fn main() -> Result<(), Box<dyn Error>> {
let opt = Opt::from_args();
let input: Box<dyn Read> = match opt.input {
Some(path) => Box::new(BufReader::new(std::fs::File::open(path)?)),
None => Box::new(BufReader::new(io::stdin())),
};
let output: Box<dyn Write> = match opt.output {
Some(path) => Box::new(BufWriter::new(std::fs::File::open(path)?)),
None => Box::new(BufWriter::new(io::stdout())),
};
let bp = Container::Blueprint(pack_read(input)?);
BlueprintCodec::encode(output, &bp)?;
Ok(())
}