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

Revision 19327, 4.7 KB (checked in by oranfry, 11 years ago)

improved error message after changed behavior

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