1 | #include <iostream>
|
---|
2 | #include <fstream>
|
---|
3 | #include <iomanip>
|
---|
4 | #include <string.h>
|
---|
5 | #include <unistd.h>
|
---|
6 | #include <cstdlib>
|
---|
7 | #include <sstream>
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | #ifndef CDROM
|
---|
11 | #include "wrapper.h"
|
---|
12 | #include "libsearch4j.h"
|
---|
13 | bool extract_bundled_file( const char[], int, char*, bool );
|
---|
14 | #else
|
---|
15 | bool prep_java( string wd );
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | void usage( char* progname, int exitvalue = -1 ) {
|
---|
19 | cerr
|
---|
20 | << "Usage: " << progname << " [OPTIONS]" << endl
|
---|
21 | << "OPTIONS: " << endl
|
---|
22 | << " -h, -help Display this usage message" << endl
|
---|
23 | << " -textonly Do a non-graphical command line installation" << endl
|
---|
24 | << " -extract Extract the jar file, this can " << endl;
|
---|
25 | #ifdef CDROM
|
---|
26 | cerr << " -wd PATH Look for the CD-ROM at PATH" << endl;
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | exit( exitvalue );
|
---|
30 | }
|
---|
31 |
|
---|
32 | int main(int argc, char** argv) {
|
---|
33 |
|
---|
34 | char wdc[1024];
|
---|
35 | getcwd(wdc, sizeof(wdc));
|
---|
36 | string wd = "";
|
---|
37 | wd.append( wdc );
|
---|
38 |
|
---|
39 | string scratch = (getenv("TMPDIR") == NULL) ? "/tmp" : getenv("TMPDIR");
|
---|
40 | string tempdir = scratch + "/@[email protected]"; //temporary directory where we will store extracted files
|
---|
41 | bool succeeded = false;
|
---|
42 | bool text_mode = false;
|
---|
43 |
|
---|
44 | //parse arguments
|
---|
45 | string a;
|
---|
46 | for ( int i=1; i<argc; i++ ) {
|
---|
47 | a = argv[i];
|
---|
48 | if ( a.compare("-wd") == 0 ) {
|
---|
49 | wd = "";
|
---|
50 | wd.append( argv[++i] );
|
---|
51 | } else if ( a.compare("-h") == 0 || a.compare("-help") == 0 || a.compare("--help") == 0) {
|
---|
52 | usage( argv[0], 0 );
|
---|
53 | } else if ( a.compare("-textonly") == 0 || a.compare("-text") == 0 || a.compare("text") == 0 ) {
|
---|
54 | //cerr << "-text is deprecated, please use -extract and then run \"java -jar @[email protected] text\"" << endl;
|
---|
55 | text_mode = true;
|
---|
56 | #ifndef CDROM
|
---|
57 | } else if ( a.compare("-extract") == 0 || a.compare("--extract") || a.compare("-x")) {
|
---|
58 | cout << "----------------------------" << endl;
|
---|
59 | cout << "Extracting java installer..." << endl;
|
---|
60 | cout << "----------------------------" << endl;
|
---|
61 | bool result = extract_bundled_file( @projectname@jar, sizeof(@projectname@jar), "@[email protected]", false);
|
---|
62 | if(result){
|
---|
63 | cout << "\nExtraction Complete" << endl;
|
---|
64 | cout << "You can now run \"java -jar @[email protected] text\" to run the installer from the command line\n" << endl;
|
---|
65 | return 0;
|
---|
66 | }
|
---|
67 | else{
|
---|
68 | cerr << "\nThere was an error while extracting the java installer" << endl;
|
---|
69 | return 1;
|
---|
70 | }
|
---|
71 | #endif
|
---|
72 | } else {
|
---|
73 | cerr << "Unrecognised option '" << a << "'" << endl;
|
---|
74 | usage(argv[0]);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | #ifdef CDROM
|
---|
79 | //ensure working directory is correct
|
---|
80 | FILE* pFile;
|
---|
81 | pFile = fopen( (wd+"/Software/core/all/uninst.jar").c_str(), "r");
|
---|
82 | if ( pFile == NULL ) {
|
---|
83 | cerr << "Could not find cdrom at '" << wd << "'." << endl;
|
---|
84 | cerr << "Please enter the path the CDROM [/media/cdrom]: ";
|
---|
85 | getline(cin, wd);
|
---|
86 | if ( wd.compare("") == 0 ) {
|
---|
87 | wd = "/media/cdrom";
|
---|
88 | }
|
---|
89 | } else {
|
---|
90 | fclose(pFile);
|
---|
91 | }
|
---|
92 |
|
---|
93 | pFile = fopen( (wd+"/Software/core/all/uninst.jar").c_str(), "r");
|
---|
94 | if ( pFile == NULL ) {
|
---|
95 | cerr << "Could not find cdrom at '"<< wd << "'." << endl;
|
---|
96 | cerr << "Please run the installer again" << endl;
|
---|
97 | exit(-1);
|
---|
98 | } else {
|
---|
99 | fclose(pFile);
|
---|
100 | }
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | //create the temp folder
|
---|
104 | cout << "Creating temp directory..." << endl;
|
---|
105 | succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
|
---|
106 | if ( !succeeded ) {
|
---|
107 | cerr << "Failed to create the temp directory '" << tempdir << "'. " <<
|
---|
108 | "Check that it does not already exist. Also check that you have write " <<
|
---|
109 | "permissions to '" << scratch << "', or set the environment variable TMPDIR to " <<
|
---|
110 | "a directory where you do have write permissions." << endl <<
|
---|
111 | "Exiting" << endl;
|
---|
112 | return 1;
|
---|
113 | }
|
---|
114 |
|
---|
115 | #ifndef CDROM
|
---|
116 |
|
---|
117 | string jarfile = tempdir + "/@[email protected]"; //where we will store the jar file
|
---|
118 | string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
|
---|
119 |
|
---|
120 |
|
---|
121 | //extract files
|
---|
122 | cout << "Extracting installer jar..." << endl;
|
---|
123 | succeeded = extract_bundled_file( @projectname@jar, sizeof(@projectname@jar), (char*)jarfile.c_str(), false);
|
---|
124 |
|
---|
125 | #ifdef java_is_bundled
|
---|
126 | cout << "Preparing @projectname@ installer..." << endl;
|
---|
127 | succeeded = extract_bundled_file( java, sizeof(java), (char*)javafile.c_str(), true ) && succeeded;
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | if ( !succeeded ) {
|
---|
131 | cerr << "Failed to extract one or more resources" << endl;
|
---|
132 | cerr << "This installer may not have sufficient file permissions to write it to disk" << endl;
|
---|
133 | cerr << "Or, the files may be corrupt or missing from this executable" << endl;
|
---|
134 | cerr << "Exiting" << endl;
|
---|
135 | return 1;
|
---|
136 | }
|
---|
137 |
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | //change to the temp directory
|
---|
141 | chdir( tempdir.c_str() );
|
---|
142 |
|
---|
143 |
|
---|
144 | #ifndef CDROM
|
---|
145 | #ifdef java_is_bundled
|
---|
146 | succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0);
|
---|
147 | // succeeded = succeeded && ( system( "tar -xf jre.tar" ) == 0 );
|
---|
148 | if ( !succeeded ) {
|
---|
149 | cout << "Failed to extract the bundled java archive to the temp directory" << endl;
|
---|
150 | // cout << "You need the tar program on your PATH" << endl;
|
---|
151 | cout << "Exiting" << endl;
|
---|
152 | return 1;
|
---|
153 | }
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | //check if an appropriate java is found
|
---|
157 | Jvm minimum; minimum.setVersionFromString( "@java.min.version@" );
|
---|
158 | string phint = "./@java.extracted@";
|
---|
159 | string hint = "";
|
---|
160 | Jvm foundJvm;
|
---|
161 | bool jvmFound = find( foundJvm, true, minimum, false, false, phint, hint, false );
|
---|
162 |
|
---|
163 | //if the jvm was not found, report not found
|
---|
164 | if ( !jvmFound ) {
|
---|
165 |
|
---|
166 | //did not find a good java
|
---|
167 | cout << "@projectname@ requires java @java.min.version@ or greater." << endl;
|
---|
168 |
|
---|
169 | //tell them if java is absent or just too old
|
---|
170 | Jvm tmpJvm;
|
---|
171 | if ( find( tmpJvm, false, minimum, false, false, phint, hint, false ) ) {
|
---|
172 | cout << "You have java, but it is too old." << endl;
|
---|
173 | } else {
|
---|
174 | cout << "Could not find java on your system." << endl;
|
---|
175 | }
|
---|
176 |
|
---|
177 | cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl;
|
---|
178 | #ifndef java_is_bundled
|
---|
179 | cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl;
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | //if we have found it, launch the installer
|
---|
183 | } else {
|
---|
184 |
|
---|
185 | cout << "Launching Installer ..." << endl;
|
---|
186 | int launch_exit_code = 0;
|
---|
187 | launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx200M -jar @[email protected]" + (text_mode?" text":"") ).c_str() );
|
---|
188 |
|
---|
189 | //report how it went
|
---|
190 | if ( launch_exit_code == 0 ) {
|
---|
191 | cout << "Setup complete" << endl;
|
---|
192 | } else {
|
---|
193 | cout << "The installer exited with an error" << endl;
|
---|
194 | cout << "@projectname@ may not be correctly installed" << endl;
|
---|
195 | }
|
---|
196 |
|
---|
197 | }
|
---|
198 | #else
|
---|
199 | bool java_ready = prep_java( wd );
|
---|
200 | if ( java_ready ) {
|
---|
201 | string cmd = "./jre/bin/java -Dorig.dir=\"" + wd + "\" -jar " + wd + "/Java/Jars/linux.jar" + (text_mode?" text":"");
|
---|
202 | system( cmd.c_str() );
|
---|
203 | }
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | //change to the scratch dir for the following operation
|
---|
207 | chdir(scratch.c_str());
|
---|
208 |
|
---|
209 | //delete the temp files
|
---|
210 | cout << "Deleting the temp directory" << endl;
|
---|
211 | system( ("rm -rf " + tempdir).c_str() );
|
---|
212 |
|
---|
213 | return 0;
|
---|
214 |
|
---|
215 | }
|
---|
216 | #ifndef CDROM
|
---|
217 | bool extract_bundled_file( const char data[], int size, char* filename, bool make_executable) {
|
---|
218 |
|
---|
219 | if ( size == 0 ) return false;
|
---|
220 |
|
---|
221 | //delete the file if it exists
|
---|
222 | remove( filename );
|
---|
223 |
|
---|
224 | //open the file
|
---|
225 | fstream binary_file( filename, ios::out|ios::binary );
|
---|
226 | if ( !binary_file.good() ) {
|
---|
227 | binary_file.close();
|
---|
228 | return false;
|
---|
229 | }
|
---|
230 |
|
---|
231 | //write the file
|
---|
232 | binary_file.write( data, size );
|
---|
233 | if ( !binary_file.good() ) {
|
---|
234 | binary_file.close();
|
---|
235 | return false;
|
---|
236 | }
|
---|
237 |
|
---|
238 | //close the file
|
---|
239 | binary_file.close();
|
---|
240 | if ( !binary_file.good() ) {
|
---|
241 | binary_file.close();
|
---|
242 | return false;
|
---|
243 | }
|
---|
244 |
|
---|
245 | if ( make_executable ) {
|
---|
246 | string command = "chmod a+x " + string(filename);
|
---|
247 | system( command.c_str() );
|
---|
248 | }
|
---|
249 |
|
---|
250 | return true;
|
---|
251 |
|
---|
252 | }
|
---|
253 | #else
|
---|
254 |
|
---|
255 | bool prep_java( string wd ) {
|
---|
256 |
|
---|
257 |
|
---|
258 | string jTestCmd = "./jre/bin/java -version >/dev/null 2>&1";
|
---|
259 |
|
---|
260 | //try to extract straight off cd
|
---|
261 | string j1cmd = wd + "/Java/Linux/jre_bin && tar -xf jre.tar && " + jTestCmd;
|
---|
262 | int j1status = system(j1cmd.c_str());
|
---|
263 | system("/bin/rm -f jre.tar");
|
---|
264 |
|
---|
265 | if (WEXITSTATUS(j1status)!=0) {
|
---|
266 | cerr << "Unable to extract java straight off CD-ROM." << endl;
|
---|
267 | cerr << "This is probably because your CD-ROM was mounted with 'noexec'." << endl;
|
---|
268 | cerr << "It can be changed by a system administrator" << endl << endl;
|
---|
269 | cerr << "Copying Java to local disk so it can be run" << endl;
|
---|
270 |
|
---|
271 | string cdtar_home = wd + "/Java/Linux/";
|
---|
272 | string tcmd = "(cd \"" + cdtar_home + "\" ; tar cf - jre_bin )";
|
---|
273 | tcmd += "| tar xvf - ";
|
---|
274 | //tcmd += "| awk 'BEGIN{C=0}{C++; printf(\".\"); if (C%70==0) printf(\"\\n\");}END{printf(\"\\n\")}'";
|
---|
275 |
|
---|
276 | int tstatus = system(tcmd.c_str());
|
---|
277 |
|
---|
278 | if (WEXITSTATUS(tstatus)!=0) {
|
---|
279 | cerr << "Failed to copy Java to disk" << endl;
|
---|
280 | cerr << "Please make sure you have write permissions in the directory where you run the installer" << endl;
|
---|
281 | }
|
---|
282 | else {
|
---|
283 |
|
---|
284 | string j2cmd = "./jre_bin && tar -xf jre.tar && " + jTestCmd;
|
---|
285 | int j2status = system(j2cmd.c_str());
|
---|
286 | system("/bin/rm -f jre.tar jre_bin");
|
---|
287 |
|
---|
288 | if (WEXITSTATUS(j2status)!=0) {
|
---|
289 | cerr << "Unable to run copied Java" << endl;
|
---|
290 | return false;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | // to get to here, full_java must be set (and working)
|
---|
296 |
|
---|
297 | return true;
|
---|
298 | }
|
---|
299 |
|
---|
300 | #endif
|
---|