Set up for the correct handling of UTF-8 with Windows
[dcpomatic.git] / src / tools / dcpomatic_server_cli.cc
1 /*
2     Copyright (C) 2012-2015 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 "lib/server.h"
21 #include <iostream>
22 #include <stdexcept>
23 #include <cstring>
24 #include <vector>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <boost/array.hpp>
29 #include <boost/asio.hpp>
30 #include <boost/algorithm/string.hpp>
31 #include <boost/thread.hpp>
32 #include <boost/thread/mutex.hpp>
33 #include <boost/thread/condition.hpp>
34 #include "lib/config.h"
35 #include "lib/dcp_video.h"
36 #include "lib/exceptions.h"
37 #include "lib/util.h"
38 #include "lib/config.h"
39 #include "lib/image.h"
40 #include "lib/log.h"
41 #include "lib/version.h"
42
43 using std::cerr;
44 using std::string;
45 using std::cout;
46 using boost::shared_ptr;
47
48 static void
49 help (string n)
50 {
51         cerr << "Syntax: " << n << " [OPTION]\n"
52              << "  -v, --version      show DCP-o-matic version\n"
53              << "  -h, --help         show this help\n"
54              << "  -t, --threads      number of parallel encoding threads to use\n"
55              << "  --verbose          be verbose to stdout\n"
56              << "  --log              write a log file of activity\n";
57 }
58
59 int
60 main (int argc, char* argv[])
61 {
62         dcpomatic_setup_path_encoding ();
63         dcpomatic_setup ();
64
65         int num_threads = Config::instance()->num_local_encoding_threads ();
66         bool verbose = false;
67         bool write_log = false;
68
69         int option_index = 0;
70         while (true) {
71                 static struct option long_options[] = {
72                         { "version", no_argument, 0, 'v'},
73                         { "help", no_argument, 0, 'h'},
74                         { "threads", required_argument, 0, 't'},
75                         { "verbose", no_argument, 0, 'A'},
76                         { "log", no_argument, 0, 'B'},
77                         { 0, 0, 0, 0 }
78                 };
79
80                 int c = getopt_long (argc, argv, "vht:AB", 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 't':
94                         num_threads = atoi (optarg);
95                         break;
96                 case 'A':
97                         verbose = true;
98                         break;
99                 case 'B':
100                         write_log = true;
101                         break;
102                 }
103         }
104
105         shared_ptr<Log> log;
106         if (write_log) {
107                 log.reset (new FileLog ("dcpomatic_server_cli.log"));
108         } else {
109                 log.reset (new NullLog);
110         }
111
112         Server server (log, verbose);
113
114         try {
115                 server.run (num_threads);
116         } catch (boost::system::system_error& e) {
117                 if (e.code() == boost::system::errc::address_in_use) {
118                         cerr << argv[0] << ": address already in use.  Is another DCP-o-matic server instance already running?\n";
119                         exit (EXIT_FAILURE);
120                 }
121                 cerr << argv[0] << ": " << e.what() << "\n";
122         }
123         return 0;
124 }