#!/usr/bin/env ruby # -*- encoding: utf-8 -*- require 'optparse' $:.unshift(File.expand_path('../../lib/', __FILE__)) require 'imap/backup' KNOWN_COMMANDS = [ {:name => 'help', :help => 'Show usage'}, {:name => 'setup', :help => 'Create/edit the configuration file'}, {:name => 'backup', :help => 'Do the backup (default)'}, {:name => 'folders', :help => 'List folders for all (or selected) accounts'}, {:name => 'status', :help => 'List count of non backed-up emails per folder'}, ] options = {:command => 'backup'} opts = OptionParser.new do |opts| opts.banner = "Usage: #{$0} [options] COMMAND" opts.separator '' opts.separator 'Commands:' KNOWN_COMMANDS.each do |command| opts.separator "\t%- 20s %s" % [command[:name], command[:help]] end opts.separator '' opts.separator 'Common options:' opts.on('-a', '--accounts ACCOUNT1[,ACCOUNT2,...]', Array, 'only these accounts') do |account| options[:account] = account end opts.on_tail("-h", "--help", "Show usage") do puts opts exit end end opts.parse! if ARGV.size > 0 options[:command] = ARGV.shift end if KNOWN_COMMANDS.find{|c| c[:name] == options[:command] }.nil? raise "Unknown command '#{options[:command]}'" end if options[:command] == 'help' puts opts exit end begin configuration = Imap::Backup::Configuration::List.new(options[:accounts]) rescue Imap::Backup::ConfigurationNotFound Imap::Backup::Configuration::Setup.new.run exit end case options[:command] when 'backup' configuration.each_connection do |connection| connection.run_backup end when 'folders' configuration.each_connection do |connection| puts connection.username folders = connection.folders if folders.nil? warn 'Unable to list account folders' exit 1 end folders.each { |f| puts "\t" + f.name } end when 'setup' Imap::Backup::Configuration::Setup.new.run when 'status' configuration.each_connection do |connection| puts connection.username folders = connection.status folders.each do |f| missing_locally = f[:remote] - f[:local] puts "#{f[:name]}: #{missing_locally.size}" end end end