Fix build.
[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 "lib/safe_stringstream.h"
33 #include <dcp/certificate.h>
34 #include <getopt.h>
35 #include <iostream>
36
37 using std::string;
38 using std::cout;
39 using std::cerr;
40 using std::list;
41 using std::vector;
42 using boost::shared_ptr;
43
44 static void
45 help ()
46 {
47         cerr << "Syntax: " << program_name << " [OPTION] [<FILM>]\n"
48                 "  -h, --help             show this help\n"
49                 "  -o, --output           output file or directory\n"
50                 "  -f, --valid-from       valid from time (in local time zone of the cinema) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
51                 "  -t, --valid-to         valid to time (in local time zone of the cinema) (e.g. \"2014-09-28 01:41:51\")\n"
52                 "  -d, --valid-duration   valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
53                 "      --formulation      modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
54                 "  -z, --zip              ZIP each cinema's KDMs into its own file\n"
55                 "  -v, --verbose          be verbose\n"
56                 "  -c, --cinema           specify a cinema, either by name or email address\n"
57                 "      --cinemas          list known cinemas from the DCP-o-matic settings\n"
58                 "      --certificate file containing projector certificate\n\n"
59                 "For example:\n\n"
60                 "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"
61                 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
62                 "\tdcpomatic_kdm -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
63 }
64
65 static void
66 error (string m)
67 {
68         cerr << program_name << ": " << m << "\n";
69         exit (EXIT_FAILURE);
70 }
71
72 static boost::posix_time::ptime
73 time_from_string (string t)
74 {
75         if (t == "now") {
76                 return boost::posix_time::second_clock::local_time ();
77         }
78
79         return boost::posix_time::time_from_string (t);
80 }
81
82 static boost::posix_time::time_duration
83 duration_from_string (string d)
84 {
85         SafeStringStream s (d);
86         int N;
87         string unit;
88         s >> N >> unit;
89
90         if (N == 0) {
91                 cerr << "Could not understand duration \"" << d << "\"\n";
92                 exit (EXIT_FAILURE);
93         }
94
95         if (unit == "year" || unit == "years") {
96                 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
97         } else if (unit == "week" || unit == "weeks") {
98                 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
99         } else if (unit == "day" || unit == "days") {
100                 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
101         } else if (unit == "hour" || unit == "hours") {
102                 return boost::posix_time::time_duration (N, 0, 0, 0);
103         }
104
105         cerr << "Could not understand duration \"" << d << "\"\n";
106         exit (EXIT_FAILURE);
107 }
108
109 int main (int argc, char* argv[])
110 {
111         boost::filesystem::path output;
112         boost::optional<boost::posix_time::ptime> valid_from;
113         boost::optional<boost::posix_time::ptime> valid_to;
114         string certificate_file;
115         bool zip = false;
116         string cinema_name;
117         bool cinemas = false;
118         string duration_string;
119         bool verbose = false;
120         dcp::Formulation formulation = dcp::MODIFIED_TRANSITIONAL_1;
121
122         program_name = argv[0];
123
124         int option_index = 0;
125         while (true) {
126                 static struct option long_options[] = {
127                         { "help", no_argument, 0, 'h'},
128                         { "output", required_argument, 0, 'o'},
129                         { "valid-from", required_argument, 0, 'f'},
130                         { "valid-to", required_argument, 0, 't'},
131                         { "certificate", required_argument, 0, 'A' },
132                         { "cinema", required_argument, 0, 'c' },
133                         { "cinemas", no_argument, 0, 'B' },
134                         { "zip", no_argument, 0, 'z' },
135                         { "duration", required_argument, 0, 'd' },
136                         { "verbose", no_argument, 0, 'v' },
137                         { "formulation", required_argument, 0, 'C' },
138                         { 0, 0, 0, 0 }
139                 };
140
141                 int c = getopt_long (argc, argv, "ho:f:t:c:A:Bzd:vC:", long_options, &option_index);
142
143                 if (c == -1) {
144                         break;
145                 }
146
147                 switch (c) {
148                 case 'h':
149                         help ();
150                         exit (EXIT_SUCCESS);
151                 case 'o':
152                         output = optarg;
153                         break;
154                 case 'f':
155                         valid_from = time_from_string (optarg);
156                         break;
157                 case 't':
158                         valid_to = time_from_string (optarg);
159                         break;
160                 case 'A':
161                         certificate_file = optarg;
162                         break;
163                 case 'c':
164                         cinema_name = optarg;
165                         break;
166                 case 'B':
167                         cinemas = true;
168                         break;
169                 case 'z':
170                         zip = true;
171                         break;
172                 case 'd':
173                         duration_string = optarg;
174                         break;
175                 case 'v':
176                         verbose = true;
177                         break;
178                 case 'C':
179                         if (string (optarg) == "modified-transitional-1") {
180                                 formulation = dcp::MODIFIED_TRANSITIONAL_1;
181                         } else if (string (optarg) == "dci-any") {
182                                 formulation = dcp::DCI_ANY;
183                         } else if (string (optarg) == "dci-specific") {
184                                 formulation = dcp::DCI_SPECIFIC;
185                         } else {
186                                 error ("unrecognised KDM formulation " + string (optarg));
187                         }
188                 }
189         }
190
191         if (cinemas) {
192                 list<boost::shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
193                 for (list<boost::shared_ptr<Cinema> >::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
194                         cout << (*i)->name << " (" << Emailer::address_list ((*i)->emails) << ")\n";
195                 }
196                 exit (EXIT_SUCCESS);
197         }
198
199         if (duration_string.empty() && !valid_to) {
200                 error ("you must specify a --valid-duration or --valid-to");
201         }
202
203         if (!valid_from) {
204                 error ("you must specify --valid-from");
205                 exit (EXIT_FAILURE);
206         }
207
208         if (optind >= argc) {
209                 help ();
210                 exit (EXIT_FAILURE);
211         }
212
213         if (cinema_name.empty() && certificate_file.empty()) {
214                 error ("you must specify either a cinema, a screen or a certificate file");
215         }
216
217         if (!duration_string.empty ()) {
218                 valid_to = valid_from.get() + duration_from_string (duration_string);
219         }
220
221         string const film_dir = argv[optind];
222
223         dcpomatic_setup_path_encoding ();
224         dcpomatic_setup ();
225
226         shared_ptr<Film> film;
227         try {
228                 film.reset (new Film (film_dir));
229                 film->read_metadata ();
230                 if (verbose) {
231                         cout << "Read film " << film->name () << "\n";
232                 }
233         } catch (std::exception& e) {
234                 cerr << program_name << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
235                 exit (EXIT_FAILURE);
236         }
237
238         if (verbose) {
239                 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
240         }
241
242         /* XXX: allow specification of this */
243         vector<CPLSummary> cpls = film->cpls ();
244         if (cpls.empty ()) {
245                 error ("no CPLs found in film");
246         } else if (cpls.size() > 1) {
247                 error ("more than one CPL found in film");
248         }
249
250         boost::filesystem::path cpl = cpls.front().cpl_file;
251
252         if (cinema_name.empty ()) {
253
254                 if (output.empty ()) {
255                         error ("you must specify --output");
256                 }
257
258                 dcp::Certificate certificate (dcp::file_to_string (certificate_file));
259                 dcp::EncryptedKDM kdm = film->make_kdm (
260                         certificate, vector<dcp::Certificate>(), cpl, dcp::LocalTime (valid_from.get()), dcp::LocalTime (valid_to.get()), formulation
261                         );
262                 kdm.as_xml (output);
263                 if (verbose) {
264                         cout << "Generated KDM " << output << " for certificate.\n";
265                 }
266         } else {
267
268                 list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
269                 list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
270                 while (
271                         i != cinemas.end() &&
272                         (*i)->name != cinema_name &&
273                         find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
274
275                         ++i;
276                 }
277
278                 if (i == cinemas.end ()) {
279                         cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
280                         exit (EXIT_FAILURE);
281                 }
282
283                 if (output.empty ()) {
284                         output = ".";
285                 }
286
287                 try {
288                         list<ScreenKDM> screen_kdms = film->make_kdms (
289                                 (*i)->screens(), cpl, valid_from.get(), valid_to.get(), formulation
290                                 );
291
292                         if (zip) {
293                                 CinemaKDMs::write_zip_files (film->name(), CinemaKDMs::collect (screen_kdms), output);
294
295                                 if (verbose) {
296                                         cout << "Wrote ZIP files to " << output << "\n";
297                                 }
298                         } else {
299                                 ScreenKDM::write_files (film->name(), screen_kdms, output);
300
301                                 if (verbose) {
302                                         cout << "Wrote KDM files to " << output << "\n";
303                                 }
304                         }
305                 } catch (FileError& e) {
306                         cerr << argv[0] << ": " << e.what() << " (" << e.file().string() << ")\n";
307                         exit (EXIT_FAILURE);
308                 } catch (KDMError& e) {
309                         cerr << argv[0] << ": " << e.what() << "\n";
310                         exit (EXIT_FAILURE);
311                 }
312         }
313
314         return 0;
315 }