-
Notifications
You must be signed in to change notification settings - Fork 11
/
install-dotfile
executable file
·67 lines (60 loc) · 2.13 KB
/
install-dotfile
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
#!/usr/bin/env ocaml
#use "topfind";;
#require "unix";;
let home = Sys.getenv "HOME"
let verbose = false
let read_file name =
match open_in_bin name with
| exception Sys_error _ -> None
| f ->
let result = Buffer.create 4096 in
let buf = Bytes.create 4096 in
let rec go () =
match input f buf 0 (Bytes.length buf) with
| 0 ->
close_in_noerr f;
Buffer.contents result
| len ->
Buffer.add_subbytes result buf 0 len;
go ()
in
Some (go ())
let write_file name permissions contents =
let f = open_out_gen [Open_binary; Open_wronly; Open_creat] permissions name in
output_string f contents;
close_out_noerr f
let rec mkdir_recursive dir perms =
if dir = "/" then invalid_arg "Cannot create /";
match Sys.is_directory dir with
| true -> ()
| false -> invalid_arg (Printf.sprintf "Is a file: %s" dir);
| exception Sys_error _ ->
let parent = Filename.dirname dir in
assert (String.length parent < String.length dir);
mkdir_recursive parent perms;
Sys.mkdir dir perms
let install f =
let source = Filename.concat (Filename.dirname Sys.argv.(0)) f in
let dest = Filename.concat home f in
let source_contents = read_file source |> Option.get in
let source_perms = (Unix.stat source).st_perm in
let dest_contents = read_file dest in
let dest_perms = try (Unix.stat dest).st_perm with Unix.Unix_error _ -> 0 in
match dest_contents with
| None ->
Printf.eprintf "cp %s %s\n%!" source dest;
mkdir_recursive (Filename.dirname dest) 0o777;
write_file dest source_perms source_contents
| Some dest_contents when String.equal source_contents dest_contents ->
if source_perms = dest_perms then (
if verbose then Printf.eprintf "Already matches: %s %s\n%!" source dest
) else (
Printf.eprintf "chmod 0o%o %s\n%!" source_perms dest;
Unix.chmod dest source_perms
)
| Some _dest_contents ->
Printf.eprintf "Not overwriting: %s %s\n%!" source dest;
let pid = Unix.create_process "diff" [| "diff"; dest; source |] Unix.stdin Unix.stdout Unix.stderr in
let _, _ = Unix.waitpid [] pid in
()
let () = install Sys.argv.(1)