00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <qapplication.h>
00012 #include <qsqldatabase.h>
00013 #include <qfile.h>
00014 #include <qtextstream.h>
00015
00016 #include <unistd.h>
00017
00018 #include <mythtv/exitcodes.h>
00019 #include <mythtv/mythcontext.h>
00020 #include <mythtv/mythdbcon.h>
00021 #include <mythtv/langsettings.h>
00022 #include <mythtv/compat.h>
00023
00024 #include "../mythvideo/dbcheck.h"
00025 #include "mtd.h"
00026
00027 #define MTD_EXIT_DAEMONIZING_ERROR FRONTEND_EXIT_START-1
00028
00029 int main(int argc, char **argv)
00030 {
00031 QApplication a(argc, argv, false);
00032 bool daemon_mode = false;
00033 int special_port = -1;
00034
00035
00036
00037
00038 for(int argpos = 1; argpos < a.argc(); ++argpos)
00039 {
00040
00041 if (!strcmp(a.argv()[argpos],"-d") ||
00042 !strcmp(a.argv()[argpos],"--daemon"))
00043 {
00044 daemon_mode = true;
00045 }
00046 else if (!strcmp(a.argv()[argpos],"-n") ||
00047 !strcmp(a.argv()[argpos],"--nodaemon"))
00048 {
00049 daemon_mode = false;
00050 }
00051 else if (!strcmp(a.argv()[argpos],"-p") ||
00052 !strcmp(a.argv()[argpos],"--port"))
00053 {
00054 if (a.argc() > argpos)
00055 {
00056 QString port_number = a.argv()[argpos+1];
00057 ++argpos;
00058 special_port = port_number.toInt();
00059 if(special_port < 1 ||
00060 special_port > 65534)
00061 {
00062 cerr << "mtd: Bad port number" << endl;
00063 return FRONTEND_EXIT_INVALID_CMDLINE;
00064 }
00065 }
00066 else
00067 {
00068 cerr << "mtd: Missing argument to -p/--port option\n";
00069 return FRONTEND_EXIT_INVALID_CMDLINE;
00070 }
00071 }
00072 else if (!strcmp(a.argv()[argpos],"-v"))
00073 {
00074 if (++argpos >= argc) {
00075 cerr << "Error: -v requires parameters (try -v help)" <<
00076 std::endl;
00077 return GENERIC_EXIT_INVALID_CMDLINE;
00078 }
00079
00080 int err;
00081 if ((err = parse_verbose_arg(a.argv()[argpos])) !=
00082 GENERIC_EXIT_OK)
00083 {
00084 return err;
00085 }
00086 }
00087 else
00088 {
00089 cerr << "Invalid argument: " << a.argv()[argpos] << endl <<
00090 "Valid options are: " << endl <<
00091 "-p or --port number A port number to listen on (default is 2442) " << endl <<
00092 "-d or --daemon Runs mtd as a daemon " << endl <<
00093 "-n or --nodaemon Does not run mtd as a daemon (default)" << endl;
00094 return FRONTEND_EXIT_INVALID_CMDLINE;
00095 }
00096 }
00097
00098
00099
00100
00101
00102 if(daemon_mode)
00103 {
00104 if(daemon(0, 1) < 0)
00105 {
00106 cerr << "mtd: Failed to run as a daemon. Bailing out." << endl ;
00107 return MTD_EXIT_DAEMONIZING_ERROR;
00108 }
00109 cout << endl;
00110 }
00111
00112
00113
00114
00115
00116 gContext = NULL;
00117 gContext = new MythContext(MYTH_BINARY_VERSION);
00118 if (!gContext->Init(false))
00119 {
00120 cerr << "Could not initialize myth context. Exiting." << endl;
00121 return FRONTEND_EXIT_NO_MYTHCONTEXT;
00122 }
00123
00124 if (!MSqlQuery::testDBConnection())
00125 {
00126 cerr << "mtd: Couldn't open database. I go away now. " << endl;
00127 return FRONTEND_EXIT_DB_ERROR;
00128 }
00129
00130 gContext->ActivateSettingsCache(false);
00131 UpgradeVideoDatabaseSchema();
00132 gContext->ActivateSettingsCache(true);
00133
00134
00135
00136
00137
00138 int assigned_port = gContext->GetNumSetting("MTDPort", 2442);
00139 if(special_port > 0)
00140 {
00141 assigned_port = special_port;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150 bool log_stdout = false;
00151 if(gContext->GetNumSetting("MTDLogFlag", 0))
00152 {
00153 log_stdout = true;
00154 }
00155 if(!daemon_mode)
00156 {
00157 log_stdout = true;
00158 }
00159
00160
00161
00162
00163
00164 LanguageSettings::load("mythdvd");
00165
00166 new MTD(assigned_port, log_stdout);
00167
00168 a.exec();
00169
00170 delete gContext;
00171 return FRONTEND_EXIT_OK;
00172 }
00173