about summary refs log tree commit diff stats
path: root/lib/Emacs.pm
blob: dc409a3b0f6b52762a3b693d7b0276ae560c3341 (plain) (blame)
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
#!/usr/bin/perl

package Emacs;

use strict;
use warnings;
use feature 'say';
use Fcntl ':mode';

use IPC::Run3;
use Path::Tiny;
use Term::ANSIColor qw/ :pushpop colored color /;

sub sync {
    my ( $options ) = @_;
    my $verbose = $options->{verbose};

    sub rsync {
        run3 ["openrsync", @_];
    }
    my @def_opt = qw{ --delete -oprtl };
    push @def_opt, "-v" if $verbose;

    # Remove --delete.
    my @no_del_opt = @def_opt;
    shift @no_del_opt;

    if ($options->{authinfo}) {
        my $authinfo_remote = "$ENV{HOME}/.authinfo-remote";
        my %path_perms = (
            $authinfo_remote => 0700,
            "$authinfo_remote/team" => 0600,
            "$authinfo_remote/inst" => 0600,
            "$authinfo_remote/town" => 0600,
        );

        # Check path permissions.
        say LOCALCOLOR CYAN "Checking path permissions...";
        foreach my $path ( sort keys %path_perms ) {
            my $mode = S_IMODE(path($path)->stat->mode);

            if ( $mode == $path_perms{$path} ) {
                say LOCALCOLOR GREEN "[OK] $path"
                    if $verbose;
            } else {
                warn "[ERR] $path
      Expected: $path_perms{$path} :: Got: $mode
      Changing permission...\n";

                path("$path")->chmod($path_perms{$path});
            }
        }
        say LOCALCOLOR CYAN "[DONE] Permissions checked";

        say LOCALCOLOR CYAN "Syncing authinfo...";
        my %authinfo_hosts = (
            "tilde.team" => "$authinfo_remote/team",
            "tilde.institute" => "$authinfo_remote/inst",
            "tilde.town" => "$authinfo_remote/town",
        );
        foreach my $host ( sort keys %authinfo_hosts ) {
            say LOCALCOLOR MAGENTA "$host";
            rsync( @def_opt, $authinfo_hosts{$host}, "andinus\@$host:~/.authinfo" );
        }
        say LOCALCOLOR CYAN "[DONE] authinfo sync";
    }

    if ($options->{config}) {
        say LOCALCOLOR CYAN "Syncing emacs config...";
        my @hosts = qw{ tilde.team tilde.institute envs.net tilde.town };

        my $e_conf = "$ENV{HOME}/.emacs.d";
        my @paths = (
            "$e_conf/init.el",
            "$e_conf/e-init.el",
            "$e_conf/e-init.org",
            "$e_conf/elpa/",
        );
        foreach my $host (@hosts) {
            say LOCALCOLOR MAGENTA "$host";
            foreach my $path (@paths) {
                say "  $path";
                rsync( @def_opt, $path, "andinus\@$host:$path");
            }
        }
        say LOCALCOLOR CYAN "[Done] Emacs config sync";
    }

    if ( $options->{irclogs} ) {
        say LOCALCOLOR CYAN "Syncing irclogs...";
        my @hosts = qw{ tilde.team tilde.institute envs.net tilde.town };

        my $e_conf = "$ENV{HOME}/.emacs.d";
        foreach my $host (@hosts) {
            say LOCALCOLOR MAGENTA "$host";
            rsync( @no_del_opt, "andinus\@$host:~/.emacs.d/irclogs/",
                   "$e_conf/irclogs/$host/");
        }

        say LOCALCOLOR CYAN "[DONE] irclogs sync";
    }

}

1;