DEVELOPMENT ENVIRONMENT

~liljamo/install-garmin-watch-face

ref: 3732fc05916cfc48f832c6d8cc21536723483e31 install-garmin-watch-face/main.ha -rw-r--r-- 1.9 KiB
3732fc05Jonni Liljamo feat: init 3 days ago
                                                                                
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
use fs;
use getopt;
use log;
use os;
use os::exec;
use path;
use strings;
use temp;

const GARMIN_APPS = "/GARMIN/APPS/";

export fn main() void = {
	const cmd = getopt::parse(os::args,
		"garmin watch face installer",
		('s', "skip MTP mount (testing)"),
		"file",
	);
	defer getopt::finish(&cmd);

	let skip_mount = false;
	for (let opt .. cmd.opts) {
		switch (opt.0) {
		case 's' =>
			skip_mount = true;
		case => abort();
		};
	};

	let file = "";
	if (len(cmd.args) < 1) {
		log::fatal("provide a file");
	} else {
		file = os::resolve(cmd.args[0]);
		if (!os::exists(file)) {
			log::fatal("provided file doesn't exist");
		};
		let parts = strings::split(file, ".")!;
		defer free(parts);
		if (parts[len(parts) - 1] != "prg") {
			log::fatal("file does not seem to be a .prg file");
		};
	};

	const temp_dir = temp::dir();
	defer os::rmdirall(temp_dir)!;
	log::printfln("created temporary directory: {}", temp_dir);

	if (skip_mount) {
		os::mkdirs(strings::concat(temp_dir, GARMIN_APPS)!, fs::mode::USER_RWX)!;
	} else {
		mount_mtp(temp_dir);
	};

	const apps_dir = strings::concat(temp_dir, GARMIN_APPS)!;
	if (!os::exists(apps_dir)) {
		log::fatal("/GARMIN/APPS doesn't exist in mount");
	};

	const file_dest = strings::concat(apps_dir, "face.prg")!;
	log::printfln("moving {} to {}", file, file_dest);
	os::move(file, file_dest)!;

	// Remember to unmount the MTP fs.
	if (!skip_mount) {
		umount_mtp(temp_dir);
	};

	log::println("your new watch face should now show up");
};

fn mount_mtp(temp_dir: str) void = {
	log::println("mounting MTP");
	let cmd = exec::cmd("simple-mtpfs", temp_dir)!;
	let proc = exec::start(&cmd)!;
	let status = exec::wait(&proc)!;
	if (status.status != 0) {
		log::fatal("simple-mtpfs failed, most likely a permission issue");
	};
};

fn umount_mtp(temp_dir: str) void = {
	log::println("umounting MTP");
	let cmd = exec::cmd("fusermount", "-u", temp_dir)!;
	let proc = exec::start(&cmd)!;
	let status = exec::wait(&proc)!;
};