Move log environment dump stuff out into its own file.
[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              << "  -f, --flags        show flags passed to C++ compiler on build\n"
52              << "  -n, --no-progress  do not print progress to stdout\n"
53              << "  -r, --no-remote    do not use any remote servers\n"
54              << "  -k, --keep-going   keep running even when the job is complete\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         bool keep_going = false;
66
67         int option_index = 0;
68         while (true) {
69                 static struct option long_options[] = {
70                         { "version", no_argument, 0, 'v'},
71                         { "help", no_argument, 0, 'h'},
72                         { "flags", no_argument, 0, 'f'},
73                         { "no-progress", no_argument, 0, 'n'},
74                         { "no-remote", no_argument, 0, 'r'},
75                         { "keep-going", no_argument, 0, 'k' },
76                         { 0, 0, 0, 0 }
77                 };
78
79                 int c = getopt_long (argc, argv, "vhfnrk", long_options, &option_index);
80
81                 if (c == -1) {
82                         break;
83                 }
84
85                 switch (c) {
86                 case 'v':
87                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
88                         exit (EXIT_SUCCESS);
89                 case 'h':
90                         help (argv[0]);
91                         exit (EXIT_SUCCESS);
92                 case 'f':
93                         cout << dcpomatic_cxx_flags << "\n";
94                         exit (EXIT_SUCCESS);
95                 case 'n':
96                         progress = false;
97                         break;
98                 case 'r':
99                         no_remote = true;
100                         break;
101                 case 'k':
102                         keep_going = true;
103                         break;
104                 }
105         }
106
107         if (optind >= argc) {
108                 help (argv[0]);
109                 exit (EXIT_FAILURE);
110         }
111
112         film_dir = argv[optind];
113                         
114         dcpomatic_setup ();
115         ui_signaller = new UISignaller ();
116
117         if (no_remote) {
118                 ServerFinder::instance()->disable ();
119         }
120
121         cout << "DCP-o-matic " << dcpomatic_version << " git " << dcpomatic_git_commit;
122         char buf[256];
123         if (gethostname (buf, 256) == 0) {
124                 cout << " on " << buf;
125         }
126         cout << "\n";
127
128         shared_ptr<Film> film;
129         try {
130                 film.reset (new Film (film_dir));
131                 film->read_metadata ();
132         } catch (std::exception& e) {
133                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
134                 exit (EXIT_FAILURE);
135         }
136
137         ContentList content = film->content ();
138         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
139                 vector<boost::filesystem::path> paths = (*i)->paths ();
140                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
141                         if (!boost::filesystem::exists (*j)) {
142                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
143                                 exit (EXIT_FAILURE);
144                         }
145                 }
146         }
147                 
148         cout << "\nMaking DCP for " << film->name() << "\n";
149
150         film->make_dcp ();
151
152         bool should_stop = false;
153         bool first = true;
154         bool error = false;
155         while (!should_stop) {
156
157                 dcpomatic_sleep (5);
158
159                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
160
161                 if (!first && progress) {
162                         cout << "\033[" << jobs.size() << "A";
163                         cout.flush ();
164                 }
165
166                 first = false;
167
168                 int unfinished = 0;
169                 int finished_in_error = 0;
170
171                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
172                         if (progress) {
173                                 cout << (*i)->name() << ": ";
174                                 
175                                 if ((*i)->progress ()) {
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         if (keep_going) {
205                 while (true) {
206                         dcpomatic_sleep (3600);
207                 }
208         }
209
210         /* This is just to stop valgrind reporting leaks due to JobManager
211            indirectly holding onto codecs.
212         */
213         JobManager::drop ();
214
215         ServerFinder::drop ();
216         
217         return error ? EXIT_FAILURE : EXIT_SUCCESS;
218 }
219
220