#!/usr/bin/env ruby
# vim: set sw=2 sts=2 et tw=80 :

# FIXME: I have barely any error handling
# FIXME: Make sure all tmp files and things we use to build the archives get
# cleaned up
require 'gettext-setup'
GettextSetup.initialize(File.absolute_path('../locales', __dir__))

require 'optparse'
require 'pe_backup_tools/cli'

cli = PeBackupTools::CLI.new
subtext = [_('Available subcommands:'),
           '  ' + _('puppet-backup create                     creates a Puppet Enterprise backup file.'),
           '  ' + _('puppet-backup restore <backup-filepath>  restores from a Puppet Enterprise backup file.'),
           '',
           cli.global_options].join("\n")

global = OptionParser.new do |opts|
  opts.banner = 'Usage: puppet-backup <subcommand> [options]'
  opts.separator ''
  opts.separator subtext
end

begin
  global.order!
rescue OptionParser::ParseError => e
  warn e.message
  warn ''
  warn global
  exit(1)
end

command = ARGV.shift
unless cli.command?(command)
  if command
    warn _('Unrecognized command: %{command}') % { command: command }
    warn ''
  end
  warn global
  exit(1)
end

cli.command(command)
