source: release-kits/shared/linux/wrapper.cpp@ 19765

Last change on this file since 19765 was 19765, checked in by oranfry, 15 years ago

cdrom installer: added david's function to return the path of a java that will work, copying java off the cd onto disk to make it work where necessary. used it in the wrapper.

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