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
use crate::util::error::STError;
use crate::util::log::log;

use std::fs::File;
use std::io::Write;
use std::{
    env, fs, io,
    path::{Path, PathBuf},
};

fn touch(path: &Path) -> io::Result<()> {
    match fs::OpenOptions::new().create(true).write(true).open(path) {
        Ok(_) => Ok(()),
        Err(e) => Err(e),
    }
}

fn mkdir(dir: String) -> Result<PathBuf, STError> {
    let dir = shellexpand::full(&dir)?.to_string();
    let dir = Path::new(&dir);

    fs::create_dir_all(dir)?;
    Ok(dir.to_path_buf())
}

pub fn cache_directory() -> Result<PathBuf, STError> {
    let dir = match env::var("STEAM_TUI_CACHE_DIR") {
        Ok(dir) => dir,
        _ => "~/.cache/steam-tui".to_string(),
    };
    mkdir(dir)
}

pub fn config_directory() -> Result<PathBuf, STError> {
    let dir = match env::var("STEAM_TUI_DIR") {
        Ok(dir) => dir,
        _ => "~/.config/steam-tui".to_string(),
    };
    mkdir(dir)
}

pub fn script_directory() -> Result<PathBuf, STError> {
    let dir = match env::var("STEAM_TUI_SCRIPT_DIR") {
        Ok(dir) => dir,
        _ => format!("{}/scripts", cache_directory()?.as_path().display()),
    };
    mkdir(dir)
}

pub fn icon_directory() -> Result<PathBuf, STError> {
    let dir = match env::var("STEAM_TUI_ICON_DIR") {
        Ok(dir) => dir,
        _ => format!("{}/icons", cache_directory()?.as_path().display()),
    };
    mkdir(dir)
}

pub fn steam_directory() -> Result<PathBuf, STError> {
    let dir = match env::var("STEAM_APP_DIR") {
        Ok(dir) => dir,
        _ => "~/.steam/steam/steamapps/common/".to_string(),
    };
    mkdir(dir)
}

pub fn steam_run_wrapper(id: i32) -> Result<PathBuf, STError> {
    // Custom script always takes precedence, then env, then hardcoded path.
    let custom_script = script_directory()?.join(&format!("{}.sh", id));
    if custom_script.exists() {
        return Ok(custom_script);
    }
    let run = match env::var("STEAM_RUN_WRAPPER") {
        Ok(run) => run,
        _ => "~/.steam/bin32/steam-runtime/run.sh".to_string(),
    };
    let run = shellexpand::full(&run)?.to_string();
    let run = Path::new(&run);

    if run.exists() {
        Ok(run.to_path_buf())
    } else {
        Err(STError::Problem(format!(
            "Run wrapper doesn't exist: {:?}",
            run
        )))
    }
}

pub fn executable_join(executable: &str, installdir: &str) -> Result<PathBuf, STError> {
    let installdir = Path::new(installdir);
    let executable = Path::new(executable);
    let script_path = installdir.join(executable);
    Ok(script_path)
}

pub fn icon_exists(id: i32) -> Result<PathBuf, STError> {
    let dir = icon_directory()?;
    let icon = &format!("{}.ico", id);
    let icon = Path::new(icon);
    let icon = dir.join(icon);
    if icon.exists() {
        Ok(icon)
    } else {
        Err(STError::Problem(format!("Icon doesn't exist: {:?}", icon)))
    }
}

pub fn icon_save(id: i32, icon: &[u8]) -> Result<(), STError> {
    let dir = icon_directory()?;
    let icon_path = &format!("{}.ico", id);
    let icon_path = Path::new(icon_path);
    let icon_path = dir.join(icon_path);
    let mut file = File::create(icon_path)?;
    file.write_all(icon)?;
    Ok(())
}

pub fn executable_exists(executable: &str) -> Result<PathBuf, STError> {
    let dir = steam_directory()?;
    let executable = Path::new(executable);
    let script_path = dir.join(executable);
    log!(script_path);
    if script_path.exists() {
        Ok(script_path)
    } else {
        Err(STError::Problem("Executable doesn't exist".to_string()))
    }
}

fn script_location(file: &Path, contents: &str) -> Result<PathBuf, STError> {
    let dir = script_directory()?;
    let script_path = dir.join(file);
    let mut f = fs::File::create(&script_path)?;
    f.write_all(contents.as_bytes())?;
    Ok(script_path)
}

pub fn install_script_location(login: String, id: i32) -> Result<PathBuf, STError> {
    let file = &format!("{}.install", id);
    let file = Path::new(file);
    let contents = format!(
        r#"
login {}
app_update "{}" -validate
quit
"#,
        login, id
    );
    script_location(file, &contents)
}

pub fn launch_script_location(login: String, id: i32) -> Result<PathBuf, STError> {
    let file = &format!("{}.launch", id);
    let file = Path::new(file);
    let contents = format!(
        r#"
login {}
app_update "{}" -validate
app_run {}
quit
"#,
        login, id, id
    );
    script_location(file, &contents)
}

pub fn config_location() -> Result<PathBuf, STError> {
    let dir = config_directory()?;
    let config_path = Path::new("config.json");
    let config_path = dir.join(config_path);
    touch(&config_path)?;
    Ok(config_path)
}

pub fn cache_location() -> Result<PathBuf, STError> {
    let dir = cache_directory()?;
    let cache_path = Path::new("games.json");
    let cache_path = dir.join(cache_path);
    touch(&cache_path)?;
    Ok(cache_path)
}

pub fn invalidate_cache() -> Result<(), STError> {
    fs::remove_file(cache_location()?)?;
    Ok(())
}