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

Revision 18283, 4.3 KB (checked in by oranfry, 11 years ago)

use new libsearch4j spec and unzip sqlite package on all platrforms

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