use crate::{diff, fs::util::*};
use std::fs;
use std::io;
use std::path::PathBuf;

pub fn init_repo(
    repo_dir: &PathBuf,
    data_dir: &PathBuf,
    ignore_file: &Option<PathBuf>,
) -> Result<(), io::Error> {
    let dirs = setup_dirs(repo_dir, Some(data_dir));
    fs::create_dir(dirs.repo_dir.clone())?;
    init_lock(repo_dir)?;

    fs::create_dir(dirs.repo_base.clone())?;
    fs::create_dir(dirs.patches_path)?;

    if let Some(file) = ignore_file {
        fs::copy(file, dirs.ignore_path.clone())?;
    }

    let data_paths = get_walk(dirs.data_dir.clone().unwrap(), dirs.ignore_path.clone())?;

    copy_dir_all(
        &dirs.data_dir.unwrap(),
        &data_paths,
        &dirs.repo_base,
        false,
        false,
    )?;

    let hashes = get_hashes(data_paths)?;
    diff::hash::write_hashes(dirs.hash_path, hashes)?;

    Ok(())
}
