#include #include #include using namespace std; #include "mywrapper.h" bool extract_bundled_file( const char[], int, char*, bool ); int main(int argc, char** argv) { string tempdir = (string)argv[0] + ".tmp"; //temporary directory where we will store extracted files string jarfile = tempdir + "/greenstone3.jar"; //where we will store the jar file string search4jfile = tempdir + "/search4j"; //where we will store the jar file bool succeeded = false; //create the temp folder cout << "Creating temp directory" << endl; succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) ); if ( !succeeded ) { cout << "Failed to write to create the temp directory '" << tempdir << "'" << endl; cout << "Check that it does not already exist and try again" << endl; cout << "Exiting" << endl; return 1; } //extract files cout << "Extracting installer jar..." << endl; succeeded = extract_bundled_file( greenstone3jar, sizeof(greenstone3jar), (char*)jarfile.c_str(), false); if ( !succeeded ) { cout << "failed" << endl; cout << "Failed to extract the JAR file to '" << jarfile << "'" << endl; cout << "The file may be corrupt or missing" << endl; cout << "Or this installer may not have sufficient file permissions to write it to disk" << endl; cout << "Exiting" << endl; return 1; } cout << "Extracting search4j tool..." << endl; succeeded = extract_bundled_file( search4j, sizeof(search4j), (char*)search4jfile.c_str(), true ); if ( !succeeded ) { cout << "failed" << endl; cout << "Failed to extract the search4j tool to '" << jarfile << "'" << endl; cout << "The file may be corrupt or missing" << endl; cout << "Or this installer may not have sufficient file permissions to write it to disk" << endl; cout << "Exiting" << endl; return 1; } //change to the temp directory chdir( tempdir.c_str() ); //check if an appropriate java is found bool jvmFound = (system( "./search4j -m @java.min.version@ -h ./@java.extracted@" ) == 0); //if the jvm was not found, try to fix it and find it if ( !jvmFound ) { //did not find a good java cout << "Greenstone requires java @java.min.version@ or greater" << endl; //tell them if java is absent or just too old if ( system( "./search4j -h ./@java.extracted@" ) == 0 ) { cout << "Your java is too old." << endl; } else { cout << "Could not find java on your system." << endl; } //is this an installer with the bundled JRE? #ifdef java_is_bundled //yes, JRE is bundled /* cout << "This installer comes bundled with a suitible version of java: " << endl << " @java.installer@" << endl << "Do you want to install this java? (y/n)" << endl; char r[1024]; cin >> r; if ( strcmp( r, "y" ) == 0 || strcmp( r, "Y" ) == 0) { */ cout << "Using bundled java." << endl; extract_bundled_file( java, sizeof(java), (char*)"@java.installer@", true ); system( "tar -xzf @java.installer@" ); jvmFound = true; //assume the java installation went well /*}*/ #endif #ifndef java_is_bundled //no, JRE is not bundled cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl; cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl; #endif } //if we have found it by now, launch the installer if ( jvmFound ) { cout << "Launching Installer ..." << endl; int launch_exit_code = 0; launch_exit_code = system("./search4j -m @java.min.version@ -l greenstone3.jar -h ./@java.extracted@"); //report how it went if ( launch_exit_code == 0 ) { cout << "Setup complete" << endl; } else { cout << "Still could not find a suitible version of java" << endl; cout << "Please install java, set JAVA_HOME or JRE_HOME, and try again" << endl; } } //change back to the original directory chdir(".."); //delete the temp files cout << "Deleting the temp directory" << endl; system( ("rm -rf " + tempdir).c_str() ); return 0; } bool extract_bundled_file( const char data[], int size, char* filename, bool make_executable) { if ( size == 0 ) return false; //delete the file if it exists remove( filename ); //open the file fstream binary_file( filename, ios::out|ios::binary ); if ( !binary_file.good() ) { binary_file.close(); return false; } //write the file binary_file.write( data, size ); if ( !binary_file.good() ) { binary_file.close(); return false; } //close the file binary_file.close(); if ( !binary_file.good() ) { binary_file.close(); return false; } if ( make_executable ) { string command = "chmod a+x " + string(filename); system( command.c_str() ); } return true; }