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