[14982] | 1 | #include <iostream>
|
---|
| 2 | #include <fstream>
|
---|
| 3 | #include <iomanip>
|
---|
[15142] | 4 |
|
---|
[14982] | 5 | using namespace std;
|
---|
| 6 |
|
---|
| 7 | #include "mywrapper.h"
|
---|
| 8 |
|
---|
[15142] | 9 | bool extract_bundled_file( const char[], int, char*, bool );
|
---|
[14982] | 10 |
|
---|
| 11 | int main(int argc, char** argv) {
|
---|
| 12 |
|
---|
| 13 | string tempdir = (string)argv[0] + ".tmp"; //temporary directory where we will store extracted files
|
---|
[15980] | 14 | string jarfile = tempdir + "/greenstone2.jar"; //where we will store the jar file
|
---|
[15142] | 15 | string search4jfile = tempdir + "/search4j"; //where we will store the jar file
|
---|
[15980] | 16 | string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
|
---|
[14982] | 17 | bool succeeded = false;
|
---|
| 18 |
|
---|
| 19 | //create the temp folder
|
---|
[15980] | 20 | cout << "Creating temp directory..." << endl;
|
---|
[14982] | 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
|
---|
[15142] | 30 | cout << "Extracting installer jar..." << endl;
|
---|
[15980] | 31 | succeeded = extract_bundled_file( greenstone2jar, sizeof(greenstone2jar), (char*)jarfile.c_str(), false);
|
---|
[14982] | 32 |
|
---|
[15142] | 33 | cout << "Extracting search4j tool..." << endl;
|
---|
[15980] | 34 | succeeded = extract_bundled_file( search4j, sizeof(search4j), (char*)search4jfile.c_str(), true ) && succeeded;
|
---|
| 35 |
|
---|
| 36 | #ifdef java_is_bundled
|
---|
| 37 | cout << "Extracting bundled java..." << endl;
|
---|
| 38 | succeeded = extract_bundled_file( java, sizeof(java), (char*)javafile.c_str(), true ) && succeeded;
|
---|
| 39 | #endif
|
---|
| 40 |
|
---|
[15142] | 41 | if ( !succeeded ) {
|
---|
[15980] | 42 | cout << "Failed to extract one or more resources" << endl;
|
---|
| 43 | cout << "This installer may not have sufficient file permissions to write it to disk" << endl;
|
---|
| 44 | cout << "Or, the files may be corrupt or missing from this executable" << endl;
|
---|
[15142] | 45 | cout << "Exiting" << endl;
|
---|
| 46 | return 1;
|
---|
| 47 | }
|
---|
[14982] | 48 |
|
---|
[15142] | 49 | //change to the temp directory
|
---|
| 50 | chdir( tempdir.c_str() );
|
---|
| 51 |
|
---|
[15980] | 52 | #ifdef java_is_bundled
|
---|
| 53 | succeeded = (system( "tar -xzf @java.installer@" ) == 0);
|
---|
| 54 | if ( !succeeded ) {
|
---|
| 55 | cout << "Failed to extract the bundled java archive to the temp directory" << endl;
|
---|
| 56 | cout << "You need the tar program on your PATH" << endl;
|
---|
| 57 | cout << "Exiting" << endl;
|
---|
| 58 | return 1;
|
---|
| 59 | }
|
---|
| 60 | #endif
|
---|
[15142] | 61 |
|
---|
| 62 | //check if an appropriate java is found
|
---|
| 63 | bool jvmFound = (system( "./search4j -m @java.min.version@ -h ./@java.extracted@" ) == 0);
|
---|
| 64 |
|
---|
[15980] | 65 | //if the jvm was not found, report not found
|
---|
[15142] | 66 | if ( !jvmFound ) {
|
---|
[15980] | 67 |
|
---|
[15142] | 68 | //did not find a good java
|
---|
[15980] | 69 | cout << "Greenstone requires java @java.min.version@ or greater." << endl;
|
---|
[15142] | 70 |
|
---|
| 71 | //tell them if java is absent or just too old
|
---|
| 72 | if ( system( "./search4j -h ./@java.extracted@" ) == 0 ) {
|
---|
[15980] | 73 | cout << "You have java, but it is too old." << endl;
|
---|
[15142] | 74 | } else {
|
---|
[15205] | 75 | cout << "Could not find java on your system." << endl;
|
---|
[15142] | 76 | }
|
---|
[15205] | 77 |
|
---|
[15980] | 78 | cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl;
|
---|
| 79 | #ifndef java_is_bundled
|
---|
| 80 | cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl;
|
---|
| 81 | #endif
|
---|
[15205] | 82 |
|
---|
[15980] | 83 | //if we have found it, launch the installer
|
---|
| 84 | } else {
|
---|
[15142] | 85 |
|
---|
| 86 | cout << "Launching Installer ..." << endl;
|
---|
| 87 | int launch_exit_code = 0;
|
---|
[15980] | 88 | launch_exit_code = system("./search4j -m @java.min.version@ -l greenstone2.jar -h ./@java.extracted@");
|
---|
[15142] | 89 |
|
---|
| 90 | //report how it went
|
---|
| 91 | if ( launch_exit_code == 0 ) {
|
---|
| 92 | cout << "Setup complete" << endl;
|
---|
| 93 | } else {
|
---|
[15980] | 94 | cout << "The installer exited with an error" << endl;
|
---|
| 95 | cout << "Greenstone may not be correctly installed" << endl;
|
---|
[15142] | 96 | }
|
---|
| 97 |
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | //change back to the original directory
|
---|
| 101 | chdir("..");
|
---|
| 102 |
|
---|
[14982] | 103 | //delete the temp files
|
---|
| 104 | cout << "Deleting the temp directory" << endl;
|
---|
| 105 | system( ("rm -rf " + tempdir).c_str() );
|
---|
| 106 |
|
---|
| 107 | return 0;
|
---|
| 108 |
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[15142] | 111 | bool extract_bundled_file( const char data[], int size, char* filename, bool make_executable) {
|
---|
[14982] | 112 |
|
---|
[15142] | 113 | if ( size == 0 ) return false;
|
---|
| 114 |
|
---|
[14982] | 115 | //delete the file if it exists
|
---|
| 116 | remove( filename );
|
---|
| 117 |
|
---|
| 118 | //open the file
|
---|
| 119 | fstream binary_file( filename, ios::out|ios::binary );
|
---|
| 120 | if ( !binary_file.good() ) {
|
---|
| 121 | binary_file.close();
|
---|
| 122 | return false;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | //write the file
|
---|
| 126 | binary_file.write( data, size );
|
---|
| 127 | if ( !binary_file.good() ) {
|
---|
| 128 | binary_file.close();
|
---|
| 129 | return false;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | //close the file
|
---|
| 133 | binary_file.close();
|
---|
| 134 | if ( !binary_file.good() ) {
|
---|
| 135 | binary_file.close();
|
---|
| 136 | return false;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[15142] | 139 | if ( make_executable ) {
|
---|
| 140 | string command = "chmod a+x " + string(filename);
|
---|
| 141 | system( command.c_str() );
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[14982] | 144 | return true;
|
---|
| 145 |
|
---|
| 146 | }
|
---|