#!/usr/bin/env ruby require 'optparse' require 'gls_agent' require 'json' options = {} # Get defaults from ~/.gls_agent begin filecontent = File.open("#{Dir.home}/.gls_agent").read # from option=value\noption2=value2 make {'option':'value','option2':'value2'} filecontent.gsub!(/ *= */, ':') filecontent.gsub!(/(\w+)/, '"\1"') filecontent.gsub!("\n", ",\n") filecontent.chomp!("\n") filecontent.chomp!(',') filecontent = "{ #{filecontent} }" begin j_options = JSON.parse filecontent j_options.each {|o,v| options[o.to_sym] = v} puts "Info: read configuration parameters from ~/.gls_agent, may be overriden with cmd line options." rescue STDERR.puts "Error: configuration file in ~/.gls_agent found but could not be parsed." STDERR.puts $!.inspect,$@ end rescue STDERR.puts "Info: No configuration file in ~/.gls_agent found, all options need to be specified on command line." end optparse = OptionParser.new do |opts| opts.on('-l', '--label-data DATA', 'label data, comma-separated.') do |l| fields = l.split(',') if fields.length != 6 STDERR.puts 'Error: label data has to be in format "John Doe,Home Street,1,1234,City,1".' exit 1 end options[:name] = fields.shift options[:street] = fields.shift options[:streetno] = fields.shift options[:zip] = fields.shift options[:city] = fields.shift options[:weight] = fields.shift end opts.on('-u', '--gls-user GLSUSER', 'username of GLS account.') do |u| options[:user] = u end opts.on('-p', '--gls-password GLSPASSWORD', 'password of GLS account.') do |p| options[:pass] = p end opts.on('-o', '--output-file FILE', 'Write output (pdf) to FILE') do |o| options[:output_file] = o end opts.on('-h', '--help', 'Show help.') do puts opts exit 0 end end optparse.parse! if !options[:user] || !options[:pass] STDERR.puts 'Error: Need to specify user and pass in ~/.gls_agent or -u -p .' exit 1 end if !options[:name] || !options[:street] || !options[:streetno] || !options[:zip] || !options[:city] || !options[:weight] STDERR.puts 'Error: Need to specify delivery data in ~/.gls_agent or -l .' exit 1 end options[:output_file] ||= 'gls-label.pdf' parcel = GLSAgent::ParcelJob.new(options[:name], options[:street], options[:streetno], options[:zip], options[:city], options[:weight]) mech = GLSMech.new mech.user = options[:user] mech.pass = options[:pass] pdf_file_path = mech.save_parcel_label parcel, options[:output_file] if pdf_file_path != options[:output_file] STDERR.puts "Warning: #{options[:output_file]} already exists, saved to #{pdf_file_path} instead!." else puts "Success: Saved label to #{pdf_file_path}." end # Be cool, explicitely. exit 0