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

Last change on this file since 28713 was 28713, checked in by ak19, 10 years ago

Undoing accidental commit on file (wrapper.cpp) not related to revision 28712

File size: 8.8 KB
RevLine 
[15972]1#include <iostream>
2#include <fstream>
3#include <iomanip>
[19720]4#include <string.h>
[19027]5#include <cstdlib>
[19720]6#include <sstream>
[15972]7using namespace std;
[22335]8
[19720]9#ifndef CDROM
[15975]10#include "wrapper.h"
[16207]11#include "libsearch4j.h"
[15972]12bool extract_bundled_file( const char[], int, char*, bool );
[19765]13#else
[19938]14bool prep_java( string wd );
[19594]15#endif
[15972]16
[19720]17void usage( char* progname, int exitvalue = -1 ) {
18 cerr
19 << "Usage: " << progname << " [OPTIONS]" << endl
20 << "OPTIONS: " << endl
[23256]21 << " -h, -help Display this usage message" << endl
[23265]22 << " -textonly Do a non-graphical command line installation" << endl
[23256]23 << " -extract Extract the jar file, this can " << endl;
[19720]24 #ifdef CDROM
25 cerr << " -wd PATH Look for the CD-ROM at PATH" << endl;
26 #endif
27
28 exit( exitvalue );
29}
30
[15972]31int main(int argc, char** argv) {
32
[19720]33 char wdc[1024];
34 getcwd(wdc, sizeof(wdc));
35 string wd = "";
36 wd.append( wdc );
37
[19027]38 string scratch = (getenv("TMPDIR") == NULL) ? "/tmp" : getenv("TMPDIR");
[28713]39 string tempdir = scratch + "/greenstone-installer.tmp"; //temporary directory where we will store extracted files
[15972]40 bool succeeded = false;
[19720]41 bool text_mode = false;
[15972]42
[18877]43 //parse arguments
[19720]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 );
[23256]52 } else if ( a.compare("-textonly") == 0 || a.compare("-text") == 0 || a.compare("text") == 0 ) {
[28713]53 //cerr << "-text is deprecated, please use -extract and then run \"java -jar greenstone.jar text\"" << endl;
[19720]54 text_mode = true;
[22334]55#ifndef CDROM
[22225]56 } else if ( a.compare("-extract") == 0 || a.compare("--extract") || a.compare("-x")) {
[22324]57 cout << "----------------------------" << endl;
58 cout << "Extracting java installer..." << endl;
59 cout << "----------------------------" << endl;
[28713]60 bool result = extract_bundled_file( greenstonejar, sizeof(greenstonejar), "greenstone.jar", false);
[22225]61 if(result){
62 cout << "\nExtraction Complete" << endl;
[28713]63 cout << "You can now run \"java -jar greenstone.jar text\" to run the installer from the command line\n" << endl;
[22225]64 return 0;
65 }
66 else{
[22324]67 cerr << "\nThere was an error while extracting the java installer" << endl;
[22225]68 return 1;
69 }
[22334]70#endif
[19720]71 } else {
72 cerr << "Unrecognised option '" << a << "'" << endl;
73 usage(argv[0]);
[18877]74 }
75 }
76
[19720]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 }
[18877]91
[19720]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
[15972]102 //create the temp folder
103 cout << "Creating temp directory..." << endl;
104 succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
105 if ( !succeeded ) {
[19720]106 cerr << "Failed to create the temp directory '" << tempdir << "'. " <<
[19327]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 " <<
[19720]109 "a directory where you do have write permissions." << endl <<
110 "Exiting" << endl;
[15972]111 return 1;
112 }
[19594]113
[19720]114 #ifndef CDROM
[15972]115
[28713]116 string jarfile = tempdir + "/greenstone.jar"; //where we will store the jar file
[19594]117 string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
118
119
[15972]120 //extract files
121 cout << "Extracting installer jar..." << endl;
[28713]122 succeeded = extract_bundled_file( greenstonejar, sizeof(greenstonejar), (char*)jarfile.c_str(), false);
[15972]123
124 #ifdef java_is_bundled
[28713]125 cout << "Preparing Greenstone installer..." << endl;
[15972]126 succeeded = extract_bundled_file( java, sizeof(java), (char*)javafile.c_str(), true ) && succeeded;
127 #endif
128
129 if ( !succeeded ) {
[19720]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;
[15972]134 return 1;
135 }
136
[19594]137 #endif
138
[15972]139 //change to the temp directory
140 chdir( tempdir.c_str() );
141
[19594]142
[19720]143 #ifndef CDROM
[15972]144 #ifdef java_is_bundled
[17761]145 succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0);
[17763]146 succeeded = succeeded && ( system( "tar -xf jre.tar" ) == 0 );
[15972]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
[16207]156 Jvm minimum; minimum.setVersionFromString( "@java.min.version@" );
[17306]157 string phint = "./@java.extracted@";
158 string hint = "";
[16207]159 Jvm foundJvm;
[18283]160 bool jvmFound = find( foundJvm, true, minimum, false, false, phint, hint, false );
[15972]161
162 //if the jvm was not found, report not found
163 if ( !jvmFound ) {
164
165 //did not find a good java
[28713]166 cout << "Greenstone requires java @java.min.version@ or greater." << endl;
[15972]167
[16207]168 //tell them if java is absent or just too old
169 Jvm tmpJvm;
[18283]170 if ( find( tmpJvm, false, minimum, false, false, phint, hint, false ) ) {
[15972]171 cout << "You have java, but it is too old." << endl;
172 } else {
173 cout << "Could not find java on your system." << endl;
[16207]174 }
[16208]175
[15972]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;
[28713]186 launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx200M -jar greenstone.jar" + (text_mode?" text":"") ).c_str() );
[15972]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;
[28713]193 cout << "Greenstone may not be correctly installed" << endl;
[15972]194 }
195
196 }
[19720]197 #else
[19938]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":"");
[19766]201 system( cmd.c_str() );
202 }
[19594]203 #endif
204
[19027]205 //change to the scratch dir for the following operation
[19034]206 chdir(scratch.c_str());
[15972]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}
[19720]215#ifndef CDROM
[15972]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}
[19765]252#else
253
[19938]254bool prep_java( string wd ) {
[19765]255
256
[19938]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;
[19765]261 int j1status = system(j1cmd.c_str());
[19938]262 system("/bin/rm -f jre.tar");
[19765]263
264 if (WEXITSTATUS(j1status)!=0) {
[19938]265 cerr << "Unable to extract java straight off CD-ROM." << endl;
[19765]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/";
[19938]271 string tcmd = "(cd \"" + cdtar_home + "\" ; tar cf - jre_bin )";
[19765]272 tcmd += "| tar xvf - ";
[19938]273 //tcmd += "| awk 'BEGIN{C=0}{C++; printf(\".\"); if (C%70==0) printf(\"\\n\");}END{printf(\"\\n\")}'";
[19765]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
[19938]283 string j2cmd = "./jre_bin && tar -xf jre.tar && " + jTestCmd;
[19765]284 int j2status = system(j2cmd.c_str());
[19938]285 system("/bin/rm -f jre.tar jre_bin");
[19765]286
[19938]287 if (WEXITSTATUS(j2status)!=0) {
288 cerr << "Unable to run copied Java" << endl;
289 return false;
290 }
[19765]291 }
292 }
293
294 // to get to here, full_java must be set (and working)
295
[19938]296 return true;
[19765]297}
298
[19594]299#endif
Note: See TracBrowser for help on using the repository browser.