Fix merge.
[dcpomatic.git] / src / tools / dcpomatic_kdm.cc
1 /*
2     Copyright (C) 2013 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 <libdcp/certificates.h>
22 #include "lib/film.h"
23
24 using std::string;
25 using std::cerr;
26 using boost::shared_ptr;
27
28 static void
29 help (string n)
30 {
31         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
32              << "  -h, --help        show this help\n"
33              << "  -o, --output      output filename\n"
34              << "  -f, --valid-from  valid from time (e.g. \"2013-09-28 01:41:51\")\n"
35              << "  -t, --valid-to    valid to time (e.g. \"2014-09-28 01:41:51\")\n"
36              << "  -c, --certificate file containing projector certificate\n";
37 }
38
39 int main (int argc, char* argv[])
40 {
41         string output;
42         string valid_from;
43         string valid_to;
44         string certificate_file;
45         
46         int option_index = 0;
47         while (1) {
48                 static struct option long_options[] = {
49                         { "help", no_argument, 0, 'h'},
50                         { "output", required_argument, 0, 'o'},
51                         { "valid-from", required_argument, 0, 'f'},
52                         { "valid-to", required_argument, 0, 't'},
53                         { "certificate", required_argument, 0, 'c' },
54                         { 0, 0, 0, 0 }
55                 };
56
57                 int c = getopt_long (argc, argv, "ho:f:t:c:", long_options, &option_index);
58
59                 if (c == -1) {
60                         break;
61                 }
62
63                 switch (c) {
64                 case 'h':
65                         help (argv[0]);
66                         exit (EXIT_SUCCESS);
67                 case 'o':
68                         output = optarg;
69                         break;
70                 case 'f':
71                         valid_from = optarg;
72                         break;
73                 case 't':
74                         valid_to = optarg;
75                         break;
76                 case 'c':
77                         certificate_file = optarg;
78                         break;
79                 }
80         }
81
82         if (optind >= argc) {
83                 help (argv[0]);
84                 exit (EXIT_FAILURE);
85         }
86
87         string const film_dir = argv[optind];
88                         
89         dcpomatic_setup ();
90
91         shared_ptr<Film> film;
92         try {
93                 film.reset (new Film (film_dir));
94                 film->read_metadata ();
95         } catch (std::exception& e) {
96                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
97                 exit (EXIT_FAILURE);
98         }
99
100         cerr << "reading " << certificate_file << "\n";
101         shared_ptr<libdcp::Certificate> certificate (new libdcp::Certificate (boost::filesystem::path (certificate_file)));
102         libdcp::KDM kdm = film->make_kdm (
103                 certificate, boost::posix_time::time_from_string (valid_from), boost::posix_time::time_from_string (valid_to)
104                 );
105
106         kdm.as_xml (output);
107         return 0;
108 }