source: release-kits/lirk2/wrapper/mywrapper.c@ 15980

Last change on this file since 15980 was 15980, checked in by oranfry, 16 years ago

have lirk2 get the linux general stuff

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