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

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

it's all good stuff

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
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 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 bool java_ready = prep_java( wd );
181 if ( java_ready ) {
182 string cmd = "./jre/bin/java -Dorig.dir=\"" + wd + "\" -jar " + wd + "/Java/Jars/linux.jar" + (text_mode?" text":"");
183 system( cmd.c_str() );
184 }
185 #endif
186
187 //change to the scratch dir for the following operation
188 chdir(scratch.c_str());
189
190 //delete the temp files
191 cout << "Deleting the temp directory" << endl;
192 system( ("rm -rf " + tempdir).c_str() );
193
194 return 0;
195
196}
197#ifndef CDROM
198bool extract_bundled_file( const char data[], int size, char* filename, bool make_executable) {
199
200 if ( size == 0 ) return false;
201
202 //delete the file if it exists
203 remove( filename );
204
205 //open the file
206 fstream binary_file( filename, ios::out|ios::binary );
207 if ( !binary_file.good() ) {
208 binary_file.close();
209 return false;
210 }
211
212 //write the file
213 binary_file.write( data, size );
214 if ( !binary_file.good() ) {
215 binary_file.close();
216 return false;
217 }
218
219 //close the file
220 binary_file.close();
221 if ( !binary_file.good() ) {
222 binary_file.close();
223 return false;
224 }
225
226 if ( make_executable ) {
227 string command = "chmod a+x " + string(filename);
228 system( command.c_str() );
229 }
230
231 return true;
232
233}
234#else
235
236bool prep_java( string wd ) {
237
238
239 string jTestCmd = "./jre/bin/java -version >/dev/null 2>&1";
240
241 //try to extract straight off cd
242 string j1cmd = wd + "/Java/Linux/jre_bin && tar -xf jre.tar && " + jTestCmd;
243 int j1status = system(j1cmd.c_str());
244 system("/bin/rm -f jre.tar");
245
246 if (WEXITSTATUS(j1status)!=0) {
247 cerr << "Unable to extract java straight off CD-ROM." << endl;
248 cerr << "This is probably because your CD-ROM was mounted with 'noexec'." << endl;
249 cerr << "It can be changed by a system administrator" << endl << endl;
250 cerr << "Copying Java to local disk so it can be run" << endl;
251
252 string cdtar_home = wd + "/Java/Linux/";
253 string tcmd = "(cd \"" + cdtar_home + "\" ; tar cf - jre_bin )";
254 tcmd += "| tar xvf - ";
255 //tcmd += "| awk 'BEGIN{C=0}{C++; printf(\".\"); if (C%70==0) printf(\"\\n\");}END{printf(\"\\n\")}'";
256
257 int tstatus = system(tcmd.c_str());
258
259 if (WEXITSTATUS(tstatus)!=0) {
260 cerr << "Failed to copy Java to disk" << endl;
261 cerr << "Please make sure you have write permissions in the directory where you run the installer" << endl;
262 }
263 else {
264
265 string j2cmd = "./jre_bin && tar -xf jre.tar && " + jTestCmd;
266 int j2status = system(j2cmd.c_str());
267 system("/bin/rm -f jre.tar jre_bin");
268
269 if (WEXITSTATUS(j2status)!=0) {
270 cerr << "Unable to run copied Java" << endl;
271 return false;
272 }
273 }
274 }
275
276 // to get to here, full_java must be set (and working)
277
278 return true;
279}
280
281#endif
Note: See TracBrowser for help on using the repository browser.