1 | #include <iostream>
|
---|
2 | #include <fstream>
|
---|
3 | #include <iomanip>
|
---|
4 | #include <string.h>
|
---|
5 | #include <cstdlib>
|
---|
6 | #include <sstream>
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 | #ifndef CDROM
|
---|
10 | #include "wrapper.h"
|
---|
11 | #include "libsearch4j.h"
|
---|
12 | bool extract_bundled_file( const char[], int, char*, bool );
|
---|
13 | #endif
|
---|
14 |
|
---|
15 | void usage( char* progname, int exitvalue = -1 ) {
|
---|
16 | cerr
|
---|
17 | << "Usage: " << progname << " [OPTIONS]" << endl
|
---|
18 | << "OPTIONS: " << endl
|
---|
19 | << " -h, -help Display this usage message" << endl
|
---|
20 | << " -text Do a non-graphical command line installation" << endl;
|
---|
21 | #ifdef CDROM
|
---|
22 | cerr << " -wd PATH Look for the CD-ROM at PATH" << endl;
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | exit( exitvalue );
|
---|
26 | }
|
---|
27 |
|
---|
28 | int main(int argc, char** argv) {
|
---|
29 |
|
---|
30 | char wdc[1024];
|
---|
31 | getcwd(wdc, sizeof(wdc));
|
---|
32 | string wd = "";
|
---|
33 | wd.append( wdc );
|
---|
34 |
|
---|
35 | string scratch = (getenv("TMPDIR") == NULL) ? "/tmp" : getenv("TMPDIR");
|
---|
36 | string tempdir = scratch + "/greenstone-installer.tmp"; //temporary directory where we will store extracted files
|
---|
37 | bool succeeded = false;
|
---|
38 | bool text_mode = false;
|
---|
39 |
|
---|
40 | //parse arguments
|
---|
41 | string a;
|
---|
42 | for ( int i=1; i<argc; i++ ) {
|
---|
43 | a = argv[i];
|
---|
44 | if ( a.compare("-wd") == 0 ) {
|
---|
45 | wd = "";
|
---|
46 | wd.append( argv[++i] );
|
---|
47 | } else if ( a.compare("-h") == 0 || a.compare("-help") == 0 || a.compare("--help") == 0) {
|
---|
48 | usage( argv[0], 0 );
|
---|
49 | } else if ( a.compare("-text") == 0 || a.compare("text") == 0 ) {
|
---|
50 | text_mode = true;
|
---|
51 | } else {
|
---|
52 | cerr << "Unrecognised option '" << a << "'" << endl;
|
---|
53 | usage(argv[0]);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | #ifdef CDROM
|
---|
58 | //ensure working directory is correct
|
---|
59 | FILE* pFile;
|
---|
60 | pFile = fopen( (wd+"/Software/core/all/uninst.jar").c_str(), "r");
|
---|
61 | if ( pFile == NULL ) {
|
---|
62 | cerr << "Could not find cdrom at '" << wd << "'." << endl;
|
---|
63 | cerr << "Please enter the path the CDROM [/media/cdrom]: ";
|
---|
64 | getline(cin, wd);
|
---|
65 | if ( wd.compare("") == 0 ) {
|
---|
66 | wd = "/media/cdrom";
|
---|
67 | }
|
---|
68 | } else {
|
---|
69 | fclose(pFile);
|
---|
70 | }
|
---|
71 |
|
---|
72 | pFile = fopen( (wd+"/Software/core/all/uninst.jar").c_str(), "r");
|
---|
73 | if ( pFile == NULL ) {
|
---|
74 | cerr << "Could not find cdrom at '"<< wd << "'." << endl;
|
---|
75 | cerr << "Please run the installer again" << endl;
|
---|
76 | exit(-1);
|
---|
77 | } else {
|
---|
78 | fclose(pFile);
|
---|
79 | }
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | //create the temp folder
|
---|
83 | cout << "Creating temp directory..." << endl;
|
---|
84 | succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
|
---|
85 | if ( !succeeded ) {
|
---|
86 | cerr << "Failed to create the temp directory '" << tempdir << "'. " <<
|
---|
87 | "Check that it does not already exist. Also check that you have write " <<
|
---|
88 | "permissions to '" << scratch << "', or set the environment variable TMPDIR to " <<
|
---|
89 | "a directory where you do have write permissions." << endl <<
|
---|
90 | "Exiting" << endl;
|
---|
91 | return 1;
|
---|
92 | }
|
---|
93 |
|
---|
94 | #ifndef CDROM
|
---|
95 |
|
---|
96 | string jarfile = tempdir + "/@[email protected]"; //where we will store the jar file
|
---|
97 | string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
|
---|
98 |
|
---|
99 |
|
---|
100 | //extract files
|
---|
101 | cout << "Extracting installer jar..." << endl;
|
---|
102 | succeeded = extract_bundled_file( @installer.name@jar, sizeof(@installer.name@jar), (char*)jarfile.c_str(), false);
|
---|
103 |
|
---|
104 | #ifdef java_is_bundled
|
---|
105 | cout << "Preparing Greenstone installer..." << endl;
|
---|
106 | succeeded = extract_bundled_file( java, sizeof(java), (char*)javafile.c_str(), true ) && succeeded;
|
---|
107 | #endif
|
---|
108 |
|
---|
109 | if ( !succeeded ) {
|
---|
110 | cerr << "Failed to extract one or more resources" << endl;
|
---|
111 | cerr << "This installer may not have sufficient file permissions to write it to disk" << endl;
|
---|
112 | cerr << "Or, the files may be corrupt or missing from this executable" << endl;
|
---|
113 | cerr << "Exiting" << endl;
|
---|
114 | return 1;
|
---|
115 | }
|
---|
116 |
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | //change to the temp directory
|
---|
120 | chdir( tempdir.c_str() );
|
---|
121 |
|
---|
122 |
|
---|
123 | #ifndef CDROM
|
---|
124 | #ifdef java_is_bundled
|
---|
125 | succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0);
|
---|
126 | succeeded = succeeded && ( system( "tar -xf jre.tar" ) == 0 );
|
---|
127 | if ( !succeeded ) {
|
---|
128 | cout << "Failed to extract the bundled java archive to the temp directory" << endl;
|
---|
129 | cout << "You need the tar program on your PATH" << endl;
|
---|
130 | cout << "Exiting" << endl;
|
---|
131 | return 1;
|
---|
132 | }
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | //check if an appropriate java is found
|
---|
136 | Jvm minimum; minimum.setVersionFromString( "@java.min.version@" );
|
---|
137 | string phint = "./@java.extracted@";
|
---|
138 | string hint = "";
|
---|
139 | Jvm foundJvm;
|
---|
140 | bool jvmFound = find( foundJvm, true, minimum, false, false, phint, hint, false );
|
---|
141 |
|
---|
142 | //if the jvm was not found, report not found
|
---|
143 | if ( !jvmFound ) {
|
---|
144 |
|
---|
145 | //did not find a good java
|
---|
146 | cout << "Greenstone requires java @java.min.version@ or greater." << endl;
|
---|
147 |
|
---|
148 | //tell them if java is absent or just too old
|
---|
149 | Jvm tmpJvm;
|
---|
150 | if ( find( tmpJvm, false, minimum, false, false, phint, hint, false ) ) {
|
---|
151 | cout << "You have java, but it is too old." << endl;
|
---|
152 | } else {
|
---|
153 | cout << "Could not find java on your system." << endl;
|
---|
154 | }
|
---|
155 |
|
---|
156 | cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl;
|
---|
157 | #ifndef java_is_bundled
|
---|
158 | cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl;
|
---|
159 | #endif
|
---|
160 |
|
---|
161 | //if we have found it, launch the installer
|
---|
162 | } else {
|
---|
163 |
|
---|
164 | cout << "Launching Installer ..." << endl;
|
---|
165 | int launch_exit_code = 0;
|
---|
166 | launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx85M -jar @[email protected]" + (text_mode?" text":"") ).c_str() );
|
---|
167 |
|
---|
168 | //report how it went
|
---|
169 | if ( launch_exit_code == 0 ) {
|
---|
170 | cout << "Setup complete" << endl;
|
---|
171 | } else {
|
---|
172 | cout << "The installer exited with an error" << endl;
|
---|
173 | cout << "Greenstone may not be correctly installed" << endl;
|
---|
174 | }
|
---|
175 |
|
---|
176 | }
|
---|
177 | #else
|
---|
178 | string cmd = wd + "/Java/Linux/jre/bin/java -Dorig.dir=\"" + wd + "\" -jar " + wd + "/Java/Jars/linux.jar" + (text_mode?" text":"");
|
---|
179 | system( cmd.c_str() );
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | //change to the scratch dir for the following operation
|
---|
183 | chdir(scratch.c_str());
|
---|
184 |
|
---|
185 | //delete the temp files
|
---|
186 | cout << "Deleting the temp directory" << endl;
|
---|
187 | system( ("rm -rf " + tempdir).c_str() );
|
---|
188 |
|
---|
189 | return 0;
|
---|
190 |
|
---|
191 | }
|
---|
192 | #ifndef CDROM
|
---|
193 | bool extract_bundled_file( const char data[], int size, char* filename, bool make_executable) {
|
---|
194 |
|
---|
195 | if ( size == 0 ) return false;
|
---|
196 |
|
---|
197 | //delete the file if it exists
|
---|
198 | remove( filename );
|
---|
199 |
|
---|
200 | //open the file
|
---|
201 | fstream binary_file( filename, ios::out|ios::binary );
|
---|
202 | if ( !binary_file.good() ) {
|
---|
203 | binary_file.close();
|
---|
204 | return false;
|
---|
205 | }
|
---|
206 |
|
---|
207 | //write the file
|
---|
208 | binary_file.write( data, size );
|
---|
209 | if ( !binary_file.good() ) {
|
---|
210 | binary_file.close();
|
---|
211 | return false;
|
---|
212 | }
|
---|
213 |
|
---|
214 | //close the file
|
---|
215 | binary_file.close();
|
---|
216 | if ( !binary_file.good() ) {
|
---|
217 | binary_file.close();
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 |
|
---|
221 | if ( make_executable ) {
|
---|
222 | string command = "chmod a+x " + string(filename);
|
---|
223 | system( command.c_str() );
|
---|
224 | }
|
---|
225 |
|
---|
226 | return true;
|
---|
227 |
|
---|
228 | }
|
---|
229 | #endif
|
---|