root/release-kits/shared/linux/wrapper.cpp @ 19594

Revision 19594, 5.1 KB (checked in by oranfry, 11 years ago)

cdrom wrapper needs to use temp dir, so basing it off web wrapper after all

Line 
1#include <iostream>
2#include <fstream>
3#include <iomanip>
4#include <cstring>
5#include <cstdlib>
6using namespace std;
7
8#ifdef WEB
9#include "wrapper.h"
10#include "libsearch4j.h"
11bool extract_bundled_file( const char[], int, char*, bool );
12#endif
13
14int main(int argc, char** argv) {
15
16    char owd[1024];
17    getcwd(owd, sizeof(owd));
18    string scratch = (getenv("TMPDIR") == NULL) ? "/tmp" : getenv("TMPDIR");
19    string tempdir = scratch + "/greenstone-installer.tmp"; //temporary directory where we will store extracted files
20    bool succeeded = false;
21    bool textMode = false;
22
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
31    //create the temp folder
32    cout << "Creating temp directory..." << endl;
33    succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
34    if ( !succeeded ) {
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
41        cout << "Exiting" << endl;
42        return 1;
43    }
44   
45    #ifdef WEB
46
47    string jarfile = tempdir + "/@installer.name@.jar"; //where we will store the jar file
48    string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
49
50
51    //extract files
52    cout << "Extracting installer jar..." << endl;
53    succeeded = extract_bundled_file( @installer.name@jar, sizeof(@installer.name@jar), (char*)jarfile.c_str(), false);
54
55    #ifdef java_is_bundled
56    cout << "Preparing Greenstone installer..." << endl;
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
68    #endif
69
70    //change to the temp directory
71    chdir( tempdir.c_str() );
72
73
74    #ifdef WEB
75    #ifdef java_is_bundled
76    succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0);
77    succeeded = succeeded && ( system( "tar -xf jre.tar" ) == 0 );
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
87    Jvm minimum; minimum.setVersionFromString( "@java.min.version@" );
88    string phint = "./@java.extracted@";
89    string hint = "";
90    Jvm foundJvm;
91    bool jvmFound = find( foundJvm, true, minimum, false, false, phint, hint, false );
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       
99        //tell them if java is absent or just too old
100        Jvm tmpJvm;
101        if ( find( tmpJvm, false, minimum, false, false, phint, hint, false ) ) {
102            cout << "You have java, but it is too old." << endl;
103        } else {
104            cout << "Could not find java on your system." << endl;
105        }
106       
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;
117        launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx85M \"-Dorig.dir=" + owd + "\" -jar @installer.name@.jar" + (textMode?" text":"") ).c_str() );
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    }
128    #endif
129
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
141    //change to the scratch dir for the following operation
142    chdir(scratch.c_str());
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}
151#ifdef WEB
152bool 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}
188#endif
Note: See TracBrowser for help on using the browser.