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