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