[15972] | 1 | #include <iostream>
|
---|
| 2 | #include <fstream>
|
---|
| 3 | #include <iomanip>
|
---|
[19720] | 4 | #include <string.h>
|
---|
[19027] | 5 | #include <cstdlib>
|
---|
[19720] | 6 | #include <sstream>
|
---|
[15972] | 7 | using namespace std;
|
---|
| 8 |
|
---|
[19720] | 9 | #ifndef CDROM
|
---|
[15975] | 10 | #include "wrapper.h"
|
---|
[16207] | 11 | #include "libsearch4j.h"
|
---|
[15972] | 12 | bool extract_bundled_file( const char[], int, char*, bool );
|
---|
[19765] | 13 | #else
|
---|
[19938] | 14 | bool prep_java( string wd );
|
---|
[19594] | 15 | #endif
|
---|
[15972] | 16 |
|
---|
[19720] | 17 | void usage( char* progname, int exitvalue = -1 ) {
|
---|
| 18 | cerr
|
---|
| 19 | << "Usage: " << progname << " [OPTIONS]" << endl
|
---|
| 20 | << "OPTIONS: " << endl
|
---|
| 21 | << " -h, -help Display this usage message" << endl
|
---|
| 22 | << " -text Do a non-graphical command line installation" << endl;
|
---|
| 23 | #ifdef CDROM
|
---|
| 24 | cerr << " -wd PATH Look for the CD-ROM at PATH" << endl;
|
---|
| 25 | #endif
|
---|
| 26 |
|
---|
| 27 | exit( exitvalue );
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[15972] | 30 | int main(int argc, char** argv) {
|
---|
| 31 |
|
---|
[19720] | 32 | char wdc[1024];
|
---|
| 33 | getcwd(wdc, sizeof(wdc));
|
---|
| 34 | string wd = "";
|
---|
| 35 | wd.append( wdc );
|
---|
| 36 |
|
---|
[19027] | 37 | string scratch = (getenv("TMPDIR") == NULL) ? "/tmp" : getenv("TMPDIR");
|
---|
[19594] | 38 | string tempdir = scratch + "/greenstone-installer.tmp"; //temporary directory where we will store extracted files
|
---|
[15972] | 39 | bool succeeded = false;
|
---|
[19720] | 40 | bool text_mode = false;
|
---|
[15972] | 41 |
|
---|
[18877] | 42 | //parse arguments
|
---|
[19720] | 43 | string a;
|
---|
| 44 | for ( int i=1; i<argc; i++ ) {
|
---|
| 45 | a = argv[i];
|
---|
| 46 | if ( a.compare("-wd") == 0 ) {
|
---|
| 47 | wd = "";
|
---|
| 48 | wd.append( argv[++i] );
|
---|
| 49 | } else if ( a.compare("-h") == 0 || a.compare("-help") == 0 || a.compare("--help") == 0) {
|
---|
| 50 | usage( argv[0], 0 );
|
---|
| 51 | } else if ( a.compare("-text") == 0 || a.compare("text") == 0 ) {
|
---|
| 52 | text_mode = true;
|
---|
| 53 | } else {
|
---|
| 54 | cerr << "Unrecognised option '" << a << "'" << endl;
|
---|
| 55 | usage(argv[0]);
|
---|
[18877] | 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[19720] | 59 | #ifdef CDROM
|
---|
| 60 | //ensure working directory is correct
|
---|
| 61 | FILE* pFile;
|
---|
| 62 | pFile = fopen( (wd+"/Software/core/all/uninst.jar").c_str(), "r");
|
---|
| 63 | if ( pFile == NULL ) {
|
---|
| 64 | cerr << "Could not find cdrom at '" << wd << "'." << endl;
|
---|
| 65 | cerr << "Please enter the path the CDROM [/media/cdrom]: ";
|
---|
| 66 | getline(cin, wd);
|
---|
| 67 | if ( wd.compare("") == 0 ) {
|
---|
| 68 | wd = "/media/cdrom";
|
---|
| 69 | }
|
---|
| 70 | } else {
|
---|
| 71 | fclose(pFile);
|
---|
| 72 | }
|
---|
[18877] | 73 |
|
---|
[19720] | 74 | pFile = fopen( (wd+"/Software/core/all/uninst.jar").c_str(), "r");
|
---|
| 75 | if ( pFile == NULL ) {
|
---|
| 76 | cerr << "Could not find cdrom at '"<< wd << "'." << endl;
|
---|
| 77 | cerr << "Please run the installer again" << endl;
|
---|
| 78 | exit(-1);
|
---|
| 79 | } else {
|
---|
| 80 | fclose(pFile);
|
---|
| 81 | }
|
---|
| 82 | #endif
|
---|
| 83 |
|
---|
[15972] | 84 | //create the temp folder
|
---|
| 85 | cout << "Creating temp directory..." << endl;
|
---|
| 86 | succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
|
---|
| 87 | if ( !succeeded ) {
|
---|
[19720] | 88 | cerr << "Failed to create the temp directory '" << tempdir << "'. " <<
|
---|
[19327] | 89 | "Check that it does not already exist. Also check that you have write " <<
|
---|
| 90 | "permissions to '" << scratch << "', or set the environment variable TMPDIR to " <<
|
---|
[19720] | 91 | "a directory where you do have write permissions." << endl <<
|
---|
| 92 | "Exiting" << endl;
|
---|
[15972] | 93 | return 1;
|
---|
| 94 | }
|
---|
[19594] | 95 |
|
---|
[19720] | 96 | #ifndef CDROM
|
---|
[15972] | 97 |
|
---|
[19594] | 98 | string jarfile = tempdir + "/@[email protected]"; //where we will store the jar file
|
---|
| 99 | string javafile = tempdir + "/@java.installer@"; //where we will store the java tar file
|
---|
| 100 |
|
---|
| 101 |
|
---|
[15972] | 102 | //extract files
|
---|
| 103 | cout << "Extracting installer jar..." << endl;
|
---|
[15976] | 104 | succeeded = extract_bundled_file( @installer.name@jar, sizeof(@installer.name@jar), (char*)jarfile.c_str(), false);
|
---|
[15972] | 105 |
|
---|
| 106 | #ifdef java_is_bundled
|
---|
[17770] | 107 | cout << "Preparing Greenstone installer..." << endl;
|
---|
[15972] | 108 | succeeded = extract_bundled_file( java, sizeof(java), (char*)javafile.c_str(), true ) && succeeded;
|
---|
| 109 | #endif
|
---|
| 110 |
|
---|
| 111 | if ( !succeeded ) {
|
---|
[19720] | 112 | cerr << "Failed to extract one or more resources" << endl;
|
---|
| 113 | cerr << "This installer may not have sufficient file permissions to write it to disk" << endl;
|
---|
| 114 | cerr << "Or, the files may be corrupt or missing from this executable" << endl;
|
---|
| 115 | cerr << "Exiting" << endl;
|
---|
[15972] | 116 | return 1;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[19594] | 119 | #endif
|
---|
| 120 |
|
---|
[15972] | 121 | //change to the temp directory
|
---|
| 122 | chdir( tempdir.c_str() );
|
---|
| 123 |
|
---|
[19594] | 124 |
|
---|
[19720] | 125 | #ifndef CDROM
|
---|
[15972] | 126 | #ifdef java_is_bundled
|
---|
[17761] | 127 | succeeded = (system( "/bin/sh -c ./@java.installer@ > /dev/null" ) == 0);
|
---|
[17763] | 128 | succeeded = succeeded && ( system( "tar -xf jre.tar" ) == 0 );
|
---|
[15972] | 129 | if ( !succeeded ) {
|
---|
| 130 | cout << "Failed to extract the bundled java archive to the temp directory" << endl;
|
---|
| 131 | cout << "You need the tar program on your PATH" << endl;
|
---|
| 132 | cout << "Exiting" << endl;
|
---|
| 133 | return 1;
|
---|
| 134 | }
|
---|
| 135 | #endif
|
---|
| 136 |
|
---|
| 137 | //check if an appropriate java is found
|
---|
[16207] | 138 | Jvm minimum; minimum.setVersionFromString( "@java.min.version@" );
|
---|
[17306] | 139 | string phint = "./@java.extracted@";
|
---|
| 140 | string hint = "";
|
---|
[16207] | 141 | Jvm foundJvm;
|
---|
[18283] | 142 | bool jvmFound = find( foundJvm, true, minimum, false, false, phint, hint, false );
|
---|
[15972] | 143 |
|
---|
| 144 | //if the jvm was not found, report not found
|
---|
| 145 | if ( !jvmFound ) {
|
---|
| 146 |
|
---|
| 147 | //did not find a good java
|
---|
| 148 | cout << "Greenstone requires java @java.min.version@ or greater." << endl;
|
---|
| 149 |
|
---|
[16207] | 150 | //tell them if java is absent or just too old
|
---|
| 151 | Jvm tmpJvm;
|
---|
[18283] | 152 | if ( find( tmpJvm, false, minimum, false, false, phint, hint, false ) ) {
|
---|
[15972] | 153 | cout << "You have java, but it is too old." << endl;
|
---|
| 154 | } else {
|
---|
| 155 | cout << "Could not find java on your system." << endl;
|
---|
[16207] | 156 | }
|
---|
[16208] | 157 |
|
---|
[15972] | 158 | cout << "Install java (@java.min.version@ or greater) and set JAVA_HOME or JRE_HOME, and try again" << endl;
|
---|
| 159 | #ifndef java_is_bundled
|
---|
| 160 | cout << "Or, download a greentsone3 installer with bundled java and use that instead of this one" << endl;
|
---|
| 161 | #endif
|
---|
| 162 |
|
---|
| 163 | //if we have found it, launch the installer
|
---|
| 164 | } else {
|
---|
| 165 |
|
---|
| 166 | cout << "Launching Installer ..." << endl;
|
---|
| 167 | int launch_exit_code = 0;
|
---|
[19720] | 168 | launch_exit_code = system( (foundJvm.getExecutable() + " -Xmx85M -jar @[email protected]" + (text_mode?" text":"") ).c_str() );
|
---|
[15972] | 169 |
|
---|
| 170 | //report how it went
|
---|
| 171 | if ( launch_exit_code == 0 ) {
|
---|
| 172 | cout << "Setup complete" << endl;
|
---|
| 173 | } else {
|
---|
| 174 | cout << "The installer exited with an error" << endl;
|
---|
| 175 | cout << "Greenstone may not be correctly installed" << endl;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | }
|
---|
[19720] | 179 | #else
|
---|
[19938] | 180 | bool java_ready = prep_java( wd );
|
---|
| 181 | if ( java_ready ) {
|
---|
| 182 | string cmd = "./jre/bin/java -Dorig.dir=\"" + wd + "\" -jar " + wd + "/Java/Jars/linux.jar" + (text_mode?" text":"");
|
---|
[19766] | 183 | system( cmd.c_str() );
|
---|
| 184 | }
|
---|
[19594] | 185 | #endif
|
---|
| 186 |
|
---|
[19027] | 187 | //change to the scratch dir for the following operation
|
---|
[19034] | 188 | chdir(scratch.c_str());
|
---|
[15972] | 189 |
|
---|
| 190 | //delete the temp files
|
---|
| 191 | cout << "Deleting the temp directory" << endl;
|
---|
| 192 | system( ("rm -rf " + tempdir).c_str() );
|
---|
| 193 |
|
---|
| 194 | return 0;
|
---|
| 195 |
|
---|
| 196 | }
|
---|
[19720] | 197 | #ifndef CDROM
|
---|
[15972] | 198 | bool extract_bundled_file( const char data[], int size, char* filename, bool make_executable) {
|
---|
| 199 |
|
---|
| 200 | if ( size == 0 ) return false;
|
---|
| 201 |
|
---|
| 202 | //delete the file if it exists
|
---|
| 203 | remove( filename );
|
---|
| 204 |
|
---|
| 205 | //open the file
|
---|
| 206 | fstream binary_file( filename, ios::out|ios::binary );
|
---|
| 207 | if ( !binary_file.good() ) {
|
---|
| 208 | binary_file.close();
|
---|
| 209 | return false;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | //write the file
|
---|
| 213 | binary_file.write( data, size );
|
---|
| 214 | if ( !binary_file.good() ) {
|
---|
| 215 | binary_file.close();
|
---|
| 216 | return false;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | //close the file
|
---|
| 220 | binary_file.close();
|
---|
| 221 | if ( !binary_file.good() ) {
|
---|
| 222 | binary_file.close();
|
---|
| 223 | return false;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | if ( make_executable ) {
|
---|
| 227 | string command = "chmod a+x " + string(filename);
|
---|
| 228 | system( command.c_str() );
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | return true;
|
---|
| 232 |
|
---|
| 233 | }
|
---|
[19765] | 234 | #else
|
---|
| 235 |
|
---|
[19938] | 236 | bool prep_java( string wd ) {
|
---|
[19765] | 237 |
|
---|
| 238 |
|
---|
[19938] | 239 | string jTestCmd = "./jre/bin/java -version >/dev/null 2>&1";
|
---|
| 240 |
|
---|
| 241 | //try to extract straight off cd
|
---|
| 242 | string j1cmd = wd + "/Java/Linux/jre_bin && tar -xf jre.tar && " + jTestCmd;
|
---|
[19765] | 243 | int j1status = system(j1cmd.c_str());
|
---|
[19938] | 244 | system("/bin/rm -f jre.tar");
|
---|
[19765] | 245 |
|
---|
| 246 | if (WEXITSTATUS(j1status)!=0) {
|
---|
[19938] | 247 | cerr << "Unable to extract java straight off CD-ROM." << endl;
|
---|
[19765] | 248 | cerr << "This is probably because your CD-ROM was mounted with 'noexec'." << endl;
|
---|
| 249 | cerr << "It can be changed by a system administrator" << endl << endl;
|
---|
| 250 | cerr << "Copying Java to local disk so it can be run" << endl;
|
---|
| 251 |
|
---|
| 252 | string cdtar_home = wd + "/Java/Linux/";
|
---|
[19938] | 253 | string tcmd = "(cd \"" + cdtar_home + "\" ; tar cf - jre_bin )";
|
---|
[19765] | 254 | tcmd += "| tar xvf - ";
|
---|
[19938] | 255 | //tcmd += "| awk 'BEGIN{C=0}{C++; printf(\".\"); if (C%70==0) printf(\"\\n\");}END{printf(\"\\n\")}'";
|
---|
[19765] | 256 |
|
---|
| 257 | int tstatus = system(tcmd.c_str());
|
---|
| 258 |
|
---|
| 259 | if (WEXITSTATUS(tstatus)!=0) {
|
---|
| 260 | cerr << "Failed to copy Java to disk" << endl;
|
---|
| 261 | cerr << "Please make sure you have write permissions in the directory where you run the installer" << endl;
|
---|
| 262 | }
|
---|
| 263 | else {
|
---|
| 264 |
|
---|
[19938] | 265 | string j2cmd = "./jre_bin && tar -xf jre.tar && " + jTestCmd;
|
---|
[19765] | 266 | int j2status = system(j2cmd.c_str());
|
---|
[19938] | 267 | system("/bin/rm -f jre.tar jre_bin");
|
---|
[19765] | 268 |
|
---|
[19938] | 269 | if (WEXITSTATUS(j2status)!=0) {
|
---|
| 270 | cerr << "Unable to run copied Java" << endl;
|
---|
| 271 | return false;
|
---|
| 272 | }
|
---|
[19765] | 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | // to get to here, full_java must be set (and working)
|
---|
| 277 |
|
---|
[19938] | 278 | return true;
|
---|
[19765] | 279 | }
|
---|
| 280 |
|
---|
[19594] | 281 | #endif
|
---|