Documentation
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use crate::error::{CliError, Result};
use std::env;
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use std::process::Command;

#[cfg(target_os = "linux")]
use std::process::Stdio;

#[cfg(target_os = "linux")]
pub fn install() -> Result<()> {
    // Linux installation
    sudo_install_binary()?;
    create_mime_type()?;
    update_mailcap()?;
    add_shell_function("$HOME/.bashrc")?;
    Ok(())
}

#[cfg(target_os = "macos")]
pub fn install() -> Result<()> {
    // macOS installation
    sudo_install_binary()?;
    setup_macos_file_association()?;
    add_shell_function_macos("$HOME/.zshrc")?;
    Ok(())
}

#[cfg(target_os = "windows")]
pub fn install() -> Result<()> {
    // Windows installation
    install_binary_windows()?;
    create_registry_file()?;
    add_to_windows_path()?;
    add_shell_function("$HOME/.profile")?;
    Ok(())
}

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
pub fn install() -> Result<()> {
    Err(CliError::Input(
        "Installation is not supported on this platform".to_string(),
    ))
}

fn sudo_install_binary() -> Result<()> {
    let binary_path = env::current_exe()?;
    Command::new("sudo")
        .args(["install", "-m", "755"])
        .arg(&binary_path)
        .arg("/usr/local/bin/glyph")
        .status()
        .map_err(CliError::Io)?;
    Ok(())
}

#[cfg(target_os = "linux")]
fn create_mime_type() -> Result<()> {
    let mime_content = r#"<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/x-glyph">
    <comment>Glyph Animation File</comment>
    <glob pattern="*.glyph"/>
    <magic priority="50">
      <match type="string" offset="0" value="GLYF"/>
    </magic>
  </mime-type>
</mime-info>"#;

    // Write to temp file first
    fs::write("/tmp/application-x-glyph.xml", mime_content)?;

    // Move to final location with sudo
    Command::new("sudo")
        .args([
            "mv",
            "/tmp/application-x-glyph.xml",
            "/usr/share/mime/packages/",
        ])
        .status()?;

    // Update mime database
    Command::new("sudo")
        .args(["update-mime-database", "/usr/share/mime"])
        .status()?;

    Ok(())
}

#[cfg(target_os = "linux")]
fn update_mailcap() -> Result<()> {
    // Check if entry already exists
    if !Command::new("grep")
        .args(["-q", "application/x-glyph", "/etc/mailcap"])
        .status()?
        .success()
    {
        let entry = "application/x-glyph; glyph %s; copiousoutput\n";
        let mut child = Command::new("sudo")
            .args(["tee", "-a", "/etc/mailcap"])
            .stdout(Stdio::null())
            .stdin(Stdio::piped())
            .spawn()?;

        if let Some(mut stdin) = child.stdin.take() {
            stdin.write_all(entry.as_bytes())?;
        }

        child.wait()?;
    }
    Ok(())
}

#[cfg(target_os = "macos")]
fn setup_macos_file_association() -> Result<()> {
    // First check if the handler already exists
    let output = Command::new("defaults")
        .args(["read", "com.apple.LaunchServices", "LSHandlers"])
        .output()?;

    if !String::from_utf8_lossy(&output.stdout).contains("public.glyph") {
        Command::new("defaults")
            .args([
                "write",
                "com.apple.LaunchServices",
                "LSHandlers",
                "-array-add",
                r#"{'LSHandlerContentType'='public.glyph';'LSHandlerRoleAll'='com.yourcompany.glyph';}"#,
            ])
            .status()?;
    }

    Ok(())
}

#[cfg(target_os = "macos")]
fn add_shell_function_macos(rc_file: &str) -> Result<()> {
    let rc_path = PathBuf::from(rc_file.replace("$HOME", &env::var("HOME")?));

    if !rc_path.exists() {
        fs::write(&rc_path, "")?;
        // Set initial permissions to be writable by owner only (644 in octal)
        Command::new("chmod")
            .args(["0644", rc_path.to_string_lossy().as_ref()])
            .status()?;
    }

    // Read current permissions
    let metadata = fs::metadata(&rc_path)?;
    let mode = metadata.permissions().mode();

    // Temporarily make the file writable by owner
    Command::new("chmod")
        .args(["0644", rc_path.to_string_lossy().as_ref()])
        .status()?;

    // Add the shell function
    let result = add_shell_function(&rc_path.to_string_lossy());

    // Restore original permissions
    Command::new("chmod")
        .args([
            &format!("0{:o}", mode & 0o777),
            rc_path.to_string_lossy().as_ref(),
        ])
        .status()?;

    // Fix zsh completion directory permissions
    fix_zsh_completion_permissions()?;

    result
}

#[cfg(target_os = "macos")]
fn fix_zsh_completion_permissions() -> Result<()> {
    let home = env::var("HOME")?;
    let zsh_completion_dir = PathBuf::from(&home).join(".zsh");

    // Create .zsh directory if it doesn't exist with proper permissions
    if !zsh_completion_dir.exists() {
        fs::create_dir_all(&zsh_completion_dir)?;
        Command::new("chmod")
            .args(["0755", zsh_completion_dir.to_string_lossy().as_ref()])
            .status()?;
    }

    // Fix permissions for all files in the completion directory
    if zsh_completion_dir.exists() {
        Command::new("chmod")
            .args(["-R", "go-w", zsh_completion_dir.to_string_lossy().as_ref()])
            .status()?;
    }

    // Also fix the parent directory permissions
    Command::new("chmod")
        .args(["0755", home.as_str()])
        .status()?;

    Ok(())
}

#[cfg(target_os = "windows")]
fn install_binary_windows() -> Result<()> {
    let home = env::var("HOME")?;
    let bin_dir = PathBuf::from(&home).join("bin");
    fs::create_dir_all(&bin_dir)?;

    let current_exe = env::current_exe()?;
    fs::copy(current_exe, bin_dir.join("glyph.exe"))?;

    Ok(())
}

#[cfg(target_os = "windows")]
fn create_registry_file() -> Result<()> {
    let home = env::var("HOME")?;
    let reg_content = format!(
        r#"Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.glyph]
@="GlyphFile"

[HKEY_CLASSES_ROOT\GlyphFile\shell\open\command]
@="\"{}/bin/glyph.exe\" \"%1\""
"#,
        home.replace('\\', "\\\\")
    );

    fs::write("glyph-association.reg", reg_content)?;
    println!("Please run glyph-association.reg to associate .glyph files");

    Ok(())
}

#[cfg(target_os = "windows")]
fn add_to_windows_path() -> Result<()> {
    let home = env::var("HOME")?;
    let profile_path = PathBuf::from(&home).join(".profile");

    let path_entry = format!("export PATH=$PATH:{}/bin\n", home);

    let profile_content = fs::read_to_string(&profile_path).unwrap_or_default();
    if !profile_content.contains(&path_entry) {
        fs::write(&profile_path, profile_content + &path_entry)?;
    }

    Ok(())
}

fn add_shell_function(rc_file: &str) -> Result<()> {
    let shell_function = r#"
function cat() {
    for arg in "$@"; do
        if [[ "${arg}" == *.glyph ]]; then
            glyph "${arg}"
        else
            command cat "${arg}"
        fi
    done
}
"#;

    let rc_path = PathBuf::from(rc_file.replace("$HOME", &env::var("HOME")?));
    let rc_content = fs::read_to_string(&rc_path).unwrap_or_default();

    if !rc_content.contains("function cat()") {
        fs::write(&rc_path, rc_content + shell_function)?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::{tempdir, TempDir};

    // Helper function to create a mock shell rc file
    fn create_mock_rc(dir: &TempDir, content: &str) -> PathBuf {
        let rc_path = dir.path().join(".bashrc");
        fs::write(&rc_path, content).unwrap();
        rc_path
    }

    // Helper to create mock system directories
    #[cfg(all(test, target_os = "linux"))]
    fn setup_mock_system(dir: &TempDir) -> PathBuf {
        let usr_local_bin = dir.path().join("usr").join("local").join("bin");
        let mime_dir = dir
            .path()
            .join("usr")
            .join("share")
            .join("mime")
            .join("packages");
        fs::create_dir_all(&usr_local_bin).unwrap();
        fs::create_dir_all(&mime_dir).unwrap();
        dir.path().to_path_buf()
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_linux_shell_function_installation() {
        let temp = tempdir().unwrap();
        let rc_content = "# Existing bashrc content\nexport PATH=$HOME/bin:$PATH\n";
        let rc_path = create_mock_rc(&temp, rc_content);

        add_shell_function(&rc_path.to_string_lossy()).unwrap();

        let new_content = fs::read_to_string(&rc_path).unwrap();
        assert!(new_content.contains("function cat()"));
        assert!(new_content.contains("glyph"));
        assert!(new_content.contains("command cat"));
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_linux_shell_function_idempotent() {
        let temp = tempdir().unwrap();
        let rc_content = "# Existing bashrc content\nfunction cat() { echo 'test'; }\n";
        let rc_path = create_mock_rc(&temp, rc_content);

        add_shell_function(&rc_path.to_string_lossy()).unwrap();

        let new_content = fs::read_to_string(&rc_path).unwrap();
        let function_count = new_content.matches("function cat()").count();
        assert_eq!(function_count, 1, "Should not add duplicate cat functions");
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_linux_mime_type_creation() {
        let temp = tempdir().unwrap();
        let root = setup_mock_system(&temp);
        let mime_file = root
            .join("usr")
            .join("share")
            .join("mime")
            .join("packages")
            .join("application-x-glyph.xml");

        // Mock the create_mime_type function to use our test directory
        let result = fs::write(&mime_file, "test content");
        assert!(result.is_ok());

        let content = fs::read_to_string(&mime_file).unwrap();
        assert!(content.len() > 0);
    }

    #[test]
    #[cfg(target_os = "macos")]
    fn test_macos_shell_function_installation() {
        let temp = tempdir().unwrap();
        let rc_content = "# Existing zshrc content\n";
        let rc_path = create_mock_rc(&temp, rc_content);

        add_shell_function_macos(&rc_path.to_string_lossy()).unwrap();

        let new_content = fs::read_to_string(&rc_path).unwrap();
        assert!(new_content.contains("function cat()"));
    }

    #[test]
    #[cfg(target_os = "windows")]
    fn test_windows_binary_installation() {
        let temp = tempdir().unwrap();
        let bin_dir = temp.path().join("bin");
        fs::create_dir_all(&bin_dir).unwrap();

        // Create a mock executable
        let mock_exe = bin_dir.join("glyph.exe");
        fs::write(&mock_exe, "mock binary content").unwrap();

        assert!(mock_exe.exists());
    }

    #[test]
    #[cfg(target_os = "windows")]
    fn test_windows_registry_file_creation() {
        let temp = tempdir().unwrap();
        let reg_file = temp.path().join("glyph-association.reg");

        // Create registry file
        fs::write(&reg_file, "Windows Registry Editor Version 5.00").unwrap();

        assert!(reg_file.exists());
        let content = fs::read_to_string(&reg_file).unwrap();
        assert!(content.contains("Windows Registry Editor"));
    }

    #[test]
    fn test_shell_function_with_empty_rc() {
        let temp = tempdir().unwrap();
        let rc_path = temp.path().join(".bashrc");

        add_shell_function(&rc_path.to_string_lossy()).unwrap();

        assert!(rc_path.exists());
        let content = fs::read_to_string(&rc_path).unwrap();
        assert!(content.contains("function cat()"));
    }

    #[test]
    fn test_shell_function_with_nonexistent_rc() {
        let temp = tempdir().unwrap();
        let rc_path = temp.path().join("nonexistent.rc");

        let result = add_shell_function(&rc_path.to_string_lossy());
        assert!(result.is_ok());
    }
}