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