#include #include #include using namespace std; #include "wrapper.h" #include "libsearch4j.h" bool extract_bundled_file( const char[], int, char*, bool ); int main(int argc, char** argv) { string tempdir = "@installer.name@.tmp"; //temporary directory where we will store extracted files string jarfile = tempdir + "/@installer.name@.jar"; //where we will store the jar file string javafile = tempdir + "/@java.installer@"; //where we will store the java tar 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( @installer.name@jar, sizeof(@installer.name@jar), (char*)jarfile.c_str(), false); #ifdef java_is_bundled cout << "Extracting bundled java..." << endl; succeeded = extract_bundled_file( java, sizeof(java), (char*)javafile.c_str(), true ) && succeeded; #endif if ( !succeeded ) { cout << "Failed to extract one or more resources" << endl; cout << "This installer may not have sufficient file permissions to write it to disk" << endl; cout << "Or, the files may be corrupt or missing from this executable" << endl; cout << "Exiting" << endl; return 1; } //change to the temp directory chdir( tempdir.c_str() ); #ifdef java_is_bundled succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0); succeeded = succeeded && ( system( "'tar -xf jre.tar'" ) == 0 ); if ( !succeeded ) { cout << "Failed to extract the bundled java archive to the temp directory" << endl; cout << "You need the tar program on your PATH" << endl; cout << "Exiting" << endl; return 1; } #endif //check if an appropriate java is found Jvm minimum; minimum.setVersionFromString( "@java.min.version@" ); string phint = "./@java.extracted@"; string hint = ""; Jvm foundJvm; bool jvmFound = find( foundJvm, true, minimum, phint, hint, false ); //if the jvm was not found, report not found 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 Jvm tmpJvm; if ( find( tmpJvm, false, minimum, phint, hint, false ) ) { cout << "You have java, but it is too old." << endl; } else { cout << "Could not find java on your system." << endl; } cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl; #ifndef java_is_bundled cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl; #endif //if we have found it, launch the installer } else { cout << "Launching Installer ..." << endl; int launch_exit_code = 0; launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx85M -jar @installer.name@.jar").c_str() ); //report how it went if ( launch_exit_code == 0 ) { cout << "Setup complete" << endl; } else { cout << "The installer exited with an error" << endl; cout << "Greenstone may not be correctly installed" << 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; }