1 | #include <windows.h>
|
---|
2 |
|
---|
3 | #include <fstream>
|
---|
4 | #include <iostream>
|
---|
5 | #include <conio.h>
|
---|
6 | #include <string>
|
---|
7 | #include <time.h>
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | void WriteUsingStream(BYTE* pBytes, int nCount, char* filename)
|
---|
12 | {
|
---|
13 | ofstream fStream(filename, ios::binary | ios::out);
|
---|
14 |
|
---|
15 | fStream.write((char*) pBytes, nCount);
|
---|
16 | fStream.close();
|
---|
17 | }
|
---|
18 |
|
---|
19 | int extractResource(const char * name, const char * type, char * file) {
|
---|
20 |
|
---|
21 | HMODULE hModule = GetModuleHandle(NULL);
|
---|
22 |
|
---|
23 | HRSRC hRsrc = FindResource(hModule, name, type);
|
---|
24 | if (hRsrc == NULL) return 1; //couldn't find
|
---|
25 |
|
---|
26 | HGLOBAL hGlobal = LoadResource(hModule, hRsrc);
|
---|
27 | if (hGlobal == NULL) return 1; //couldn't lock
|
---|
28 |
|
---|
29 | BYTE* pBytes = (BYTE*) LockResource(hGlobal);
|
---|
30 | if (pBytes == NULL) return 1; //couldn't lock
|
---|
31 |
|
---|
32 | DWORD dwLength = SizeofResource(hModule, hRsrc);
|
---|
33 | WriteUsingStream(pBytes, dwLength, file);
|
---|
34 | UnlockResource(hGlobal);
|
---|
35 |
|
---|
36 | FreeResource(hGlobal);
|
---|
37 |
|
---|
38 | return 0;
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | void cleanup( string dir, string file ) {
|
---|
43 | string command = "if EXIST ";
|
---|
44 | command.append( dir );
|
---|
45 | command.append( "\\" );
|
---|
46 | command.append( file );
|
---|
47 | command.append( " del " );
|
---|
48 | command.append( dir );
|
---|
49 | command.append( "\\" );
|
---|
50 | command.append( file );
|
---|
51 | system( command.c_str() );
|
---|
52 | }
|
---|
53 |
|
---|
54 | void main() {
|
---|
55 | string command;
|
---|
56 | srand( time(0) );
|
---|
57 |
|
---|
58 | cout << "Greenstone3 Installer" << endl;
|
---|
59 |
|
---|
60 | //create the temp folder
|
---|
61 | char id[6]; for( int i=0; i<5; i++ ) id[i] = (char)((rand()%26) + 97); id[5] = '\0'; //id for this instance
|
---|
62 | string tempdir = "greenstone3.tmp"; tempdir.append( id ); //the temp dir
|
---|
63 |
|
---|
64 | cout << "Creating temp folder '";
|
---|
65 | cout << tempdir;
|
---|
66 | cout << "' ..." << endl;
|
---|
67 | command = "mkdir "; command.append(tempdir);
|
---|
68 | system( command.c_str() );
|
---|
69 |
|
---|
70 | //extract the resources
|
---|
71 | cout << "Extracting search4j tool ..." << endl;
|
---|
72 | string search4jLocation = ""; search4jLocation.append( tempdir ); search4jLocation.append( "\\search4j.exe" );
|
---|
73 | extractResource( "SEARCH4J", "EXE", (char*) search4jLocation.c_str() );
|
---|
74 |
|
---|
75 | cout << "Extracting jar installer ..." << endl;
|
---|
76 | string jarLocation = ""; jarLocation.append( tempdir ); jarLocation.append( "\\greenstone3.jar" );
|
---|
77 | extractResource( "GREENSTONE_JAR", "JAR", (char*) jarLocation.c_str() );
|
---|
78 |
|
---|
79 | //change to the temp directory
|
---|
80 | SetCurrentDirectory( tempdir.c_str() );
|
---|
81 |
|
---|
82 | //check if an appropriate java is found
|
---|
83 | bool jvmFound = (system( "search4j.exe -m @java.min.version@" ) == 0);
|
---|
84 |
|
---|
85 | //if the jvm was not found, try to fix it and find it
|
---|
86 | if ( !jvmFound ) {
|
---|
87 | //did not find a good java
|
---|
88 | cout << "Greenstone requires java @java.min.version@ or greater" << endl;
|
---|
89 |
|
---|
90 | //tell them if java is absent or just too old
|
---|
91 | if ( system( "search4j.exe" ) == 0 ) {
|
---|
92 | cout << "Your java is too old." << endl;
|
---|
93 | } else {
|
---|
94 | cout << "Could not find java." << endl;
|
---|
95 | }
|
---|
96 |
|
---|
97 | //is this an installer with the bundled JRE?
|
---|
98 | cout << "Checking for bundled java ..." << endl;
|
---|
99 | int extract_result = extractResource( "JAVA", "EXE", "@java.installer@" );
|
---|
100 | if ( extract_result == 0 ) {
|
---|
101 | //yes, JRE is bundled
|
---|
102 | cout
|
---|
103 | << "This installer comes bundled with a suitible version of java: " << endl
|
---|
104 | << " @java.installer@" << endl
|
---|
105 | << "Do you want to install this java? (y/n)" << endl;
|
---|
106 | char r[1024]; cin >> r;
|
---|
107 | if ( _stricmp( r, "y" ) == 0 ) {
|
---|
108 | system( "@java.installer@" );
|
---|
109 | jvmFound = true; //assume the java installation went well
|
---|
110 | }
|
---|
111 | } else {
|
---|
112 | //no, JRE is not bundled
|
---|
113 | cout << "Sorry, there is no bundled java with this installer" << endl;
|
---|
114 | cout << "Install java (@java.min.version@ or newer) and try again" << endl;
|
---|
115 | cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | //if we have found it by now, launch the installer
|
---|
120 | if ( jvmFound ) {
|
---|
121 | //launch the jar with search4j, taking a note of the exit code
|
---|
122 | cout << "Launching Installer ..." << endl;
|
---|
123 | int launch_exit_code = 0;
|
---|
124 | launch_exit_code = system("search4j.exe -m @java.min.version@ -l greenstone3.jar");
|
---|
125 |
|
---|
126 | //report how it went
|
---|
127 | if ( launch_exit_code == 0 ) {
|
---|
128 | cout << "Setup complete" << endl;
|
---|
129 | } else {
|
---|
130 | cout << "Still could not find a suitible version of java" << endl;
|
---|
131 | cout << "Please install java and try again" << endl;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | //change back to the original directory and clean up the temp directory
|
---|
136 | SetCurrentDirectory("..");
|
---|
137 |
|
---|
138 | cleanup(tempdir, "greenstone3.jar" );
|
---|
139 | cleanup(tempdir, "search4j.exe");
|
---|
140 | cleanup(tempdir, "ant.install.log");
|
---|
141 | cleanup(tempdir, "@java.installer@");
|
---|
142 |
|
---|
143 | command = "rmdir ";
|
---|
144 | command.append( tempdir );
|
---|
145 | system( command.c_str() );
|
---|
146 | cout << "Press any key to exit..." << endl;
|
---|
147 | getch();
|
---|
148 |
|
---|
149 | }
|
---|