1 // Copyright (C) 2011, 2014 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 //  02110-1301, USA.
17 
18 #include <config.h>
19 
20 #include <iostream>
21 #include <stdlib.h>
22 #include "Configuration.h"
23 #include "defs.h"
24 #include "vector.h"
25 #include "ucompose.hpp"
26 #include "ghs-client-tool.h"
27 #include "profile.h"
28 #include "profilelist.h"
29 
30 int max_vector_width;
31 
main(int argc,char * argv[])32 int main(int argc, char* argv[])
33 {
34   Profile *profile = NULL;
35   Glib::ustring host;
36   Glib::ustring file;
37   Glib::ustring unhost;
38   bool show_list = false;
39   bool reload = false;
40   bool terminate = false;
41   srand(time(NULL));         // set the random seed
42 
43   initialize_configuration();
44   Vector<int>::setMaximumWidth(1000);
45 
46   setlocale(LC_ALL, Configuration::s_lang.c_str());
47 
48   int port = 0;
49   Gtk::Main *gtk_main = new Gtk::Main(argc, argv);
50   if (argc > 1)
51     {
52       for (int i = 2; i <= argc; i++)
53 	{
54           Glib::ustring parameter(argv[i-1]);
55 	  if (parameter == "--port" || parameter == "-p")
56 	    {
57 	      i++;
58 	      //convert the next argument
59 	      char* error = 0;
60 	      long userport = strtol(argv[i-1], &error, 10);
61 	      if (error && (*error != '\0'))
62 		{
63                   std::cerr <<_("non-numerical value for --port") <<std::endl;
64 		  exit(-1);
65 		}
66               if (userport > 65535 || userport < 1000)
67                 {
68                   std::cerr <<_("invalid value for --port") <<std::endl;
69 		  exit(-1);
70                 }
71               port = userport;
72 	    }
73           else if (parameter == "--profile" || parameter == "-P")
74             {
75               profile = Profilelist::getInstance()->findProfileById(parameter);
76               if (!profile)
77                 {
78                   std::cerr << _("invalid profile id") << std::endl;
79                   exit(-1);
80                 }
81             }
82           else if (parameter == "--list" || parameter == "-l")
83             {
84               show_list = true;
85             }
86           else if (parameter == "--reload" || parameter == "-R")
87             {
88               reload = true;
89             }
90           else if (parameter == "--host" || parameter == "-h")
91             {
92               file = parameter;
93             }
94           else if (parameter == "--unhost" || parameter == "-u")
95             {
96               unhost = parameter;
97             }
98           else if (parameter == "--terminate" || parameter == "-t")
99             {
100               terminate = true;
101             }
102 	  else if (parameter == "--help" || parameter == "-?")
103 	    {
104               std::cout << Glib::get_prgname() << " " << _("[OPTION]... [HOST]") << std::endl << std::endl;
105               std::cout << "LordsAWar! Game-list Client " << _("version") << " " << VERSION << std::endl << std::endl;
106               std::cout << _("Options:") << std::endl << std::endl;
107               std::cout << "  -?, --help                 " << _("Display this help and exit") <<std::endl;
108               std::cout << "  -P, --profile <id>         " << _("Use this identity, specified by profile id") << std::endl;
109               std::cout << "  -p, --port <number>        " << _("Connect to the server on the given port") << std::endl;
110               std::cout << "  -l, --list                 " << _("See a list of hosted games") << std::endl;
111               std::cout << "  -R, --reload               " << _("Reload the game list from disk") << std::endl;
112               std::cout << "  -u, --unhost <id>          " << _("Stop hosting a game (specified by scenario id)") << std::endl;
113               std::cout << "  -h, --host <file>          " << _("Host a game") << std::endl;
114               std::cout << "  -t, --terminate            " << _("Stop the server") << std::endl;
115               std::cout << std::endl;
116               std::cout << String::ucompose ("%1", _("If HOST is not specified on the command-line, this tool will try to connect to \nthe game-host server at 127.0.0.1.")) << std::endl;
117               std::cout << std::endl;
118               std::cout << _("Report bugs to") << " <" << PACKAGE_BUGREPORT ">." << std::endl;
119 	      exit(0);
120 	    }
121           else
122             host = parameter;
123 	}
124     }
125 
126   if (port == 0)
127     port = LORDSAWAR_GAMEHOST_PORT;
128 
129   if (!show_list && !reload && unhost.empty() && file.empty())
130     {
131       Glib::ustring s =
132         String::ucompose("Try `%1 --help' for more information.",
133                          Glib::get_prgname());
134       std::cout << s << std::endl;
135       return EXIT_SUCCESS;
136     }
137   if (host == "")
138     host = "127.0.0.1";
139   GhsClientTool tool(host, port, profile, show_list, reload, unhost, file,
140                      terminate);
141 
142   gtk_main->run();
143   //delete gtk_main;
144 
145   return EXIT_SUCCESS;
146 }
147