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

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

allowing the use of installer argument 'text' to trigger text mode installation

File size: 4.4 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <iomanip>
4
5using namespace std;
6
7#include "wrapper.h"
8#include "libsearch4j.h"
9
10bool extract_bundled_file( const char[], int, char*, bool );
11
12int main(int argc, char** argv) {
13
14 string tempdir = "@[email protected]"; //temporary directory where we will store extracted files
15 string jarfile = tempdir + "/@[email protected]"; //where we will store the jar file
16 string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
17 bool succeeded = false;
18 bool textMode = false;
19
20 //parse arguments
21 for ( int i=0; i<argc; i++ ) {
22 if ( strcmp( argv[i], "text" ) == 0 ) {
23 textMode = true;
24 }
25 }
26
27
28 //create the temp folder
29 cout << "Creating temp directory..." << endl;
30 succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
31 if ( !succeeded ) {
32 cout << "Failed to write to create the temp directory '" << tempdir << "'" << endl;
33 cout << "Check that it does not already exist and try again" << endl;
34 cout << "Exiting" << endl;
35 return 1;
36 }
37
38 //extract files
39 cout << "Extracting installer jar..." << endl;
40 succeeded = extract_bundled_file( @installer.name@jar, sizeof(@installer.name@jar), (char*)jarfile.c_str(), false);
41
42 #ifdef java_is_bundled
43 cout << "Preparing Greenstone installer..." << endl;
44 succeeded = extract_bundled_file( java, sizeof(java), (char*)javafile.c_str(), true ) && succeeded;
45 #endif
46
47 if ( !succeeded ) {
48 cout << "Failed to extract one or more resources" << endl;
49 cout << "This installer may not have sufficient file permissions to write it to disk" << endl;
50 cout << "Or, the files may be corrupt or missing from this executable" << endl;
51 cout << "Exiting" << endl;
52 return 1;
53 }
54
55 //change to the temp directory
56 chdir( tempdir.c_str() );
57
58 #ifdef java_is_bundled
59 succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0);
60 succeeded = succeeded && ( system( "tar -xf jre.tar" ) == 0 );
61 if ( !succeeded ) {
62 cout << "Failed to extract the bundled java archive to the temp directory" << endl;
63 cout << "You need the tar program on your PATH" << endl;
64 cout << "Exiting" << endl;
65 return 1;
66 }
67 #endif
68
69 //check if an appropriate java is found
70 Jvm minimum; minimum.setVersionFromString( "@java.min.version@" );
71 string phint = "./@java.extracted@";
72 string hint = "";
73 Jvm foundJvm;
74 bool jvmFound = find( foundJvm, true, minimum, false, false, phint, hint, false );
75
76 //if the jvm was not found, report not found
77 if ( !jvmFound ) {
78
79 //did not find a good java
80 cout << "Greenstone requires java @java.min.version@ or greater." << endl;
81
82 //tell them if java is absent or just too old
83 Jvm tmpJvm;
84 if ( find( tmpJvm, false, minimum, false, false, phint, hint, false ) ) {
85 cout << "You have java, but it is too old." << endl;
86 } else {
87 cout << "Could not find java on your system." << endl;
88 }
89
90 cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl;
91 #ifndef java_is_bundled
92 cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl;
93 #endif
94
95 //if we have found it, launch the installer
96 } else {
97
98 cout << "Launching Installer ..." << endl;
99 int launch_exit_code = 0;
100 launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx85M -jar @[email protected]" + (textMode?" text":"") ).c_str() );
101
102 //report how it went
103 if ( launch_exit_code == 0 ) {
104 cout << "Setup complete" << endl;
105 } else {
106 cout << "The installer exited with an error" << endl;
107 cout << "Greenstone may not be correctly installed" << endl;
108 }
109
110 }
111
112 //change back to the original directory
113 chdir("..");
114
115 //delete the temp files
116 cout << "Deleting the temp directory" << endl;
117 system( ("rm -rf " + tempdir).c_str() );
118
119 return 0;
120
121}
122
123bool extract_bundled_file( const char data[], int size, char* filename, bool make_executable) {
124
125 if ( size == 0 ) return false;
126
127 //delete the file if it exists
128 remove( filename );
129
130 //open the file
131 fstream binary_file( filename, ios::out|ios::binary );
132 if ( !binary_file.good() ) {
133 binary_file.close();
134 return false;
135 }
136
137 //write the file
138 binary_file.write( data, size );
139 if ( !binary_file.good() ) {
140 binary_file.close();
141 return false;
142 }
143
144 //close the file
145 binary_file.close();
146 if ( !binary_file.good() ) {
147 binary_file.close();
148 return false;
149 }
150
151 if ( make_executable ) {
152 string command = "chmod a+x " + string(filename);
153 system( command.c_str() );
154 }
155
156 return true;
157
158}
Note: See TracBrowser for help on using the repository browser.