Remove all use of stringstream in an attempt to fix
[dcpomatic.git] / src / tools / dcpomatic_kdm_cli.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/tools/dcpomatic_kdm_cli.cc
22  *  @brief Command-line program to generate KDMs.
23  */
24
25 #include "lib/film.h"
26 #include "lib/cinema.h"
27 #include "lib/screen_kdm.h"
28 #include "lib/cinema_kdms.h"
29 #include "lib/config.h"
30 #include "lib/exceptions.h"
31 #include "lib/emailer.h"
32 #include <dcp/certificate.h>
33 #include <getopt.h>
34 #include <iostream>
35
36 using std::string;
37 using std::cout;
38 using std::cerr;
39 using std::list;
40 using std::vector;
41 using boost::shared_ptr;
42
43 static void
44 help ()
45 {
46         cerr << "Syntax: " << program_name << " [OPTION] [<FILM>]\n"
47                 "  -h, --help             show this help\n"
48                 "  -o, --output           output file or directory\n"
49                 "  -f, --valid-from       valid from time (in local time zone of the cinema) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
50                 "  -t, --valid-to         valid to time (in local time zone of the cinema) (e.g. \"2014-09-28 01:41:51\")\n"
51                 "  -d, --valid-duration   valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
52                 "      --formulation      modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
53                 "  -z, --zip              ZIP each cinema's KDMs into its own file\n"
54                 "  -v, --verbose          be verbose\n"
55                 "  -c, --cinema           specify a cinema, either by name or email address\n"
56                 "      --cinemas          list known cinemas from the DCP-o-matic settings\n"
57                 "      --certificate file containing projector certificate\n\n"
58                 "For example:\n\n"
59                 "Create KDMs for my_great_movie to play in all of Fred's Cinema's screens for the next two weeks and zip them up.\n"
60                 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
61                 "\tdcpomatic_kdm -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
62 }
63
64 static void
65 error (string m)
66 {
67         cerr << program_name << ": " << m << "\n";
68         exit (EXIT_FAILURE);
69 }
70
71 static boost::posix_time::ptime
72 time_from_string (string t)
73 {
74         if (t == "now") {
75                 return boost::posix_time::second_clock::local_time ();
76         }
77
78         return boost::posix_time::time_from_string (t);
79 }
80
81 static boost::posix_time::time_duration
82 duration_from_string (string d)
83 {
84         int N;
85         char unit_buf[64] = "\0";
86         sscanf (d.c_str(), "%d %64s", &N, unit_buf);
87         string const unit (unit_buf);
88
89         if (N == 0) {
90                 cerr << "Could not understand duration \"" << d << "\"\n";
91                 exit (EXIT_FAILURE);
92         }
93
94         if (unit == "year" || unit == "years") {
95                 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
96         } else if (unit == "week" || unit == "weeks") {
97                 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
98         } else if (unit == "day" || unit == "days") {
99                 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
100         } else if (unit == "hour" || unit == "hours") {
101                 return boost::posix_time::time_duration (N, 0, 0, 0);
102         }
103
104         cerr << "Could not understand duration \"" << d << "\"\n";
105         exit (EXIT_FAILURE);
106 }
107
108 int main (int argc, char* argv[])
109 {
110         boost::filesystem::path output;
111         boost::optional<boost::posix_time::ptime> valid_from;
112         boost::optional<boost::posix_time::ptime> valid_to;
113         string certificate_file;
114         bool zip = false;
115         string cinema_name;
116         bool cinemas = false;
117         string duration_string;
118         bool verbose = false;
119         dcp::Formulation formulation = dcp::MODIFIED_TRANSITIONAL_1;
120
121         program_name = argv[0];
122
123         int option_index = 0;
124         while (true) {
125                 static struct option long_options[] = {
126                         { "help", no_argument, 0, 'h'},
127                         { "output", required_argument, 0, 'o'},
128                         { "valid-from", required_argument, 0, 'f'},
129                         { "valid-to", required_argument, 0, 't'},
130                         { "certificate", required_argument, 0, 'A' },
131                         { "cinema", required_argument, 0, 'c' },
132                         { "cinemas", no_argument, 0, 'B' },
133                         { "zip", no_argument, 0, 'z' },
134                         { "duration", required_argument, 0, 'd' },
135                         { "verbose", no_argument, 0, 'v' },
136                         { "formulation", required_argument, 0, 'C' },
137                         { 0, 0, 0, 0 }
138                 };
139
140                 int c = getopt_long (argc, argv, "ho:f:t:c:A:Bzd:vC:", long_options, &option_index);
141
142                 if (c == -1) {
143                         break;
144                 }
145
146                 switch (c) {
147                 case 'h':
148                         help ();
149                         exit (EXIT_SUCCESS);
150                 case 'o':
151                         output = optarg;
152                         break;
153                 case 'f':
154                         valid_from = time_from_string (optarg);
155                         break;
156                 case 't':
157                         valid_to = time_from_string (optarg);
158                         break;
159                 case 'A':
160                         certificate_file = optarg;
161                         break;
162                 case 'c':
163                         cinema_name = optarg;
164                         break;
165                 case 'B':
166                         cinemas = true;
167                         break;
168                 case 'z':
169                         zip = true;
170                         break;
171                 case 'd':
172                         duration_string = optarg;
173                         break;
174                 case 'v':
175                         verbose = true;
176                         break;
177                 case 'C':
178                         if (string (optarg) == "modified-transitional-1") {
179                                 formulation = dcp::MODIFIED_TRANSITIONAL_1;
180                         } else if (string (optarg) == "dci-any") {
181                                 formulation = dcp::DCI_ANY;
182                         } else if (string (optarg) == "dci-specific") {
183                                 formulation = dcp::DCI_SPECIFIC;
184                         } else {
185                                 error ("unrecognised KDM formulation " + string (optarg));
186                         }
187                 }
188         }
189
190         if (cinemas) {
191                 list<boost::shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
192                 for (list<boost::shared_ptr<Cinema> >::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
193                         cout << (*i)->name << " (" << Emailer::address_list ((*i)->emails) << ")\n";
194                 }
195                 exit (EXIT_SUCCESS);
196         }
197
198         if (duration_string.empty() && !valid_to) {
199                 error ("you must specify a --valid-duration or --valid-to");
200         }
201
202         if (!valid_from) {
203                 error ("you must specify --valid-from");
204                 exit (EXIT_FAILURE);
205         }
206
207         if (optind >= argc) {
208                 help ();
209                 exit (EXIT_FAILURE);
210         }
211
212         if (cinema_name.empty() && certificate_file.empty()) {
213                 error ("you must specify either a cinema, a screen or a certificate file");
214         }
215
216         if (!duration_string.empty ()) {
217                 valid_to = valid_from.get() + duration_from_string (duration_string);
218         }
219
220         string const film_dir = argv[optind];
221
222         dcpomatic_setup_path_encoding ();
223         dcpomatic_setup ();
224
225         shared_ptr<Film> film;
226         try {
227                 film.reset (new Film (film_dir));
228                 film->read_metadata ();
229                 if (verbose) {
230                         cout << "Read film " << film->name () << "\n";
231                 }
232         } catch (std::exception& e) {
233                 cerr << program_name << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
234                 exit (EXIT_FAILURE);
235         }
236
237         if (verbose) {
238                 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
239         }
240
241         /* XXX: allow specification of this */
242         vector<CPLSummary> cpls = film->cpls ();
243         if (cpls.empty ()) {
244                 error ("no CPLs found in film");
245         } else if (cpls.size() > 1) {
246                 error ("more than one CPL found in film");
247         }
248
249         boost::filesystem::path cpl = cpls.front().cpl_file;
250
251         if (cinema_name.empty ()) {
252
253                 if (output.empty ()) {
254                         error ("you must specify --output");
255                 }
256
257                 dcp::Certificate certificate (dcp::file_to_string (certificate_file));
258                 dcp::EncryptedKDM kdm = film->make_kdm (
259                         certificate, vector<dcp::Certificate>(), cpl, dcp::LocalTime (valid_from.get()), dcp::LocalTime (valid_to.get()), formulation
260                         );
261                 kdm.as_xml (output);
262                 if (verbose) {
263                         cout << "Generated KDM " << output << " for certificate.\n";
264                 }
265         } else {
266
267                 list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
268                 list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
269                 while (
270                         i != cinemas.end() &&
271                         (*i)->name != cinema_name &&
272                         find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
273
274                         ++i;
275                 }
276
277                 if (i == cinemas.end ()) {
278                         cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
279                         exit (EXIT_FAILURE);
280                 }
281
282                 if (output.empty ()) {
283                         output = ".";
284                 }
285
286                 dcp::NameFormat::Map values;
287                 values['f'] = film->name();
288                 values['b'] = dcp::LocalTime(valid_from.get()).date() + " " + dcp::LocalTime(valid_from.get()).time_of_day();
289                 values['e'] = dcp::LocalTime(valid_to.get()).date() + " " + dcp::LocalTime(valid_to.get()).time_of_day();
290
291                 try {
292                         list<ScreenKDM> screen_kdms = film->make_kdms (
293                                 (*i)->screens(), cpl, valid_from.get(), valid_to.get(), formulation
294                                 );
295
296                         if (zip) {
297                                 CinemaKDMs::write_zip_files (
298                                         CinemaKDMs::collect (screen_kdms),
299                                         output,
300                                         Config::instance()->kdm_filename_format(),
301                                         values
302                                         );
303
304                                 if (verbose) {
305                                         cout << "Wrote ZIP files to " << output << "\n";
306                                 }
307                         } else {
308                                 ScreenKDM::write_files (screen_kdms, output, Config::instance()->kdm_filename_format(), values);
309
310                                 if (verbose) {
311                                         cout << "Wrote KDM files to " << output << "\n";
312                                 }
313                         }
314                 } catch (FileError& e) {
315                         cerr << argv[0] << ": " << e.what() << " (" << e.file().string() << ")\n";
316                         exit (EXIT_FAILURE);
317                 } catch (KDMError& e) {
318                         cerr << argv[0] << ": " << e.what() << "\n";
319                         exit (EXIT_FAILURE);
320                 }
321         }
322
323         return 0;
324 }