use crate::errors::{ConfigFileErrors, Errors};
use clap::AppSettings;
use dyn_wall_rs::{
auto_time_setup, check_dir_exists, config::Args, print_schedule, sun_timings, time_track::Time,
wallpaper_listener,
};
use std::fs::canonicalize;
use structopt::StructOpt;
use walkdir::WalkDir;
pub mod config;
pub mod errors;
pub mod time_track;
fn main() {
let clap = Args::clap().setting(AppSettings::DeriveDisplayOrder);
let cli_args = Args::from_clap(&clap.get_matches());
let cli_args_used = !(Args::default() == cli_args);
let mut min_depth = 1;
match Args::mixed(cli_args, cli_args_used) {
Err(e) => {
eprintln!("{}", e);
}
Ok(mut args) => {
if args.lat.is_some() && args.long.is_some() {
min_depth = 2;
}
if let Some(dir) = &args.directory {
let dir = dir.as_str();
let dir_count = WalkDir::new(dir).min_depth(min_depth).into_iter().count();
let dir = canonicalize(dir).expect("Failed to canonicalize");
let dir = dir.to_str().expect("Couldn't convert to string");
if args.times.is_none() {
if 1440 % dir_count != 0 || dir_count == 0 {
eprintln!("{}", Errors::CountCompatError(dir_count));
} else {
let (step_time, mut loop_time) = auto_time_setup(dir);
match step_time {
Err(e) => eprintln!("{}", e),
Ok(step_time) => {
let mut times: Vec<Time> = vec![];
for _ in 1..=dir_count {
times.push(loop_time);
loop_time += step_time;
}
args.times = Some(times);
}
}
}
}
if args.schedule {
if let Err(e) = print_schedule(dir, min_depth, args) {
eprintln!("{}", e);
}
} else if let Err(e) =
wallpaper_listener(String::from(dir), args, min_depth)
{
eprintln!("{}", e);
}
}
}
}
}