[15972] | 1 | #include <iostream>
|
---|
| 2 | #include <fstream>
|
---|
| 3 | #include <iomanip>
|
---|
[19594] | 4 | #include <cstring>
|
---|
[19027] | 5 | #include <cstdlib>
|
---|
[15972] | 6 | using namespace std;
|
---|
| 7 |
|
---|
[19594] | 8 | #ifdef WEB
|
---|
[15975] | 9 | #include "wrapper.h"
|
---|
[16207] | 10 | #include "libsearch4j.h"
|
---|
[15972] | 11 | bool extract_bundled_file( const char[], int, char*, bool );
|
---|
[19594] | 12 | #endif
|
---|
[15972] | 13 |
|
---|
| 14 | int main(int argc, char** argv) {
|
---|
| 15 |
|
---|
[19510] | 16 | char owd[1024];
|
---|
| 17 | getcwd(owd, sizeof(owd));
|
---|
[19027] | 18 | string scratch = (getenv("TMPDIR") == NULL) ? "/tmp" : getenv("TMPDIR");
|
---|
[19594] | 19 | string tempdir = scratch + "/greenstone-installer.tmp"; //temporary directory where we will store extracted files
|
---|
[15972] | 20 | bool succeeded = false;
|
---|
[18877] | 21 | bool textMode = false;
|
---|
[15972] | 22 |
|
---|
[18877] | 23 | //parse arguments
|
---|
| 24 | for ( int i=0; i<argc; i++ ) {
|
---|
| 25 | if ( strcmp( argv[i], "text" ) == 0 ) {
|
---|
| 26 | textMode = true;
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 |
|
---|
[15972] | 31 | //create the temp folder
|
---|
| 32 | cout << "Creating temp directory..." << endl;
|
---|
| 33 | succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
|
---|
| 34 | if ( !succeeded ) {
|
---|
[19327] | 35 | cout << "Failed to create the temp directory '" << tempdir << "'. " <<
|
---|
| 36 | "Check that it does not already exist. Also check that you have write " <<
|
---|
| 37 | "permissions to '" << scratch << "', or set the environment variable TMPDIR to " <<
|
---|
| 38 | "a directory where you do have write permissions." <<
|
---|
| 39 | endl;
|
---|
| 40 |
|
---|
[15972] | 41 | cout << "Exiting" << endl;
|
---|
| 42 | return 1;
|
---|
| 43 | }
|
---|
[19594] | 44 |
|
---|
| 45 | #ifdef WEB
|
---|
[15972] | 46 |
|
---|
[19594] | 47 | string jarfile = tempdir + "/@[email protected]"; //where we will store the jar file
|
---|
| 48 | string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
|
---|
| 49 |
|
---|
| 50 |
|
---|
[15972] | 51 | //extract files
|
---|
| 52 | cout << "Extracting installer jar..." << endl;
|
---|
[15976] | 53 | succeeded = extract_bundled_file( @installer.name@jar, sizeof(@installer.name@jar), (char*)jarfile.c_str(), false);
|
---|
[15972] | 54 |
|
---|
| 55 | #ifdef java_is_bundled
|
---|
[17770] | 56 | cout << "Preparing Greenstone installer..." << endl;
|
---|
[15972] | 57 | succeeded = extract_bundled_file( java, sizeof(java), (char*)javafile.c_str(), true ) && succeeded;
|
---|
| 58 | #endif
|
---|
| 59 |
|
---|
| 60 | if ( !succeeded ) {
|
---|
| 61 | cout << "Failed to extract one or more resources" << endl;
|
---|
| 62 | cout << "This installer may not have sufficient file permissions to write it to disk" << endl;
|
---|
| 63 | cout << "Or, the files may be corrupt or missing from this executable" << endl;
|
---|
| 64 | cout << "Exiting" << endl;
|
---|
| 65 | return 1;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[19594] | 68 | #endif
|
---|
| 69 |
|
---|
[15972] | 70 | //change to the temp directory
|
---|
| 71 | chdir( tempdir.c_str() );
|
---|
| 72 |
|
---|
[19594] | 73 |
|
---|
| 74 | #ifdef WEB
|
---|
[15972] | 75 | #ifdef java_is_bundled
|
---|
[17761] | 76 | succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0);
|
---|
[17763] | 77 | succeeded = succeeded && ( system( "tar -xf jre.tar" ) == 0 );
|
---|
[15972] | 78 | if ( !succeeded ) {
|
---|
| 79 | cout << "Failed to extract the bundled java archive to the temp directory" << endl;
|
---|
| 80 | cout << "You need the tar program on your PATH" << endl;
|
---|
| 81 | cout << "Exiting" << endl;
|
---|
| 82 | return 1;
|
---|
| 83 | }
|
---|
| 84 | #endif
|
---|
| 85 |
|
---|
| 86 | //check if an appropriate java is found
|
---|
[16207] | 87 | Jvm minimum; minimum.setVersionFromString( "@java.min.version@" );
|
---|
[17306] | 88 | string phint = "./@java.extracted@";
|
---|
| 89 | string hint = "";
|
---|
[16207] | 90 | Jvm foundJvm;
|
---|
[18283] | 91 | bool jvmFound = find( foundJvm, true, minimum, false, false, phint, hint, false );
|
---|
[15972] | 92 |
|
---|
| 93 | //if the jvm was not found, report not found
|
---|
| 94 | if ( !jvmFound ) {
|
---|
| 95 |
|
---|
| 96 | //did not find a good java
|
---|
| 97 | cout << "Greenstone requires java @java.min.version@ or greater." << endl;
|
---|
| 98 |
|
---|
[16207] | 99 | //tell them if java is absent or just too old
|
---|
| 100 | Jvm tmpJvm;
|
---|
[18283] | 101 | if ( find( tmpJvm, false, minimum, false, false, phint, hint, false ) ) {
|
---|
[15972] | 102 | cout << "You have java, but it is too old." << endl;
|
---|
| 103 | } else {
|
---|
| 104 | cout << "Could not find java on your system." << endl;
|
---|
[16207] | 105 | }
|
---|
[16208] | 106 |
|
---|
[15972] | 107 | cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl;
|
---|
| 108 | #ifndef java_is_bundled
|
---|
| 109 | cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl;
|
---|
| 110 | #endif
|
---|
| 111 |
|
---|
| 112 | //if we have found it, launch the installer
|
---|
| 113 | } else {
|
---|
| 114 |
|
---|
| 115 | cout << "Launching Installer ..." << endl;
|
---|
| 116 | int launch_exit_code = 0;
|
---|
[19510] | 117 | launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx85M \"-Dorig.dir=" + owd + "\" -jar @[email protected]" + (textMode?" text":"") ).c_str() );
|
---|
[15972] | 118 |
|
---|
| 119 | //report how it went
|
---|
| 120 | if ( launch_exit_code == 0 ) {
|
---|
| 121 | cout << "Setup complete" << endl;
|
---|
| 122 | } else {
|
---|
| 123 | cout << "The installer exited with an error" << endl;
|
---|
| 124 | cout << "Greenstone may not be correctly installed" << endl;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | }
|
---|
[19594] | 128 | #endif
|
---|
[15972] | 129 |
|
---|
[19594] | 130 | #ifdef CDROM
|
---|
| 131 | string cmd = "";
|
---|
| 132 | cmd.append( owd );
|
---|
| 133 | cmd.append( "/Java/Linux/jre/bin/java -Dorig.dir=\"" );
|
---|
| 134 | cmd.append( owd );
|
---|
| 135 | cmd.append( "\" -jar " );
|
---|
| 136 | cmd.append( owd );
|
---|
| 137 | cmd.append( "/Java/Jars/linux.jar" );
|
---|
| 138 | system( cmd.c_str() );
|
---|
| 139 | #endif
|
---|
| 140 |
|
---|
[19027] | 141 | //change to the scratch dir for the following operation
|
---|
[19034] | 142 | chdir(scratch.c_str());
|
---|
[15972] | 143 |
|
---|
| 144 | //delete the temp files
|
---|
| 145 | cout << "Deleting the temp directory" << endl;
|
---|
| 146 | system( ("rm -rf " + tempdir).c_str() );
|
---|
| 147 |
|
---|
| 148 | return 0;
|
---|
| 149 |
|
---|
| 150 | }
|
---|
[19594] | 151 | #ifdef WEB
|
---|
[15972] | 152 | bool extract_bundled_file( const char data[], int size, char* filename, bool make_executable) {
|
---|
| 153 |
|
---|
| 154 | if ( size == 0 ) return false;
|
---|
| 155 |
|
---|
| 156 | //delete the file if it exists
|
---|
| 157 | remove( filename );
|
---|
| 158 |
|
---|
| 159 | //open the file
|
---|
| 160 | fstream binary_file( filename, ios::out|ios::binary );
|
---|
| 161 | if ( !binary_file.good() ) {
|
---|
| 162 | binary_file.close();
|
---|
| 163 | return false;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | //write the file
|
---|
| 167 | binary_file.write( data, size );
|
---|
| 168 | if ( !binary_file.good() ) {
|
---|
| 169 | binary_file.close();
|
---|
| 170 | return false;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | //close the file
|
---|
| 174 | binary_file.close();
|
---|
| 175 | if ( !binary_file.good() ) {
|
---|
| 176 | binary_file.close();
|
---|
| 177 | return false;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | if ( make_executable ) {
|
---|
| 181 | string command = "chmod a+x " + string(filename);
|
---|
| 182 | system( command.c_str() );
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | return true;
|
---|
| 186 |
|
---|
| 187 | }
|
---|
[19594] | 188 | #endif
|
---|