Separate from-film code out into a method.
[dcpomatic.git] / src / tools / dcpomatic_kdm_cli.cc
1 /*
2     Copyright (C) 2013-2017 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 void
117 write_files (list<ScreenKDM> screen_kdms, bool zip, boost::filesystem::path output, dcp::NameFormat::Map values, bool verbose)
118 {
119         if (zip) {
120                 int const N = CinemaKDMs::write_zip_files (
121                         CinemaKDMs::collect (screen_kdms),
122                         output,
123                         Config::instance()->kdm_container_name_format(),
124                         Config::instance()->kdm_filename_format(),
125                         values,
126                         bind (&always_overwrite)
127                         );
128
129                 if (verbose) {
130                         cout << "Wrote " << N << " ZIP files to " << output << "\n";
131                 }
132         } else {
133                 int const N = ScreenKDM::write_files (
134                         screen_kdms, output, Config::instance()->kdm_filename_format(), values,
135                         bind (&always_overwrite)
136                         );
137
138                 if (verbose) {
139                         cout << "Wrote " << N << " KDM files to " << output << "\n";
140                 }
141         }
142 }
143
144 shared_ptr<Cinema>
145 find_cinema (string cinema_name)
146 {
147         list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
148         list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
149         while (
150                 i != cinemas.end() &&
151                 (*i)->name != cinema_name &&
152                 find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
153
154                 ++i;
155         }
156
157         if (i == cinemas.end ()) {
158                 cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
159                 exit (EXIT_FAILURE);
160         }
161
162         return *i;
163 }
164
165 void
166 from_film (
167         boost::filesystem::path film_dir,
168         bool verbose,
169         optional<string> cinema_name,
170         optional<boost::filesystem::path> output,
171         optional<boost::filesystem::path> certificate_file,
172         boost::posix_time::ptime valid_from,
173         boost::posix_time::ptime valid_to,
174         dcp::Formulation formulation,
175         bool zip
176         )
177 {
178         shared_ptr<Film> film;
179         try {
180                 film.reset (new Film (film_dir));
181                 film->read_metadata ();
182                 if (verbose) {
183                         cout << "Read film " << film->name () << "\n";
184                 }
185         } catch (std::exception& e) {
186                 cerr << program_name << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
187                 exit (EXIT_FAILURE);
188         }
189
190         /* XXX: allow specification of this */
191         vector<CPLSummary> cpls = film->cpls ();
192         if (cpls.empty ()) {
193                 error ("no CPLs found in film");
194         } else if (cpls.size() > 1) {
195                 error ("more than one CPL found in film");
196         }
197
198         boost::filesystem::path cpl = cpls.front().cpl_file;
199
200         if (!cinema_name) {
201
202                 if (!output) {
203                         error ("you must specify --output");
204                 }
205
206                 dcp::Certificate certificate (dcp::file_to_string (*certificate_file));
207                 dcp::EncryptedKDM kdm = film->make_kdm (
208                         certificate, vector<dcp::Certificate>(), cpl, dcp::LocalTime (valid_from), dcp::LocalTime (valid_to), formulation
209                         );
210                 kdm.as_xml (*output);
211                 if (verbose) {
212                         cout << "Generated KDM " << *output << " for certificate.\n";
213                 }
214         } else {
215
216                 if (!output) {
217                         output = ".";
218                 }
219
220                 dcp::NameFormat::Map values;
221                 values['f'] = film->name();
222                 values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
223                 values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
224
225                 try {
226                         list<ScreenKDM> screen_kdms = film->make_kdms (
227                                 find_cinema(*cinema_name)->screens(), cpl, valid_from, valid_to, formulation
228                                 );
229
230                         write_files (screen_kdms, zip, *output, values, verbose);
231                 } catch (FileError& e) {
232                         cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
233                         exit (EXIT_FAILURE);
234                 } catch (KDMError& e) {
235                         cerr << program_name << ": " << e.what() << "\n";
236                         exit (EXIT_FAILURE);
237                 }
238         }
239 }
240
241 int main (int argc, char* argv[])
242 {
243         optional<boost::filesystem::path> output;
244         optional<boost::posix_time::ptime> valid_from;
245         optional<boost::posix_time::ptime> valid_to;
246         optional<boost::filesystem::path> certificate_file;
247         bool zip = false;
248         optional<string> cinema_name;
249         bool cinemas = false;
250         optional<string> duration_string;
251         bool verbose = false;
252         dcp::Formulation formulation = dcp::MODIFIED_TRANSITIONAL_1;
253
254         program_name = argv[0];
255
256         int option_index = 0;
257         while (true) {
258                 static struct option long_options[] = {
259                         { "help", no_argument, 0, 'h'},
260                         { "output", required_argument, 0, 'o'},
261                         { "valid-from", required_argument, 0, 'f'},
262                         { "valid-to", required_argument, 0, 't'},
263                         { "certificate", required_argument, 0, 'A' },
264                         { "cinema", required_argument, 0, 'c' },
265                         { "cinemas", no_argument, 0, 'B' },
266                         { "zip", no_argument, 0, 'z' },
267                         { "duration", required_argument, 0, 'd' },
268                         { "verbose", no_argument, 0, 'v' },
269                         { "formulation", required_argument, 0, 'C' },
270                         { 0, 0, 0, 0 }
271                 };
272
273                 int c = getopt_long (argc, argv, "ho:f:t:c:A:Bzd:vC:", long_options, &option_index);
274
275                 if (c == -1) {
276                         break;
277                 }
278
279                 switch (c) {
280                 case 'h':
281                         help ();
282                         exit (EXIT_SUCCESS);
283                 case 'o':
284                         output = optarg;
285                         break;
286                 case 'f':
287                         valid_from = time_from_string (optarg);
288                         break;
289                 case 't':
290                         valid_to = time_from_string (optarg);
291                         break;
292                 case 'A':
293                         certificate_file = optarg;
294                         break;
295                 case 'c':
296                         cinema_name = optarg;
297                         break;
298                 case 'B':
299                         cinemas = true;
300                         break;
301                 case 'z':
302                         zip = true;
303                         break;
304                 case 'd':
305                         duration_string = optarg;
306                         break;
307                 case 'v':
308                         verbose = true;
309                         break;
310                 case 'C':
311                         if (string (optarg) == "modified-transitional-1") {
312                                 formulation = dcp::MODIFIED_TRANSITIONAL_1;
313                         } else if (string (optarg) == "dci-any") {
314                                 formulation = dcp::DCI_ANY;
315                         } else if (string (optarg) == "dci-specific") {
316                                 formulation = dcp::DCI_SPECIFIC;
317                         } else {
318                                 error ("unrecognised KDM formulation " + string (optarg));
319                         }
320                 }
321         }
322
323         if (cinemas) {
324                 list<boost::shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
325                 for (list<boost::shared_ptr<Cinema> >::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
326                         cout << (*i)->name << " (" << Emailer::address_list ((*i)->emails) << ")\n";
327                 }
328                 exit (EXIT_SUCCESS);
329         }
330
331         if (!duration_string && !valid_to) {
332                 error ("you must specify a --valid-duration or --valid-to");
333         }
334
335         if (!valid_from) {
336                 error ("you must specify --valid-from");
337                 exit (EXIT_FAILURE);
338         }
339
340         if (optind >= argc) {
341                 help ();
342                 exit (EXIT_FAILURE);
343         }
344
345         if (!cinema_name && !certificate_file) {
346                 error ("you must specify either a cinema, a screen or a certificate file");
347         }
348
349         if (duration_string) {
350                 valid_to = valid_from.get() + duration_from_string (*duration_string);
351         }
352
353         boost::filesystem::path const film_dir = argv[optind];
354
355         dcpomatic_setup_path_encoding ();
356         dcpomatic_setup ();
357
358         if (verbose) {
359                 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
360         }
361
362         from_film (film_dir, verbose, cinema_name, output, certificate_file, *valid_from, *valid_to, formulation, zip);
363
364         return 0;
365 }