-#include "lib/film.h"
-#include "lib/cinema.h"
-#include "lib/kdm_with_metadata.h"
-#include "lib/config.h"
-#include "lib/exceptions.h"
-#include "lib/emailer.h"
-#include "lib/dkdm_wrapper.h"
-#include "lib/screen.h"
-#include <dcp/certificate.h>
-#include <dcp/decrypted_kdm.h>
-#include <dcp/encrypted_kdm.h>
-#include <getopt.h>
-#include <iostream>
-
-using std::string;
-using std::cout;
-using std::cerr;
-using std::list;
-using std::vector;
-using std::runtime_error;
-using boost::shared_ptr;
-using boost::optional;
-using boost::bind;
-using boost::dynamic_pointer_cast;
-#if BOOST_VERSION >= 106100
-using namespace boost::placeholders;
-#endif
-using namespace dcpomatic;
-
-
-static void
-help ()
-{
- cerr << "Syntax: " << program_name << " [OPTION] <FILM|CPL-ID|DKDM>\n"
- " -h, --help show this help\n"
- " -o, --output output file or directory\n"
- " -K, --filename-format filename format for KDMs\n"
- " -Z, --container-name-format filename format for ZIP containers\n"
- " -f, --valid-from valid from time (in local time zone of the cinema) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
- " -t, --valid-to valid to time (in local time zone of the cinema) (e.g. \"2014-09-28 01:41:51\")\n"
- " -d, --valid-duration valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
- " -F, --formulation modified-transitional-1, multiple-modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
- " -a, --disable-forensic-marking-picture disable forensic marking of pictures essences\n"
- " -a, --disable-forensic-marking-audio disable forensic marking of audio essences (optionally above a given channel, e.g 12)\n"
- " -z, --zip ZIP each cinema's KDMs into its own file\n"
- " -v, --verbose be verbose\n"
- " -c, --cinema specify a cinema, either by name or email address\n"
- " -S, --screen screen description\n"
- " -C, --certificate file containing projector certificate\n"
- " -T, --trusted-device file containing a trusted device's certificate\n"
- " --list-cinemas list known cinemas from the DCP-o-matic settings\n"
- " --list-dkdm-cpls list CPLs for which DCP-o-matic has DKDMs\n\n"
- "CPL-ID must be the ID of a CPL that is mentioned in DCP-o-matic's DKDM list.\n\n"
- "For example:\n\n"
- "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"
- "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
- "\t" << program_name << " -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
-}
-
-static void
-error (string m)
-{
- cerr << program_name << ": " << m << "\n";
- exit (EXIT_FAILURE);
-}
-
-static boost::posix_time::ptime
-time_from_string (string t)
-{
- if (t == "now") {
- return boost::posix_time::second_clock::local_time ();
- }
-
- return boost::posix_time::time_from_string (t);
-}
-
-static boost::posix_time::time_duration
-duration_from_string (string d)
-{
- int N;
- char unit_buf[64] = "\0";
- sscanf (d.c_str(), "%d %63s", &N, unit_buf);
- string const unit (unit_buf);
-
- if (N == 0) {
- cerr << "Could not understand duration \"" << d << "\"\n";
- exit (EXIT_FAILURE);
- }
-
- if (unit == "year" || unit == "years") {
- return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
- } else if (unit == "week" || unit == "weeks") {
- return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
- } else if (unit == "day" || unit == "days") {
- return boost::posix_time::time_duration (N * 24, 0, 0, 0);
- } else if (unit == "hour" || unit == "hours") {
- return boost::posix_time::time_duration (N, 0, 0, 0);
- }
-
- cerr << "Could not understand duration \"" << d << "\"\n";
- exit (EXIT_FAILURE);
-}
-
-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)
-{
- list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
- list<shared_ptr<Cinema> >::const_iterator 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.reset (new 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");
- }
-
- boost::filesystem::path cpl = cpls.front().cpl_file;
-
- try {
- list<KDMWithMetadataPtr> kdms;
- BOOST_FOREACH (shared_ptr<Screen> i, screens) {
- KDMWithMetadataPtr 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)
-{
- BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
- shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
- if (g) {
- optional<dcp::EncryptedKDM> dkdm = sub_find_dkdm (g, cpl_id);
- if (dkdm) {
- return dkdm;
- }
- } else {
- shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
- assert (d);
- if (d->dkdm().cpl_id() == cpl_id) {
- return d->dkdm();
- }
- }
- }