1 | #include <iostream>
|
---|
2 | #include <fstream>
|
---|
3 | #include <iomanip>
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | #include "mywrapper.h"
|
---|
8 |
|
---|
9 | bool extract_bundled_file( const char[], int, char*, bool );
|
---|
10 |
|
---|
11 | int main(int argc, char** argv) {
|
---|
12 |
|
---|
13 | string tempdir = (string)argv[0] + ".tmp"; //temporary directory where we will store extracted files
|
---|
14 | string jarfile = tempdir + "/greenstone3.jar"; //where we will store the jar file
|
---|
15 | string search4jfile = tempdir + "/search4j"; //where we will store the jar file
|
---|
16 | bool succeeded = false;
|
---|
17 |
|
---|
18 | //create the temp folder
|
---|
19 | cout << "Creating temp directory" << endl;
|
---|
20 | succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
|
---|
21 | if ( !succeeded ) {
|
---|
22 | cout << "Failed to write to create the temp directory '" << tempdir << "'" << endl;
|
---|
23 | cout << "Check that it does not already exist and try again" << endl;
|
---|
24 | cout << "Exiting" << endl;
|
---|
25 | return 1;
|
---|
26 | }
|
---|
27 |
|
---|
28 | //extract files
|
---|
29 | cout << "Extracting installer jar..." << endl;
|
---|
30 | succeeded = extract_bundled_file( greenstone3jar, sizeof(greenstone3jar), (char*)jarfile.c_str(), false);
|
---|
31 | if ( !succeeded ) {
|
---|
32 | cout << "failed" << endl;
|
---|
33 | cout << "Failed to extract the JAR file to '" << jarfile << "'" << endl;
|
---|
34 | cout << "The file may be corrupt or missing" << endl;
|
---|
35 | cout << "Or this installer may not have sufficient file permissions to write it to disk" << endl;
|
---|
36 | cout << "Exiting" << endl;
|
---|
37 | return 1;
|
---|
38 | }
|
---|
39 |
|
---|
40 | cout << "Extracting search4j tool..." << endl;
|
---|
41 | succeeded = extract_bundled_file( search4j, sizeof(search4j), (char*)search4jfile.c_str(), true );
|
---|
42 | if ( !succeeded ) {
|
---|
43 | cout << "failed" << endl;
|
---|
44 | cout << "Failed to extract the search4j tool to '" << jarfile << "'" << endl;
|
---|
45 | cout << "The file may be corrupt or missing" << endl;
|
---|
46 | cout << "Or this installer may not have sufficient file permissions to write it to disk" << endl;
|
---|
47 | cout << "Exiting" << endl;
|
---|
48 | return 1;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | //change to the temp directory
|
---|
53 | chdir( tempdir.c_str() );
|
---|
54 |
|
---|
55 |
|
---|
56 | //check if an appropriate java is found
|
---|
57 | bool jvmFound = (system( "./search4j -m @java.min.version@ -h ./@java.extracted@" ) == 0);
|
---|
58 |
|
---|
59 | //if the jvm was not found, try to fix it and find it
|
---|
60 | if ( !jvmFound ) {
|
---|
61 | //did not find a good java
|
---|
62 | cout << "Greenstone requires java @java.min.version@ or greater" << endl;
|
---|
63 |
|
---|
64 | //tell them if java is absent or just too old
|
---|
65 | if ( system( "./search4j -h ./@java.extracted@" ) == 0 ) {
|
---|
66 | cout << "Your java is too old." << endl;
|
---|
67 | } else {
|
---|
68 | cout << "Could not find java." << endl;
|
---|
69 | }
|
---|
70 |
|
---|
71 | //is this an installer with the bundled JRE?
|
---|
72 |
|
---|
73 | #ifdef java_is_bundled
|
---|
74 | //yes, JRE is bundled
|
---|
75 | cout
|
---|
76 | << "This installer comes bundled with a suitible version of java: " << endl
|
---|
77 | << " @java.installer@" << endl
|
---|
78 | << "Do you want to install this java? (y/n)" << endl;
|
---|
79 | char r[1024]; cin >> r;
|
---|
80 | if ( strcmp( r, "y" ) == 0 || strcmp( r, "Y" ) == 0) {
|
---|
81 | extract_bundled_file( java, sizeof(java), (char*)"@java.installer@", true );
|
---|
82 | system( "./@java.installer@" );
|
---|
83 | jvmFound = true; //assume the java installation went well
|
---|
84 | }
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | #ifndef java_is_bundled
|
---|
88 | //no, JRE is not bundled
|
---|
89 | cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl;
|
---|
90 | cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl;
|
---|
91 | #endif
|
---|
92 | }
|
---|
93 |
|
---|
94 | //if we have found it by now, launch the installer
|
---|
95 | if ( jvmFound ) {
|
---|
96 | cout << "Launching Installer ..." << endl;
|
---|
97 | int launch_exit_code = 0;
|
---|
98 | launch_exit_code = system("./search4j -m @java.min.version@ -l greenstone3.jar -h ./@java.extracted@");
|
---|
99 |
|
---|
100 | //report how it went
|
---|
101 | if ( launch_exit_code == 0 ) {
|
---|
102 | cout << "Setup complete" << endl;
|
---|
103 | } else {
|
---|
104 | cout << "Still could not find a suitible version of java" << endl;
|
---|
105 | cout << "Please install java, set JAVA_HOME or JRE_HOME, and try again" << endl;
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | //change back to the original directory
|
---|
111 | chdir("..");
|
---|
112 |
|
---|
113 | //delete the temp files
|
---|
114 | cout << "Deleting the temp directory" << endl;
|
---|
115 | system( ("rm -rf " + tempdir).c_str() );
|
---|
116 |
|
---|
117 |
|
---|
118 | return 0;
|
---|
119 |
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool extract_bundled_file( const char data[], int size, char* filename, bool make_executable) {
|
---|
123 |
|
---|
124 | if ( size == 0 ) return false;
|
---|
125 |
|
---|
126 | //delete the file if it exists
|
---|
127 | remove( filename );
|
---|
128 |
|
---|
129 | //open the file
|
---|
130 | fstream binary_file( filename, ios::out|ios::binary );
|
---|
131 | if ( !binary_file.good() ) {
|
---|
132 | binary_file.close();
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 |
|
---|
136 | //write the file
|
---|
137 | binary_file.write( data, size );
|
---|
138 | if ( !binary_file.good() ) {
|
---|
139 | binary_file.close();
|
---|
140 | return false;
|
---|
141 | }
|
---|
142 |
|
---|
143 | //close the file
|
---|
144 | binary_file.close();
|
---|
145 | if ( !binary_file.good() ) {
|
---|
146 | binary_file.close();
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if ( make_executable ) {
|
---|
151 | string command = "chmod a+x " + string(filename);
|
---|
152 | system( command.c_str() );
|
---|
153 | }
|
---|
154 |
|
---|
155 | return true;
|
---|
156 |
|
---|
157 | }
|
---|