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

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

Removed the extract option for the CD on Linux

File size: 8.7 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
21 << " -h, -help Display this usage message" << endl
[22225]22 << " -text (Depricated) Do a non-graphical command line installation" << endl;
[19720]23 #ifdef CDROM
24 cerr << " -wd PATH Look for the CD-ROM at PATH" << endl;
25 #endif
26
27 exit( exitvalue );
28}
29
[15972]30int main(int argc, char** argv) {
31
[19720]32 char wdc[1024];
33 getcwd(wdc, sizeof(wdc));
34 string wd = "";
35 wd.append( wdc );
36
[19027]37 string scratch = (getenv("TMPDIR") == NULL) ? "/tmp" : getenv("TMPDIR");
[19594]38 string tempdir = scratch + "/greenstone-installer.tmp"; //temporary directory where we will store extracted files
[15972]39 bool succeeded = false;
[19720]40 bool text_mode = false;
[15972]41
[18877]42 //parse arguments
[19720]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 ) {
[22225]52 cerr << "-text is depricated, please use -extract and then run \"java -jar greenstone.jar text\"" << endl;
[19720]53 text_mode = true;
[22334]54#ifndef CDROM
[22225]55 } else if ( a.compare("-extract") == 0 || a.compare("--extract") || a.compare("-x")) {
[22324]56 cout << "----------------------------" << endl;
57 cout << "Extracting java installer..." << endl;
58 cout << "----------------------------" << endl;
59 bool result = extract_bundled_file( greenstonejar, sizeof(greenstonejar), "greenstone.jar", false);
[22225]60 if(result){
61 cout << "\nExtraction Complete" << endl;
[22324]62 cout << "You can now run \"java -jar greenstone.jar text\" to run the installer from the command line\n" << endl;
[22225]63 return 0;
64 }
65 else{
[22324]66 cerr << "\nThere was an error while extracting the java installer" << endl;
[22225]67 return 1;
68 }
[22334]69#endif
[19720]70 } else {
71 cerr << "Unrecognised option '" << a << "'" << endl;
72 usage(argv[0]);
[18877]73 }
74 }
75
[19720]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 }
[18877]90
[19720]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
[15972]101 //create the temp folder
102 cout << "Creating temp directory..." << endl;
103 succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
104 if ( !succeeded ) {
[19720]105 cerr << "Failed to create the temp directory '" << tempdir << "'. " <<
[19327]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 " <<
[19720]108 "a directory where you do have write permissions." << endl <<
109 "Exiting" << endl;
[15972]110 return 1;
111 }
[19594]112
[19720]113 #ifndef CDROM
[15972]114
[21630]115 string jarfile = tempdir + "/greenstone.jar"; //where we will store the jar file
[19594]116 string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
117
118
[15972]119 //extract files
120 cout << "Extracting installer jar..." << endl;
[21630]121 succeeded = extract_bundled_file( greenstonejar, sizeof(greenstonejar), (char*)jarfile.c_str(), false);
[15972]122
123 #ifdef java_is_bundled
[17770]124 cout << "Preparing Greenstone installer..." << endl;
[15972]125 succeeded = extract_bundled_file( java, sizeof(java), (char*)javafile.c_str(), true ) && succeeded;
126 #endif
127
128 if ( !succeeded ) {
[19720]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;
[15972]133 return 1;
134 }
135
[19594]136 #endif
137
[15972]138 //change to the temp directory
139 chdir( tempdir.c_str() );
140
[19594]141
[19720]142 #ifndef CDROM
[15972]143 #ifdef java_is_bundled
[17761]144 succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0);
[17763]145 succeeded = succeeded && ( system( "tar -xf jre.tar" ) == 0 );
[15972]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
[16207]155 Jvm minimum; minimum.setVersionFromString( "@java.min.version@" );
[17306]156 string phint = "./@java.extracted@";
157 string hint = "";
[16207]158 Jvm foundJvm;
[18283]159 bool jvmFound = find( foundJvm, true, minimum, false, false, phint, hint, false );
[15972]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
[16207]167 //tell them if java is absent or just too old
168 Jvm tmpJvm;
[18283]169 if ( find( tmpJvm, false, minimum, false, false, phint, hint, false ) ) {
[15972]170 cout << "You have java, but it is too old." << endl;
171 } else {
172 cout << "Could not find java on your system." << endl;
[16207]173 }
[16208]174
[15972]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;
[21630]185 launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx85M -jar greenstone.jar" + (text_mode?" text":"") ).c_str() );
[15972]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 }
[19720]196 #else
[19938]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":"");
[19766]200 system( cmd.c_str() );
201 }
[19594]202 #endif
203
[19027]204 //change to the scratch dir for the following operation
[19034]205 chdir(scratch.c_str());
[15972]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}
[19720]214#ifndef CDROM
[15972]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}
[19765]251#else
252
[19938]253bool prep_java( string wd ) {
[19765]254
255
[19938]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;
[19765]260 int j1status = system(j1cmd.c_str());
[19938]261 system("/bin/rm -f jre.tar");
[19765]262
263 if (WEXITSTATUS(j1status)!=0) {
[19938]264 cerr << "Unable to extract java straight off CD-ROM." << endl;
[19765]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/";
[19938]270 string tcmd = "(cd \"" + cdtar_home + "\" ; tar cf - jre_bin )";
[19765]271 tcmd += "| tar xvf - ";
[19938]272 //tcmd += "| awk 'BEGIN{C=0}{C++; printf(\".\"); if (C%70==0) printf(\"\\n\");}END{printf(\"\\n\")}'";
[19765]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
[19938]282 string j2cmd = "./jre_bin && tar -xf jre.tar && " + jTestCmd;
[19765]283 int j2status = system(j2cmd.c_str());
[19938]284 system("/bin/rm -f jre.tar jre_bin");
[19765]285
[19938]286 if (WEXITSTATUS(j2status)!=0) {
287 cerr << "Unable to run copied Java" << endl;
288 return false;
289 }
[19765]290 }
291 }
292
293 // to get to here, full_java must be set (and working)
294
[19938]295 return true;
[19765]296}
297
[19594]298#endif
Note: See TracBrowser for help on using the repository browser.