1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use naty_common::{AppSettings, Parser, Platform};
use site_icons::{Icon, IconInfo, IconKind};
use std::path::{Path, PathBuf};

const LINUX: &str = "https://github.com/lyonsyonii/naty/releases/latest/download/naty-linux";
const WIN: &str = "https://github.com/lyonsyonii/naty/releases/latest/download/naty-windows.exe";
const MACOS: &str = "https://github.com/lyonsyonii/naty/releases/latest/download/naty-macos";
const ICON: &[u8] = include_bytes!("icon.png");

#[cfg(target_family = "windows")]
pub fn copy_executable(output_dir: &Path, name: &str) -> std::io::Result<u64> {
    let name = format!("{name}.exe");
    std::fs::copy(std::env::current_exe()?, output_dir.join(name))
}

#[cfg(target_family = "unix")]
pub fn copy_executable(output_dir: &Path, name: &str) -> std::io::Result<u64> {
    std::fs::copy(std::env::current_exe()?, output_dir.join(name))
}

async fn download_file(
    url: impl AsRef<str>,
    output_dir: impl AsRef<Path>,
    name: &str,
    msg: impl AsRef<str>,
) -> std::io::Result<()> {
    let output_dir = output_dir.as_ref();
    let msg = msg.as_ref();

    let mut downloader = downloader::Downloader::builder()
        .download_folder(output_dir)
        .build()
        .unwrap();
    let download = downloader::Download::new(url.as_ref()).file_name(Path::new(name));
    let output_file = output_dir.join(name);
    if output_file.exists() {
        std::fs::remove_file(output_file)?;
    }

    println!("{msg}");
    let _result =
        tokio::task::spawn_blocking(move || downloader.download(&[download]).unwrap()).await?;
    //println!("{:?}", result);
    Ok(())
}

async fn download_webpage_icon(
    url: impl AsRef<str>,
    output_dir: impl AsRef<Path>,
) -> std::io::Result<()> {
    let output_dir = output_dir.as_ref().to_owned();
    let url = url.as_ref();

    let mut icons = site_icons::Icons::new();
    icons.load_website(url).await.unwrap_or_else(|e| {
        println!("Error");
        match e.downcast::<reqwest_wasm::Error>() {
            Ok(error) => println!("Extracted error: {error}"),
            Err(not) => {
                println!("Not extracted: {}", not);
            }
        }
    });
    let entries = icons.entries().await;
    println!("Available icons: {:?}", entries);
    // Get icon of higher size with: width == height && !Favicon && !SVG
    let mut best_icon: Option<Icon> = None;
    for icon in entries {
        if let Some(sizes) = icon.info.sizes() {
            let size = sizes.first();
            let (width, height) = (size.width, size.height);
            if width == height && icon.kind != IconKind::SiteFavicon && icon.info != IconInfo::SVG {
                best_icon = Some(icon);
                break;
            }
        }
    }

    if let Some(icon) = best_icon {
        let url = icon.url.as_str().to_owned();

        let output_file = output_dir.join("icon.png");
        if output_file.exists() {
            std::fs::remove_file(output_file)?;
        }

        return tokio::task::spawn_blocking(|| {
            println!("Icon Output directory: {}", output_dir.display());
            download_file(url, output_dir, "icon.png", "Downloading icon...")
        })
        .await
        .unwrap()
        .await;
    }

    Err(std::io::Error::new(
        std::io::ErrorKind::InvalidData,
        "Website does not have a valid icon",
    ))
}

async fn setup_executable(
    target_url: &str,
    name: Option<&str>,
    output_dir: &Path,
    icon: &[u8],
    naty_bin_url: impl AsRef<str>,
    platform: impl AsRef<str>,
) -> std::io::Result<PathBuf> {
    let naty_bin_url = naty_bin_url.as_ref();
    let mut platform = platform.as_ref();
    if platform.is_empty() {
        platform = std::env::consts::OS;
    }

    let url: url::Url = target_url.try_into().unwrap_or_else(|err| {
        println!("Error parsing the url: {err}");
        std::process::exit(1)
    });
    
    let name = naty_common::get_webpage_name(name, &url);
    let out_dir_name = format!("{}-{platform}", &name);
    let output_dir = output_dir.join(&out_dir_name);
    std::fs::create_dir_all(&output_dir).expect("Could not create directory");

    if platform == std::env::consts::OS {
        copy_executable(&output_dir, &name)?;
    } else {
        download_file(
            &naty_bin_url,
            &output_dir,
            &name,
            format!("Downloading {platform} binary from {naty_bin_url}"),
        )
        .await?;
    }
    
    std::fs::write(output_dir.join("icon.png"), icon)?;

    Ok(output_dir)
}

async fn run_async() -> std::io::Result<()> {
    let mut cli: AppSettings = AppSettings::parse();
    
    if cli.platforms.is_empty() {
        cli.platforms.push(std::env::consts::OS.into())
    }
    cli.platforms.dedup();
    
    let icon: std::borrow::Cow<[u8]> = match &cli.icon {
        // Icon is a URL
        Some(url) if download_file(url, &cli.output_dir, "icon.png", "Downloading icon from '{url}'...").await.is_ok() => {
            std::fs::read(cli.output_dir.join("icon.png"))?.into()
        }
        // Icon is a Path
        Some(icon) => {
            std::fs::read(icon).unwrap().into()
        }
        // Icon is extracted from website
        None if download_webpage_icon(&cli.target_url, &cli.output_dir).await.is_ok() => {
            std::fs::read(cli.output_dir.join("icon.png"))?.into()
        }
        // Using fallback icon
        None => {
            println!(
                "Unable to extract an icon from {}, using default one",
                cli.target_url
            );
            ICON.into()
        }
    };
    
    let platforms = cli.platforms.clone();
    for platform in platforms {
        let output_dir = match platform {
            Platform::Linux => {
                cli.command = cli.linux_command.clone();
                setup_executable(&cli.target_url, cli.name.as_deref(), &cli.output_dir, &icon, LINUX,"linux", ).await?
            }
            Platform::Windows => {
                cli.command = cli.windows_command.clone();
                setup_executable(&cli.target_url, cli.name.as_deref(), &cli.output_dir, &icon, WIN, "windows").await?
            }
            Platform::MacOs => {
                cli.command = cli.macos_command.clone();
                setup_executable(&cli.target_url, cli.name.as_deref(), &cli.output_dir, &icon, MACOS, "macos", ).await?
            }
        };

        let settings = toml::to_string_pretty(&cli).unwrap();
        std::fs::write(output_dir.join("naty.toml"), settings).expect("Could not create naty.toml");
        
        println!(
            "Successfully created \"{}\" in {}",
            output_dir.file_name().unwrap().to_string_lossy(),
            output_dir.canonicalize().unwrap().display()
        );
    }

    Ok(())
}

pub fn run() -> std::io::Result<()> {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async { run_async().await.unwrap() });
    Ok(())
}