use rusqlite::Connection; use std::{sync::RwLock, path::PathBuf, process::Command}; pub static PROGRAM_HOME_PATH: RwLock = RwLock::new(String::new()); pub fn init() { let program_home_path = if cfg!(target_os = "windows") { let binding = std::env::var("APP_DATA").expect("No APP_DATA directory"); let mut appdata = PathBuf::from(&binding); appdata = appdata.join("learn-hieroglyph"); Command::new("mkdir").arg(&appdata).spawn().expect(&format!("Cannot create program's home: {}", &appdata.to_str().unwrap())).wait().unwrap(); appdata } else { let binding = std::env::var("HOME").expect("No HOME directory"); let mut home = PathBuf::from(&binding); home = home.join(".config/learn-hieroglyph"); Command::new("mkdir").args(["-p", &home.to_str().unwrap()]).spawn().expect(&format!("Cannot create program's home: {}", &home.to_str().unwrap())).wait().unwrap(); home }; let mut p = PROGRAM_HOME_PATH.write().unwrap(); *p = program_home_path.as_path().to_str().unwrap().to_string(); let db_path = program_home_path.join("db.sqlite"); let connection = Connection::open(db_path).unwrap(); connection.execute("CREATE TABLE IF NOT EXISTS cards ( id INTEGER PRIMARY KEY AUTOINCREMENT, image_hash CHAR(64), hieroglyph VARCHAR(2), translation VARCHAR(128) )",()).unwrap(); } pub fn get_program_home_path() -> &'static RwLock { &PROGRAM_HOME_PATH }