+
+static bool
+always_overwrite ()
+{
+ return true;
+}
+
+
+void
+write_files (
+ list<KDMWithMetadataPtr> kdms,
+ bool zip,
+ boost::filesystem::path output,
+ dcp::NameFormat container_name_format,
+ dcp::NameFormat filename_format,
+ bool verbose
+ )
+{
+ if (zip) {
+ int const N = write_zip_files (
+ collect (kdms),
+ output,
+ container_name_format,
+ filename_format,
+ bind (&always_overwrite)
+ );
+
+ if (verbose) {
+ cout << "Wrote " << N << " ZIP files to " << output << "\n";
+ }
+ } else {
+ int const N = write_files (
+ kdms, output, filename_format,
+ bind (&always_overwrite)
+ );
+
+ if (verbose) {
+ cout << "Wrote " << N << " KDM files to " << output << "\n";
+ }
+ }
+}
+
+
+shared_ptr<Cinema>
+find_cinema (string cinema_name)
+{
+ auto cinemas = Config::instance()->cinemas ();
+ auto i = cinemas.begin();
+ while (
+ i != cinemas.end() &&
+ (*i)->name != cinema_name &&
+ find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
+
+ ++i;
+ }
+
+ if (i == cinemas.end ()) {
+ cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
+ exit (EXIT_FAILURE);
+ }
+
+ return *i;
+}
+
+
+void
+from_film (
+ list<shared_ptr<Screen>> screens,
+ boost::filesystem::path film_dir,
+ bool verbose,
+ boost::filesystem::path output,
+ dcp::NameFormat container_name_format,
+ dcp::NameFormat filename_format,
+ boost::posix_time::ptime valid_from,
+ boost::posix_time::ptime valid_to,
+ dcp::Formulation formulation,
+ bool disable_forensic_marking_picture,
+ optional<int> disable_forensic_marking_audio,
+ bool zip
+ )
+{
+ shared_ptr<Film> film;
+ try {
+ film = make_shared<Film>(film_dir);
+ film->read_metadata ();
+ if (verbose) {
+ cout << "Read film " << film->name () << "\n";
+ }
+ } catch (std::exception& e) {
+ cerr << program_name << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
+ exit (EXIT_FAILURE);
+ }
+
+ /* XXX: allow specification of this */
+ vector<CPLSummary> cpls = film->cpls ();
+ if (cpls.empty ()) {
+ error ("no CPLs found in film");
+ } else if (cpls.size() > 1) {
+ error ("more than one CPL found in film");
+ }
+
+ auto cpl = cpls.front().cpl_file;
+
+ try {
+ list<KDMWithMetadataPtr> kdms;
+ for (auto i: screens) {
+ auto p = kdm_for_screen (film, cpl, i, valid_from, valid_to, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio);
+ if (p) {
+ kdms.push_back (p);
+ }
+ }
+ write_files (kdms, zip, output, container_name_format, filename_format, verbose);
+ } catch (FileError& e) {
+ cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
+ exit (EXIT_FAILURE);
+ } catch (KDMError& e) {
+ cerr << program_name << ": " << e.what() << "\n";
+ exit (EXIT_FAILURE);
+ } catch (runtime_error& e) {
+ cerr << program_name << ": " << e.what() << "\n";
+ exit (EXIT_FAILURE);
+ }
+}
+
+
+optional<dcp::EncryptedKDM>
+sub_find_dkdm (shared_ptr<DKDMGroup> group, string cpl_id)
+{
+ for (auto i: group->children()) {
+ auto g = dynamic_pointer_cast<DKDMGroup>(i);
+ if (g) {
+ auto dkdm = sub_find_dkdm (g, cpl_id);
+ if (dkdm) {
+ return dkdm;
+ }
+ } else {
+ auto d = dynamic_pointer_cast<DKDM>(i);
+ assert (d);
+ if (d->dkdm().cpl_id() == cpl_id) {
+ return d->dkdm();
+ }
+ }
+ }
+
+ return optional<dcp::EncryptedKDM>();
+}
+
+
+optional<dcp::EncryptedKDM>
+find_dkdm (string cpl_id)
+{
+ return sub_find_dkdm (Config::instance()->dkdms(), cpl_id);
+}
+
+
+dcp::EncryptedKDM
+kdm_from_dkdm (
+ dcp::DecryptedKDM dkdm,
+ dcp::Certificate target,
+ vector<string> trusted_devices,
+ dcp::LocalTime valid_from,
+ dcp::LocalTime valid_to,
+ dcp::Formulation formulation,
+ bool disable_forensic_marking_picture,
+ optional<int> disable_forensic_marking_audio
+ )
+{
+ /* Signer for new KDM */
+ auto signer = Config::instance()->signer_chain ();
+ if (!signer->valid ()) {
+ error ("signing certificate chain is invalid.");
+ }
+
+ /* Make a new empty KDM and add the keys from the DKDM to it */
+ dcp::DecryptedKDM kdm (
+ valid_from,
+ valid_to,
+ dkdm.annotation_text().get_value_or(""),
+ dkdm.content_title_text(),
+ dcp::LocalTime().as_string()
+ );
+
+ for (auto const& j: dkdm.keys()) {
+ kdm.add_key(j);
+ }
+
+ return kdm.encrypt (signer, target, trusted_devices, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio);
+}
+
+
+void
+from_dkdm (
+ list<shared_ptr<Screen>> screens,
+ dcp::DecryptedKDM dkdm,
+ bool verbose,
+ boost::filesystem::path output,
+ dcp::NameFormat container_name_format,
+ dcp::NameFormat filename_format,
+ boost::posix_time::ptime valid_from,
+ boost::posix_time::ptime valid_to,
+ dcp::Formulation formulation,
+ bool disable_forensic_marking_picture,
+ optional<int> disable_forensic_marking_audio,
+ bool zip
+ )
+{
+ dcp::NameFormat::Map values;
+
+ try {
+ list<KDMWithMetadataPtr> kdms;
+ for (auto i: screens) {
+ if (!i->recipient) {
+ continue;
+ }
+
+ dcp::LocalTime begin(valid_from, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute());
+ dcp::LocalTime end(valid_to, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute());
+
+ auto const kdm = kdm_from_dkdm(
+ dkdm,
+ i->recipient.get(),
+ i->trusted_device_thumbprints(),
+ begin,
+ end,
+ formulation,
+ disable_forensic_marking_picture,
+ disable_forensic_marking_audio
+ );
+
+ dcp::NameFormat::Map name_values;
+ name_values['c'] = i->cinema->name;
+ name_values['s'] = i->name;
+ name_values['f'] = dkdm.annotation_text().get_value_or("");
+ name_values['b'] = begin.date() + " " + begin.time_of_day(true, false);
+ name_values['e'] = end.date() + " " + end.time_of_day(true, false);
+ name_values['i'] = kdm.cpl_id();
+
+ kdms.push_back (make_shared<KDMWithMetadata>(name_values, i->cinema.get(), i->cinema->emails, kdm));
+ }
+ write_files (kdms, zip, output, container_name_format, filename_format, verbose);
+ } catch (FileError& e) {
+ cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
+ exit (EXIT_FAILURE);
+ } catch (KDMError& e) {
+ cerr << program_name << ": " << e.what() << "\n";
+ exit (EXIT_FAILURE);
+ }
+}
+
+
+void
+dump_dkdm_group (shared_ptr<DKDMGroup> group, int indent)
+{
+ if (indent > 0) {
+ for (int i = 0; i < indent; ++i) {
+ cout << " ";
+ }
+ cout << group->name() << "\n";
+ }
+ for (auto i: group->children()) {
+ auto g = dynamic_pointer_cast<DKDMGroup>(i);
+ if (g) {
+ dump_dkdm_group (g, indent + 2);
+ } else {
+ for (int j = 0; j < indent; ++j) {
+ cout << " ";
+ }
+ auto d = dynamic_pointer_cast<DKDM>(i);
+ assert(d);
+ cout << d->dkdm().cpl_id() << "\n";
+ }
+ }
+}
+
+