2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/tools/dcpomatic_kdm_cli.cc
21 * @brief Command-line program to generate KDMs.
25 #include "lib/cinema.h"
26 #include "lib/screen_kdm.h"
27 #include "lib/cinema_kdms.h"
28 #include "lib/config.h"
29 #include "lib/exceptions.h"
30 #include "lib/emailer.h"
31 #include "lib/safe_stringstream.h"
32 #include <dcp/certificate.h>
41 using boost::shared_ptr;
46 cerr << "Syntax: " << program_name << " [OPTION] [<FILM>]\n"
47 " -h, --help show this help\n"
48 " -o, --output output file or directory\n"
49 " -f, --valid-from valid from time (in local time zone) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
50 " -t, --valid-to valid to time (in local time zone) (e.g. \"2014-09-28 01:41:51\")\n"
51 " -d, --valid-duration valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
52 " --formulation modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
53 " -z, --zip ZIP each cinema's KDMs into its own file\n"
54 " -v, --verbose be verbose\n"
55 " -c, --cinema specify a cinema, either by name or email address\n"
56 " --cinemas list known cinemas from the DCP-o-matic settings\n"
57 " --certificate file containing projector certificate\n\n"
59 "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"
60 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
61 "\tdcpomatic_kdm -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
67 cerr << program_name << ": " << m << "\n";
71 static boost::posix_time::ptime
72 time_from_string (string t)
75 return boost::posix_time::second_clock::local_time ();
78 return boost::posix_time::time_from_string (t);
81 static boost::posix_time::time_duration
82 duration_from_string (string d)
84 SafeStringStream s (d);
90 cerr << "Could not understand duration \"" << d << "\"\n";
94 if (unit == "year" || unit == "years") {
95 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
96 } else if (unit == "week" || unit == "weeks") {
97 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
98 } else if (unit == "day" || unit == "days") {
99 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
100 } else if (unit == "hour" || unit == "hours") {
101 return boost::posix_time::time_duration (N, 0, 0, 0);
104 cerr << "Could not understand duration \"" << d << "\"\n";
108 int main (int argc, char* argv[])
110 boost::filesystem::path output;
111 boost::optional<boost::posix_time::ptime> valid_from;
112 boost::optional<boost::posix_time::ptime> valid_to;
113 string certificate_file;
116 bool cinemas = false;
117 string duration_string;
118 bool verbose = false;
119 dcp::Formulation formulation = dcp::MODIFIED_TRANSITIONAL_1;
121 program_name = argv[0];
123 int option_index = 0;
125 static struct option long_options[] = {
126 { "help", no_argument, 0, 'h'},
127 { "output", required_argument, 0, 'o'},
128 { "valid-from", required_argument, 0, 'f'},
129 { "valid-to", required_argument, 0, 't'},
130 { "certificate", required_argument, 0, 'A' },
131 { "cinema", required_argument, 0, 'c' },
132 { "cinemas", no_argument, 0, 'B' },
133 { "zip", no_argument, 0, 'z' },
134 { "duration", required_argument, 0, 'd' },
135 { "verbose", no_argument, 0, 'v' },
136 { "formulation", required_argument, 0, 'C' },
140 int c = getopt_long (argc, argv, "ho:f:t:c:A:Bzd:vC:", long_options, &option_index);
154 valid_from = time_from_string (optarg);
157 valid_to = time_from_string (optarg);
160 certificate_file = optarg;
163 cinema_name = optarg;
172 duration_string = optarg;
178 if (string (optarg) == "modified-transitional-1") {
179 formulation = dcp::MODIFIED_TRANSITIONAL_1;
180 } else if (string (optarg) == "dci-any") {
181 formulation = dcp::DCI_ANY;
182 } else if (string (optarg) == "dci-specific") {
183 formulation = dcp::DCI_SPECIFIC;
185 error ("unrecognised KDM formulation " + string (optarg));
191 list<boost::shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
192 for (list<boost::shared_ptr<Cinema> >::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
193 cout << (*i)->name << " (" << Emailer::address_list ((*i)->emails) << ")\n";
198 if (duration_string.empty() && !valid_to) {
199 error ("you must specify a --valid-duration or --valid-to");
203 error ("you must specify --valid-from");
207 if (optind >= argc) {
212 if (cinema_name.empty() && certificate_file.empty()) {
213 error ("you must specify either a cinema, a screen or a certificate file");
216 if (!duration_string.empty ()) {
217 valid_to = valid_from.get() + duration_from_string (duration_string);
220 string const film_dir = argv[optind];
222 dcpomatic_setup_path_encoding ();
225 shared_ptr<Film> film;
227 film.reset (new Film (film_dir));
228 film->read_metadata ();
230 cout << "Read film " << film->name () << "\n";
232 } catch (std::exception& e) {
233 cerr << program_name << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
238 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
241 /* XXX: allow specification of this */
242 vector<CPLSummary> cpls = film->cpls ();
244 error ("no CPLs found in film");
245 } else if (cpls.size() > 1) {
246 error ("more than one CPL found in film");
249 boost::filesystem::path cpl = cpls.front().cpl_file;
251 if (cinema_name.empty ()) {
253 if (output.empty ()) {
254 error ("you must specify --output");
257 dcp::Certificate certificate (dcp::file_to_string (certificate_file));
258 dcp::EncryptedKDM kdm = film->make_kdm (certificate, vector<dcp::Certificate>(), cpl, valid_from.get(), valid_to.get(), formulation);
261 cout << "Generated KDM " << output << " for certificate.\n";
265 list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
266 list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
268 i != cinemas.end() &&
269 (*i)->name != cinema_name &&
270 find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
275 if (i == cinemas.end ()) {
276 cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
280 if (output.empty ()) {
285 list<ScreenKDM> screen_kdms = film->make_kdms (
286 (*i)->screens(), cpl, dcp::LocalTime (valid_from.get()), dcp::LocalTime (valid_to.get()), formulation
290 CinemaKDMs::write_zip_files (film->name(), CinemaKDMs::collect (screen_kdms), output);
293 cout << "Wrote ZIP files to " << output << "\n";
296 ScreenKDM::write_files (film->name(), screen_kdms, output);
299 cout << "Wrote KDM files to " << output << "\n";
302 } catch (FileError& e) {
303 cerr << argv[0] << ": " << e.what() << " (" << e.file().string() << ")\n";
305 } catch (KDMError& e) {
306 cerr << argv[0] << ": " << e.what() << "\n";