Class: Quadtone::Tools::AddPrinter

Inherits:
Quadtone::Tool show all
Defined in:
lib/quadtone/tools/add_printer.rb

Instance Attribute Summary

Attributes inherited from Quadtone::Tool

#profile, #verbose

Instance Method Summary collapse

Methods inherited from Quadtone::Tool

#parse_global_option, #parse_option, process_args, #process_environment

Instance Method Details

#load_profileObject



7
8
9
# File 'lib/quadtone/tools/add_printer.rb', line 7

def load_profile
  false
end

#run(printer) ⇒ Object



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
# File 'lib/quadtone/tools/add_printer.rb', line 11

def run(printer)

  #FIXME: move this into Printer class

  unless %x{lpstat -v #{printer} 2>/dev/null}.empty?
    raise ToolUsageError, "Printer #{printer.inspect} already exists"
  end

  curves_dir          = Path.new('/Library/Printers/QTR/quadtone') / printer
  ppds_dir            = Path.new('/Library/Printers/PPDs/Contents/Resources')
  cups_serverbin_dir  = Path.new(%x{cups-config --serverbin}.chomp)
  cups_backend_usb_tool = cups_serverbin_dir / 'backend' / 'usb'

  model = printer.split(/[-_=]/).first
  model_ppd = "#{model}.ppd.gz"
  ppd_file = ppds_dir / model_ppd

  raise "QuadToneRIP does not support printer model #{model.inspect}" unless ppd_file.exist?

  uri = loc = nil

  File.popen(cups_backend_usb_tool.to_s).readlines.each do |line|
    #FIXME: Too fragile -- use 'lpinfo' to find all printers
    if line =~ /(usb:.*EPSON.*#{Regexp.escape(model.sub(/^Quad/, ''))})/
      uri = $1
      loc = "USB Printer"
      break
    end
  end

  if uri.nil?
    loop do
      print "Enter IP address of IPP printer, or <return> to cancel: "
      ipp = STDIN.gets.chomp
      if ipp.empty?
        warn "Install cancelled."
        return
      end
      if ipp =~ /^\d+\.\d+\.\d+\.\d+$/
        uri = "lpd://#{ipp}"
        loc = "Network Printer IPP = #{ipp}"
        break
      end
      warn "Invalid IPP number: #{ipp.inspect}"
    end
  end

  warn "Creating printer #{printer.inspect}"
  system('lpadmin',
    '-p', printer,
    '-E',
    '-m', model_ppd,
    '-L', loc,
    '-v', uri)
  raise "Failed to create printer" unless $? == 0

  curves_dir.mkpath
end