Add nascent dcpomatic_create.
[dcpomatic.git] / src / tools / dcpomatic_create.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 <string>
21 #include <iostream>
22 #include <cstdlib>
23 #include <getopt.h>
24 #include <boost/filesystem.hpp>
25 #include "lib/version.h"
26 #include "lib/film.h"
27 #include "lib/util.h"
28 #include "lib/content_factory.h"
29 #include "lib/job_manager.h"
30 #include "lib/ui_signaller.h"
31 #include "lib/job.h"
32
33 using std::string;
34 using std::cout;
35 using std::cerr;
36 using std::list;
37 using boost::shared_ptr;
38
39 static void
40 help (string n)
41 {
42         cerr << "Create a film directory (ready for making a DCP) from some content files.\n"
43              << "Syntax: " << n << " [OPTION] <CONTENT> [<CONTENT> ...]\n"
44              << "  -v, --version   show DCP-o-matic version\n"
45              << "  -h, --help      show this help\n"
46              << "  -n, --name      film name\n"
47              << "  -o, --output    output directory (required)\n";
48 }
49
50 int
51 main (int argc, char* argv[])
52 {
53         string name;
54         boost::filesystem::path output;
55         
56         int option_index = 0;
57         while (1) {
58                 static struct option long_options[] = {
59                         { "version", no_argument, 0, 'v'},
60                         { "help", no_argument, 0, 'h'},
61                         { "name", required_argument, 0, 'n'},
62                         { "output", required_argument, 0, 'o'},
63                         { 0, 0, 0, 0}
64                 };
65
66                 int c = getopt_long (argc, argv, "vhn:o:", long_options, &option_index);
67                 if (c == -1) {
68                         break;
69                 }
70
71                 switch (c) {
72                 case 'v':
73                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
74                         exit (EXIT_SUCCESS);
75                 case 'h':
76                         help (argv[0]);
77                         exit (EXIT_SUCCESS);
78                 case 'n':
79                         name = optarg;
80                         break;
81                 case 'o':
82                         output = optarg;
83                         break;
84                 }
85         }
86
87         if (optind > argc) {
88                 help (argv[0]);
89                 exit (EXIT_FAILURE);
90         }
91
92         if (output.empty ()) {
93                 cerr << "Missing required option -o or --output.\n"
94                      << "Use " << argv[0] << " --help for help.\n";
95                 exit (EXIT_FAILURE);
96         }
97
98         dcpomatic_setup ();
99         ui_signaller = new UISignaller ();
100
101         shared_ptr<Film> film (new Film (output));
102         if (!name.empty ()) {
103                 film->set_name (name);
104         }
105
106         for (int i = optind; i < argc; ++i) {
107                 film->examine_and_add_content (content_factory (film, argv[i]));
108         }
109
110         JobManager* jm = JobManager::instance ();
111         while (jm->work_to_do ()) {
112                 ui_signaller->ui_idle ();
113         }
114
115         if (jm->errors ()) {
116                 list<shared_ptr<Job> > jobs = jm->get ();
117                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
118                         if ((*i)->finished_in_error ()) {
119                                 cerr << (*i)->error_summary () << "\n"
120                                      << (*i)->error_details () << "\n";
121                         }
122                 }
123                 exit (EXIT_FAILURE);
124         }
125
126         film->write_metadata ();
127         return 0;
128 }