/* ======== IGeoS ===== Distribition full ===== IGeoS ========= ** ** install.c : Created by 'AMSS-developer' on Fri Jan 12 19:40:00 2018 ** ** Product : IGeoS - Integrated Geoscience Software ** ** Description : System for seismic, well log, and potential-field data analysis ** ** =================== Limited License: =================================== ** ** This software is provided free under the following terms and conditions: ** ** 1. Permission to use, copy, and modify this software ** for non-commercial purposes without fee is hereby granted, provided ** that this copyright notice, the warranty disclaimer, and this ** permission notice appear in all copies. ** ** 2. Distribution of this software or any part of it "bundled" in with ** any product is considered to be a 'commercial purpose'. ** ** 3. Any new or adapted code developed to operate as a part of this ** software shall be contributed to the authors and distributed under ** the same license. ** ** ================== Warranty Disclaimer: ================================ ** ** This software is provided "as is", with no support and without ** obligation on the part of the author to assist in its use, correction, ** modification, or enhancement. No guarantees or warranties, ** either express or implied, and regarding the accuracy, safety, or ** fitness for any particular purpose are provided by any contributor ** to this software package. ** ** ======== IGeoS ===== Distribition full ===== IGeoS ========= */ /********* The following definitions may be auto-generated: *********/ #define CODE_SERVER "http://seisweb.usask.ca" /* ** ** Installs IGeoS (SIA) by retrieving files from an HTTP server and running tar, gcc, and make */ #include #include #include #include #include #include #include #include #include /* #define DEBUG */ #define BUF_LENGTH 2048 #define UNIX_SHELL "/bin/sh" #define FULL_DISTTRIBUTION "full" #define SERVER_UPDATE_DIR "SIA/admin/update/" /* location of update tar.gz files */ #define SERVER_DEMO_DIR "SIA/admin/" /* location of demos.tar.gz file */ #define MAX_DISTRO_LEN 10000 /* maximum number of modules in distribution */ char *program = NULL, distribution[80] = FULL_DISTTRIBUTION, /* distribution package name */ *SIAHOME = NULL, /* environment */ *SIABIN = NULL, SIALIB[1024], unpack_dir[1024] = "./", /* ditrectory for unpacking tar files */ response[102400], respcpy[1024], gcc_option[1024]="", /* gcc options */ upd_option[1024]="", /* updater options */ buf1[512], buf2[512]; int get_cpr = 1, print_log = 1, need_updater = 1, interactive = 0, build_now = 1; /* flag to build the item after downloading and unpacking */ char server[400] = CODE_SERVER; char query( const char *message ) { char c='\0'; printf( "\n%s -> ",message); scanf ( "%c", &c ); return c; } void get_distro_name() { char str[512]; FILE *f; sprintf(str,"%s/configure/distribution", SIAHOME); if ( (f = fopen(str,"r")) ) { if ( fgets(str,512,f) ) { const char *delims = " \t\n="; char *tok = strtok(str,delims); #ifdef DEBUG printf("*** get_distro_name: distr '%s'\n",tok); #endif if ( tok ) strcpy(distribution,tok); } fclose(f); } } void get_options() { char str[512]; FILE *f; sprintf(str,"%s/configure/distribution.%s", SIAHOME,distribution); if ( (f = fopen(str,"r")) ) { while ( fgets(str,512,f) ) { const char *delims = " \t\n="; char *tok = strtok(str,delims); #ifdef DEBUG printf("*** get_options: opt '%s'\n",tok); #endif if ( tok ) { if ( !strcmp(tok,"$serv" ) ) { if ( (tok = strtok(NULL,delims)) ) strcpy(server,tok); } else if ( !strcmp(tok,"$copt" ) ) { while ( (tok = strtok(NULL,delims)) ) strcat(strcat(gcc_option," "),tok); } else if ( *tok == '.' ) // options ended break; tok = strtok(NULL,delims); } } fclose(f); } } int usage_info() { printf("\nIGeoS (formerly SIA) web installer\n"); printf("----------------------------------\n"); printf("Usage:\n"); printf("\t1) To prepare for installation (create file $SIALIB/SETUP/sia_setup.h):\n\n\t\t%s URL [--distribution name] " "[ --interactive --prepare ]\n",program); printf("\n\t2) To install programs or demos:\n\n\t\t%s URL [--distribution name] [-Doptions -n -q] " "[ --interactive --prepare --redo --update ] [components]\n\n",program); printf("\t Add option '--agree' if you accept the license agreement and wish to " "bypass its display.\n\n"); printf("\t [components] can include module names, .gui for GUI, .xviewer for display server,\n"); printf("\t .modules for all modules, .demos for demos, or .all for everything except the demos.\n\n"); printf("\t -Doptions give optional environment variables passed to the compiler and overriding the\n"); printf("\t automatic software configuration. Examples: -DUSE_PVM -DUSE_QT -DUSE_POSTGRES\n\n"); printf("\t Note that .demos can only be obtained separately from the code. \n"); printf("\t Also, .demos can be obtained by any user (not necessarily code administrator).\n\n"); printf("\t Above, URL equal '-' corresponds to the default distribution server (%s).\n\n", server ); printf("\t Option --redo reinstalls by using current settings (equivalent to '%s --distribution %s --agree')\n\n", server, distribution ); printf("\n\t3) To update an installed distribution:\n\n\t\t%s --update\n",program); printf("\n\t\nShort options:\n."); printf("\t\t-n: disables obtaining the updater;\n"); printf("\t\t-q: disables informational messages.\n"); printf("\nPrerequisites:\n"); printf("\t1) Create environment variables SIAHOME (directory for the source codes) and SIABIN (for binaries);\n"); printf("\t2) Administrator: make sure you have the priviliges to create and write into $SIAHOME and $SIABIN.\n\n"); printf("NOTE: Installation by 'root' is not needed and not recommended.\n\n", server ); return 1; } int sh_call ( const char *cmd_string ) { #ifdef DEBUG printf("*** sh_call: '%s'\n",cmd_string); #endif pid_t pid; int status; if ( !cmd_string ) return 1; if ( ( pid = fork() ) < 0 ) return -1; if ( pid == 0 ) // child { execl(UNIX_SHELL,UNIX_SHELL,"-c",cmd_string,(char*)0); exit(0); } /*-- parent here -------------------*/ wait(&status); if ( WIFEXITED(status) ) return 0; if ( WIFSIGNALED(status) ) printf("command '%s' terminated by signal %d\n", cmd_string,WTERMSIG(status)); if ( WIFSTOPPED(status) ) printf("command '%s' stopped by signal %d\n", cmd_string,WSTOPSIG(status)); return status; } int full_distro() { return strcmp(distribution,FULL_DISTTRIBUTION)==0; } int request ( char *url ) { char str[512]; FILE *f; #ifdef DEBUG printf("*** request: %s\n", url); #endif *response = '\0'; sprintf(str,"wget -q -O - %s", url); #ifdef DEBUG printf("*** command: %s\n", str); #endif f = popen(str, "r" ); if ( f ) { while ( fgets(str,512,f) ) strcat(response,str); pclose(f); } else { printf("Could not execute '%s'\n",str); exit(1); } return strlen(response); } /* request and obtain file from server. return 1 if OK, ) if failed */ int getfile( int silent, const char *dir, const char *file ) { char str[1024]; /* delete the existing file */ if ( access(file,F_OK)==0 ) { sprintf(str,"rm -f %s",file); sh_call(str); } sprintf(str,"wget --read-timeout=15 %s %s/%s/%s",silent ? "-q" : "", server,dir,file); if ( print_log ) printf("*** Retrieving file: %s/%s/%s\n", server,dir,file); #ifdef DEBUG printf("*** getfile: Command: %s\n", str); #endif if ( sh_call(str) ) { printf("*** Error executing '%s'\n",str); return 0; } return access(file,F_OK)==0; } int getphpfile( const char *mes ) { char str[1024]; sprintf(str,"%s/SIA/cs.php?%s",server,mes); request(str); } int check_dir( const char *dir ) { return access(dir,R_OK|W_OK|X_OK) == 0; } int check_subdir( const char *dir, const char *sub ) { char d[200]; strcat(strcpy(d,dir),sub); return access(d,R_OK|X_OK) == 0; } char DIRSTR[10000]; char *dirselect2( const char *dirname, const char *start, const char *end, const char *exclude ) { DIR *dp; DIRSTR[0] = '\0'; dp = opendir(dirname); if (dp != NULL) { struct dirent *ep; while (ep = readdir(dp)) if ( ep->d_name ) { if ( ep->d_name[0] == '.' ) continue; if ( start ) if ( strncmp(ep->d_name,start,strlen(start)) ) /* skip */ continue; if ( end ) { int le = strlen(end), l = strlen(ep->d_name); if ( l < le ) continue; if ( strncmp(ep->d_name + l-le,end,strlen(end)) ) /* skip */ continue; } if ( exclude ) { int le = strlen(exclude), l = strlen(ep->d_name); if ( l >= le ) if ( !strncmp(ep->d_name + l-le,exclude,strlen(exclude)) ) /* skip matching */ continue; } strcat(strcat(DIRSTR,ep->d_name)," "); /* output */ } closedir (dp); } return DIRSTR; } /* remove files ending in 'select' in 'dirname' */ void rmfiles( const char *dir, const char *subdir, const char *options, const char *select_start, const char *select_end, const char *exclude ) { char d[40000],wd[400],td[400]; getcwd(wd,400); sprintf(td,"%s/%s",dir,subdir); chdir(td); { const char *s = dirselect2("./",select_start,select_end,exclude); /* printf("td '%s' sel '%s' %s' dirs '%s'\n",td,select_start,select_end, s); */ if ( strlen(s) ) { sprintf(d,"rm %s %s",options,s); sh_call(d); } } chdir(wd); } void test_system() { /*--- word length test done in sia_config --------*/ /*--- endian test done in sia_config --------*/ if ( access(UNIX_SHELL,X_OK) != 0 ) { printf("*** Shell '%s' is not available on this system. Installation aborted.\n", UNIX_SHELL); exit(1); } } /*------- obtain and install all requested items ----------*/ int install( const char *subdir, int argc, char ** argv ) { char str[3072]; int arg; int count=0; if ( print_log ) printf("*** Installing IGeoS source codes in directory %s\n",subdir); for ( arg=2; arg 0.0 ) { printf("*** Installing new '%s' version %1.2f from '%s' ...\n", mname[i],mver[i],server); sprintf(str, "%s/sia-install %s -q -n --redo %s", SIABIN, server, mname[i] ); sh_call(str); changed=1; } } /* if changed anything, relist the local distro */ if ( changed ) { sprintf(str, "%s/sia-update --prelist --distribution %s", SIABIN, distribution ); sh_call(str); } } } int main (int argc, char ** argv) { char str[102400]; int arg, demos=0, get_updater = 0, get_server_name = 1; program = argv[0]; SIAHOME = getenv("SIAHOME"); SIABIN = getenv("SIABIN"); /*-- test the system ---*/ test_system(); if ( argc<2 ) return usage_info(); if ( !SIAHOME ) { printf("*** SIAHOME environment variable is not set ****\n"); return 1; } if ( !SIABIN ) { printf("*** SIABIN environment variable is not set ****\n"); return 1; } { const char *c = getenv("SIALIB"); if ( c ) strcpy(SIALIB,c); else sprintf(SIALIB,"%s/lib",SIABIN); } /*-- first pass through parameters - pick out the general settings ---*/ for ( arg=1; arg "); if ( fgets(buf1,512,stdin) ) SIAHOME = strtok(buf1," \t\n"); printf("Object and executable root -> "); if ( fgets(buf2,512,stdin) ) { SIABIN = strtok(buf2," \t\n"); sprintf(SIALIB,"%s/lib",SIABIN); } } #ifdef DEBUG printf("********* USER '%s'\n",getenv("USER")); #endif /*------- second pass - check if the updater needs to be obtained ----------*/ for ( arg=2; arg"); scanf("%s",str); if ( strcmp(str,"yes") ) { if ( tolower(*str) != 'n' ) { printf("You have responded '%s'.\n", str); goto Ask; } exit(0); // responded 'no' } printf("\n\nInstalling SIA distribution '%s'...\n",distribution); } else exit(1); } /* get updater */ #ifdef DEBUG printf("********* GET UPDATER %d\n",get_updater); #endif if ( get_updater && need_updater ) { int err=0; if ( getfile(1,"SIA/install","update.c") ) { sprintf(str, "mv update.c %s/source/\n",SIAHOME); if ( sh_call(str) ) err=1; } if ( getfile(1,"SIA/install","configure.h") ) { sprintf(str, "mv configure.h %s/include/\n",SIAHOME); if ( sh_call(str) ) err=1; } if ( err ) { printf("*** Error obtaining updater files. Installation aborted.\n"); exit(1); } { char options[2048] = ""; /*------- perform initial cleanup in case a previous version is installed ----------*/ /* rmfiles(SIABIN,"","-f","sia-config",NULL,NULL); rmfiles(SIABIN,"","-f","sia-update",NULL,NULL); rmfiles(SIABIN,"","-f","pdf",NULL,NULL); rmfiles(SIABIN,"","-f","cwr",NULL,NULL); rmfiles(SIABIN,"","-f","sia_",NULL,NULL); printf("*** Old SIA utilities and executables removed.\n"); */ if ( *gcc_option ) strcat(options,gcc_option); sprintf(str, "cd %s/source; gcc -DINPACKAGE %s update.c -I%s/SETUP -o %s/sia-update", SIAHOME, options, SIALIB, SIABIN); if ( sh_call(str) ) { printf("*** Error buiding the updater. Installation aborted.\n"); exit(1); } printf("*** Updater obtained and built.\n"); } } sprintf(str, "%s/sia-update", SIABIN); if ( access(str,X_OK) ) /* no updater */ { printf("*** Missing updater program: %s\n",str); return 1; } else /* perform installation */ install(SERVER_UPDATE_DIR,argc,argv); return 0; }