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