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

Last change on this file since 22335 was 22335, checked in by sjm84, 12 years ago

Removed the extract option for the CD on Linux

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