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