Merge master.
[dcpomatic.git] / src / tools / dcpomatic_cli.cc
1 /*
2     Copyright (C) 2012 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 <iostream>
21 #include <iomanip>
22 #include <getopt.h>
23 #include <dcp/version.h>
24 #include "lib/film.h"
25 #include "lib/filter.h"
26 #include "lib/transcode_job.h"
27 #include "lib/job_manager.h"
28 #include "lib/util.h"
29 #include "lib/scaler.h"
30 #include "lib/version.h"
31 #include "lib/cross.h"
32 #include "lib/config.h"
33 #include "lib/log.h"
34 #include "lib/ui_signaller.h"
35 #include "lib/server_finder.h"
36
37 using std::string;
38 using std::cerr;
39 using std::cout;
40 using std::vector;
41 using std::pair;
42 using std::list;
43 using boost::shared_ptr;
44
45 static void
46 help (string n)
47 {
48         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
49              << "  -v, --version      show DCP-o-matic version\n"
50              << "  -h, --help         show this help\n"
51              << "  -d, --deps         list DCP-o-matic dependency details and quit\n"
52              << "  -f, --flags        show flags passed to C++ compiler on build\n"
53              << "  -n, --no-progress  do not print progress to stdout\n"
54              << "  -r, --no-remote    do not use any remote servers\n"
55              << "  -k, --keep-going   keep running even when the job is complete\n"
56              << "\n"
57              << "<FILM> is the film directory.\n";
58 }
59
60 int
61 main (int argc, char* argv[])
62 {
63         string film_dir;
64         bool progress = true;
65         bool no_remote = false;
66         bool keep_going = false;
67
68         int option_index = 0;
69         while (true) {
70                 static struct option long_options[] = {
71                         { "version", no_argument, 0, 'v'},
72                         { "help", no_argument, 0, 'h'},
73                         { "deps", no_argument, 0, 'd'},
74                         { "flags", no_argument, 0, 'f'},
75                         { "no-progress", no_argument, 0, 'n'},
76                         { "no-remote", no_argument, 0, 'r'},
77                         { "keep-going", no_argument, 0, 'k' },
78                         { 0, 0, 0, 0 }
79                 };
80
81                 int c = getopt_long (argc, argv, "vhdfnrk", long_options, &option_index);
82
83                 if (c == -1) {
84                         break;
85                 }
86
87                 switch (c) {
88                 case 'v':
89                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
90                         exit (EXIT_SUCCESS);
91                 case 'h':
92                         help (argv[0]);
93                         exit (EXIT_SUCCESS);
94                 case 'd':
95                         cout << dependency_version_summary () << "\n";
96                         exit (EXIT_SUCCESS);
97                 case 'f':
98                         cout << dcpomatic_cxx_flags << "\n";
99                         exit (EXIT_SUCCESS);
100                 case 'n':
101                         progress = false;
102                         break;
103                 case 'r':
104                         no_remote = true;
105                         break;
106                 case 'k':
107                         keep_going = true;
108                         break;
109                 }
110         }
111
112         if (optind >= argc) {
113                 help (argv[0]);
114                 exit (EXIT_FAILURE);
115         }
116
117         film_dir = argv[optind];
118                         
119         dcpomatic_setup ();
120         ui_signaller = new UISignaller ();
121
122         if (no_remote) {
123                 ServerFinder::instance()->disable ();
124         }
125
126         cout << "DCP-o-matic " << dcpomatic_version << " git " << dcpomatic_git_commit;
127         char buf[256];
128         if (gethostname (buf, 256) == 0) {
129                 cout << " on " << buf;
130         }
131         cout << "\n";
132
133         shared_ptr<Film> film;
134         try {
135                 film.reset (new Film (film_dir));
136                 film->read_metadata ();
137         } catch (std::exception& e) {
138                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
139                 exit (EXIT_FAILURE);
140         }
141
142         cout << "\nMaking DCP for " << film->name() << "\n";
143
144         film->make_dcp ();
145
146         bool should_stop = false;
147         bool first = true;
148         bool error = false;
149         while (!should_stop) {
150
151                 dcpomatic_sleep (5);
152
153                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
154
155                 if (!first && progress) {
156                         cout << "\033[" << jobs.size() << "A";
157                         cout.flush ();
158                 }
159
160                 first = false;
161
162                 int unfinished = 0;
163                 int finished_in_error = 0;
164
165                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
166                         if (progress) {
167                                 cout << (*i)->name() << ": ";
168                                 
169                                 float const p = (*i)->progress ();
170                                 
171                                 if (p >= 0) {
172                                         cout << (*i)->status() << "                         \n";
173                                 } else {
174                                         cout << ": Running           \n";
175                                 }
176                         }
177
178                         if (!(*i)->finished ()) {
179                                 ++unfinished;
180                         }
181
182                         if ((*i)->finished_in_error ()) {
183                                 ++finished_in_error;
184                                 error = true;
185                         }
186
187                         if (!progress && (*i)->finished_in_error ()) {
188                                 /* We won't see this error if we haven't been showing progress,
189                                    so show it now.
190                                 */
191                                 cout << (*i)->status() << "\n";
192                         }
193                 }
194
195                 if (unfinished == 0 || finished_in_error != 0) {
196                         should_stop = true;
197                 }
198         }
199
200         if (keep_going) {
201                 while (true) {
202                         dcpomatic_sleep (3600);
203                 }
204         }
205
206         /* This is just to stop valgrind reporting leaks due to JobManager
207            indirectly holding onto codecs.
208         */
209         JobManager::drop ();
210         
211         return error ? EXIT_FAILURE : EXIT_SUCCESS;
212 }
213
214