source: main/trunk/release-kits/shared/linux/wrapper.cpp@ 23265

Last change on this file since 23265 was 23265, checked in by sjm84, 13 years ago

Removed an accidentally added semi-colon

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