i2pd-launch 0.0.1

Deletes current state and launches i2pd
/*
Delete state and launch i2pd installed on Windows using scoop

This is free and unencumbered software released into the public domain.
SPDX-License-Identifier: Unlicense OR CC0-1.0
*/

use windows_sys::Win32::UI::Shell::FOLDERID_ProgramData;
use windows_sys::Win32::UI::Shell::FOLDERID_Profile;
use windows_sys::core::GUID;
use std::path::{Path, PathBuf};
use std::process::ExitCode;

/** start i2pd */

#[cfg(not(target_os = "windows"))] 
fn main() -> ExitCode { 
   println!("This program is supported only on Windows.");
   ExitCode::From(3)
}

fn main() -> ExitCode {
    let global_i2p = dirs_sys::known_folder(FOLDERID_ProgramData).map_or(false, |f| detect_scoop_i2p(&f));
    let user_i2p = dirs_sys::known_folder(FOLDERID_Profile).map_or(false, |f| detect_scoop_i2p(&f));
    if ( global_i2p || user_i2p ) == false {
       println!("no scoop i2pd install detected");
       ExitCode::from(1)
    } else {
       println!("scoop i2pd detected: global {} user {}", global_i2p, user_i2p);
       let workdir = { if user_i2p { get_scoop_i2p_dir( FOLDERID_Profile ) } 
                       else { get_scoop_i2p_dir( FOLDERID_ProgramData ) }
       };
       // Change to the working directory
       std::env::set_current_dir(&workdir).expect("Failed to change directory to scoop i2pd installation");
       // Delete files listed in TO_DELETE, ignoring errors
       for file in TO_DELETE.iter() { 
          let file_path = workdir.join(file);
          let _ = std::fs::remove_file(&file_path);
       } 
       // Launch I2PD_EXE without arguments
       println!("starting {}", I2PD_EXE);
       std::process::Command::new(I2PD_EXE).spawn().expect("Failed to launch i2pd");
       ExitCode::SUCCESS
    }
}

fn get_scoop_i2p_dir(folder: GUID) -> PathBuf {
   let mut f = dirs_sys::known_folder(folder).unwrap();
   f.push(SCOOP_I2P_INSTALL);
   f
}

fn detect_scoop_i2p(search: impl AsRef<Path>) -> bool {
   #[cfg(debug_assertions)]
   println!("Testing {}", search.as_ref().display());
   let mut p = PathBuf::from(search.as_ref());
   if p.is_dir() {
      p.push(SCOOP_I2P_INSTALL);
      #[cfg(debug_assertions)]
      println!("Testing2 {}", p.display());
      p.is_dir()
   } else {
      false
   }
}

const TO_DELETE: [&str; 5] = [ "i2pd.log", "ntcp2.keys", "ssu2.keys", "router.info", "router.keys" ];
const SCOOP_I2P_INSTALL: &str = "scoop\\apps\\i2pd\\current";
const I2PD_EXE: &str = "i2pd.exe";