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