Tweak formatting of help().
[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 <libdcp/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              << "\n"
56              << "<FILM> is the film directory.\n";
57 }
58
59 int
60 main (int argc, char* argv[])
61 {
62         string film_dir;
63         bool progress = true;
64         bool no_remote = false;
65         int log_level = 0;
66
67         int option_index = 0;
68         while (1) {
69                 static struct option long_options[] = {
70                         { "version", no_argument, 0, 'v'},
71                         { "help", no_argument, 0, 'h'},
72                         { "deps", no_argument, 0, 'd'},
73                         { "flags", no_argument, 0, 'f'},
74                         { "no-progress", no_argument, 0, 'n'},
75                         { "no-remote", no_argument, 0, 'r'},
76                         { "log-level", required_argument, 0, 'l' },
77                         { 0, 0, 0, 0 }
78                 };
79
80                 int c = getopt_long (argc, argv, "vhdfnrl:", long_options, &option_index);
81
82                 if (c == -1) {
83                         break;
84                 }
85
86                 switch (c) {
87                 case 'v':
88                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
89                         exit (EXIT_SUCCESS);
90                 case 'h':
91                         help (argv[0]);
92                         exit (EXIT_SUCCESS);
93                 case 'd':
94                         cout << dependency_version_summary () << "\n";
95                         exit (EXIT_SUCCESS);
96                 case 'f':
97                         cout << dcpomatic_cxx_flags << "\n";
98                         exit (EXIT_SUCCESS);
99                 case 'n':
100                         progress = false;
101                         break;
102                 case 'r':
103                         no_remote = true;
104                         break;
105                 case 'l':
106                         log_level = atoi (optarg);
107                         break;
108                 }
109         }
110
111         if (optind >= argc) {
112                 help (argv[0]);
113                 exit (EXIT_FAILURE);
114         }
115
116         film_dir = argv[optind];
117                         
118         dcpomatic_setup ();
119         ui_signaller = new UISignaller ();
120
121         if (no_remote) {
122                 ServerFinder::instance()->disable ();
123         }
124
125         cout << "DCP-o-matic " << dcpomatic_version << " git " << dcpomatic_git_commit;
126         char buf[256];
127         if (gethostname (buf, 256) == 0) {
128                 cout << " on " << buf;
129         }
130         cout << "\n";
131
132         shared_ptr<Film> film;
133         try {
134                 film.reset (new Film (film_dir));
135                 film->read_metadata ();
136         } catch (std::exception& e) {
137                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
138                 exit (EXIT_FAILURE);
139         }
140
141         film->log()->set_level ((Log::Level) log_level);
142
143         cout << "\nMaking DCP for " << film->name() << "\n";
144 //      cout << "Content: " << film->content() << "\n";
145 //      pair<string, string> const f = Filter::ffmpeg_strings (film->filters ());
146 //      cout << "Filters: " << f.first << " " << f.second << "\n";
147
148         film->make_dcp ();
149
150         bool should_stop = false;
151         bool first = true;
152         bool error = false;
153         while (!should_stop) {
154
155                 dcpomatic_sleep (5);
156
157                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
158
159                 if (!first && progress) {
160                         cout << "\033[" << jobs.size() << "A";
161                         cout.flush ();
162                 }
163
164                 first = false;
165
166                 int unfinished = 0;
167                 int finished_in_error = 0;
168
169                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
170                         if (progress) {
171                                 cout << (*i)->name() << ": ";
172                                 
173                                 float const p = (*i)->progress ();
174                                 
175                                 if (p >= 0) {
176                                         cout << (*i)->status() << "                         \n";
177                                 } else {
178                                         cout << ": Running           \n";
179                                 }
180                         }
181
182                         if (!(*i)->finished ()) {
183                                 ++unfinished;
184                         }
185
186                         if ((*i)->finished_in_error ()) {
187                                 ++finished_in_error;
188                                 error = true;
189                         }
190
191                         if (!progress && (*i)->finished_in_error ()) {
192                                 /* We won't see this error if we haven't been showing progress,
193                                    so show it now.
194                                 */
195                                 cout << (*i)->status() << "\n";
196                         }
197                 }
198
199                 if (unfinished == 0 || finished_in_error != 0) {
200                         should_stop = true;
201                 }
202         }
203
204         /* This is just to stop valgrind reporting leaks due to JobManager
205            indirectly holding onto codecs.
206         */
207         JobManager::drop ();
208         
209         return error ? EXIT_FAILURE : EXIT_SUCCESS;
210 }
211
212