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

Last change on this file since 16208 was 16208, checked in by oranfry, 16 years ago

making the new linux wrapper work

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