summaryrefslogtreecommitdiff
path: root/script/fax_new
blob: 125632c3531ebb978dde76844e0c79dd3b5c857d (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#! /usr/bin/env ruby

begin

if !ARGV[0] or ARGV[0] == '' or !ARGV[10] or ARGV[10] == ''
	$stderr.puts "usage: fax_new <fax_account_id> \
<result_code> \
<document_total_pages> \
<document_transferred_pages> \
<ecm_requested> \
<ecm_used> \
<image_resolution> \
<remote_station_id> \
<transfer_rate> \
<transmission_time> \
<document> \
<caller_id_number> \
<caller_id_name>"
  exit 1
end

fax_arguments = { 
  :fax_account_id => ARGV[0],
  :result_code => ARGV[1],
  :document_total_pages => ARGV[2],
  :document_transferred_pages => ARGV[3],
  :ecm_requested => ARGV[4],
  :ecm_used => ARGV[5],
  :image_resolution => ARGV[6],
  :remote_station_id => ARGV[7],
  :transfer_rate => ARGV[8],
  :transmission_time => ARGV[9],
  :document => ARGV[10],
  :caller_id_number => ARGV[11],
  :caller_id_name => ARGV[12],
}

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require APP_PATH

begin
	Rails.application.require_environment!
rescue ActiveRecord::AdapterNotSpecified => e
	error "No such Rails environment: \"#{Rails.env}\"."
	exit 1
end

TIFF_FUFFIX = ".tiff"
PDF_SUFFIX = ".pdf"

tiff_file = fax_arguments[:document]

if !tiff_file or !File.exists?( tiff_file ) 
  $stderr.puts "File \"#{tiff_file}\" does not exist"
  exit 1
end

paper_size = "letter"
pdf_file = "#{File.dirname(tiff_file)}/#{File.basename(tiff_file, TIFF_FUFFIX)}#{PDF_SUFFIX}"

system "tiff2pdf \\
  -o \"#{pdf_file}\" \\
  -p #{paper_size} \\
  -a \"#{fax_arguments[:remote_station_id]}\" \\
  -c \"AMOOMA Gemeinschaft version #{GsParameter.get('GEMEINSCHAFT_VERSION')}\" \\
  -t \"#{fax_arguments[:remote_station_id]}\" \"#{tiff_file}\""

if !File.exists?( pdf_file ) 
  $stderr.puts "File \"#{pdf_file}\" does not exist"
  exit 1
end

fax_account = FaxAccount.find(fax_arguments[:fax_account_id])
if !fax_account
  $stderr.puts "Fax account \"#{fax_arguments[:fax_account_id]}\" does not exist"
  exit 1
end

fax_arguments[:document]          = nil
fax_arguments[:success]           = true
fax_arguments[:inbound]           = true
fax_arguments[:sent_at]           = Time.now
fax_arguments[:local_station_id]  = fax_account.station_id
fax_arguments[:retry_counter]     = 0
fax_arguments[:fax_resolution_id] = FaxResolution.first.id
fax_arguments[:image_size]        = File.size(tiff_file)
fax_arguments[:ecm_used]          = fax_arguments[:ecm_used] == "on" ? true : false
fax_document = fax_account.fax_documents.build(fax_arguments)
fax_document.document = File.open(pdf_file)

if ! fax_document.save
  $stderr.puts "Error(s) creating fax document:"
  $stderr.puts fax_document.errors.inspect
  exit 1
end

fax_document.mark_as_inbound!

Notifications.new_fax(fax_document).deliver

exit 0

rescue SignalException => e
	$stderr.print "#{e.class.to_s}"
	$stderr.print " (Signal #{e.signo.to_s})" if e.respond_to?(:signo) && e.signo
	$stderr.puts ""
	exit 130
end

exit 0