m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/mmm.rb
blob: cb2251754532ff072d64dd9364e105f905910995 (plain)
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
require 'yaml'
require 'optparse'

require './mbsync'
require './mutt'
require './msmtp'

options = {
  config: '~/.config/mmm/config.yaml',
  write: false
}

OptionParser.new do |opts|
  opts.on '-w', '--write', 'Write configs to disk. Otherwise, just outputs to stdout.'
  opts.on '-cCONFIG', '--config CONFIG', 'Specify configuration file, defaults to ~/.config/mmm/config.'
end.parse!(into: options)

config = YAML.load File.read(File.expand_path options[:config])

def write_mbsync_config config, options
  mbsync_config = make_mbsync_config config
  (options[:write] ? File.open(File.expand_path(config['mbsyncrc']), 'w') : $stdout).puts mbsync_config
end

def write_msmtp_config config, options
  preamble = msmtprc_preamble
  io = (options[:write] ? File.open(File.expand_path(config['msmtprc']), 'w') : $stdout)
  io.puts preamble
  io.puts
  msmtp_config = make_msmtp_config config
  io.puts msmtp_config
end

def write_mutt_configs config, options
  mutt_configs = make_mutt_configs config
  profiles_directory = File.expand_path(config['neomutt_profiles_directory'])
  mutt_configs.each do |account, account_config|
    path = File.join(profiles_directory, "#{account}.neomuttrc")
    (options[:write] ? File.open(path, 'w') : $stdout).puts account_config
  end

  mmmrc = make_mmm_muttrc config
  mmmrc_path = File.join(File.expand_path(config['neomutt_config_dir']), "mmm.neomuttrc")
  (options[:write] ? File.open(mmmrc_path, 'w') : $stdout).puts mmmrc
end

write_mbsync_config config, options
write_msmtp_config config, options
write_mutt_configs config, options