1 /* @source compembassy application
2 **
3 ** Test whether EMBASSY bundles compile and install
4 **
5 ** @author Alan Bleasby
6 ** @@
7 **
8 ** This program is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License
10 ** as published by the Free Software Foundation; either version 2
11 ** of the License, or (at your option) any later version.
12 **
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 **
22 **
23 ** This is a UNIX application
24 **
25 ** Compile using: cc -o compembassy -O2 compembassy.c
26 **
27 ** Usage: compembassy bundledir prefixdir
28 **
29 ** This program expects you to have compiled and installed EMBOSS
30 ** into a given prefix. It accepts a directory containing just
31 ** the bundled EMBASSY 'gz' files. It extracts each in turn
32 ** and configures, makes and installs to the given prefix.
33 ******************************************************************************/
34 
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <dirent.h>
40 #include <stdlib.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 
44 
45 static int find_gz(const char *dirname, char ***aptr);
46 static int find_dirname(const char *dirname, char ***aptr);
47 
48 
49 
50 
find_gz(const char * dirname,char *** aptr)51 static int find_gz(const char *dirname, char ***aptr)
52 {
53     char **earray = NULL;
54     DIR *dirp = NULL;
55     struct dirent *dresult = NULL;
56     char fname[MAXPATHLEN];
57     char buf[sizeof(struct dirent) + MAXPATHLEN];
58     struct stat sbuf;
59     int count = 0;
60     int i;
61     int len;
62 
63     dirp = opendir(dirname);
64 
65     if(!dirp)
66     {
67         fprintf(stderr,"Error: Cannot open directory [%s]\n",dirname);
68         exit(-1);
69     }
70 
71     count = 0;
72 
73     while(!readdir_r(dirp,(struct dirent *)buf, &dresult))
74     {
75         if(!dresult)
76             break;
77 
78         if(dresult->d_type == DT_REG)
79         {
80             len = strlen(dresult->d_name);
81 
82             if(strcmp(&dresult->d_name[len-3],".gz"))
83                 continue;
84 
85             ++count;
86         }
87     }
88 
89     rewinddir(dirp);
90 
91 
92     *aptr  = malloc((sizeof(char *) * (count + 1)));
93     earray = *aptr;
94 
95     for(i = 0; i < count; ++i)
96         earray[i] = malloc(sizeof(char) * MAXPATHLEN);
97 
98     earray[count] = NULL;
99 
100     i = 0;
101 
102     while(!readdir_r(dirp,(struct dirent *)buf, &dresult))
103     {
104         if(!dresult)
105             break;
106 
107         if(dresult->d_type == DT_REG)
108         {
109             len = strlen(dresult->d_name);
110             if(strcmp(&dresult->d_name[len-3],".gz"))
111                 continue;
112 
113             strcpy(earray[i++],dresult->d_name);
114         }
115     }
116 
117 
118     closedir(dirp);
119 
120     return count;
121 }
122 
123 
124 
125 
find_dirname(const char * dirname,char *** aptr)126 static int find_dirname(const char *dirname, char ***aptr)
127 {
128     char **earray = NULL;
129     DIR *dirp = NULL;
130     struct dirent *dresult = NULL;
131     char fname[MAXPATHLEN];
132     char buf[sizeof(struct dirent) + MAXPATHLEN];
133     struct stat sbuf;
134     int count = 0;
135     int i;
136 
137     dirp = opendir(dirname);
138 
139     if(!dirp)
140     {
141         fprintf(stderr,"Error: Cannot open directory [%s]\n",dirname);
142         exit(-1);
143     }
144 
145     count = 0;
146 
147     while(!readdir_r(dirp,(struct dirent *)buf, &dresult))
148     {
149         if(!dresult)
150             break;
151 
152         if(dresult->d_type == DT_DIR)
153         {
154             if(*dresult->d_name == '.')
155                 continue;
156 
157             sprintf(fname,"%s/%s",dirname,dresult->d_name);
158 
159             if(!stat(fname,&sbuf))
160             {
161                 if(sbuf.st_mode & S_IXOTH)
162                     ++count;
163             }
164         }
165     }
166 
167     rewinddir(dirp);
168 
169 
170     *aptr  = malloc((sizeof(char *) * (count + 1)));
171     earray = *aptr;
172 
173     for(i = 0; i < count; ++i)
174         earray[i] = malloc(sizeof(char) * MAXPATHLEN);
175 
176     earray[count] = NULL;
177 
178     i = 0;
179 
180     while(!readdir_r(dirp,(struct dirent *)buf, &dresult))
181     {
182         if(!dresult)
183             break;
184 
185         if(dresult->d_type == DT_DIR)
186         {
187             if(*dresult->d_name == '.')
188                 continue;
189 
190             sprintf(fname,"%s/%s",dirname,dresult->d_name);
191 
192             if(!stat(fname,&sbuf))
193             {
194                 if(sbuf.st_mode & S_IXOTH)
195                     strcpy(earray[i++],dresult->d_name);
196             }
197         }
198     }
199 
200 
201     closedir(dirp);
202 
203     return count;
204 }
205 
206 
207 
main(int argc,char ** argv)208 int main(int argc, char **argv)
209 {
210     char **gzfiles = NULL;
211     char bundledir[MAXPATHLEN];
212     char tarfile[MAXPATHLEN];
213     char command[MAXPATHLEN];
214     char **dnames;
215 
216     int nemb = 0;
217     int ndirs = 0;
218 
219 
220     int i = 0;
221     int j = 0;
222 
223     int len = 0;
224 
225     if(argc < 3)
226     {
227         fprintf(stderr,"Usage: compembassy bundledir prefixdir\n");
228         exit(-1);
229     }
230 
231     sprintf(bundledir,"%s",argv[1]);
232     nemb = find_gz(bundledir,&gzfiles);
233 
234     for(i = 0; i < nemb; ++i)
235     {
236         strcpy(tarfile,gzfiles[i]);
237         len = strlen(tarfile);
238         tarfile[len-3] = '\0';
239 
240         sprintf(command,"cd %s; gunzip %s; tar xf %s",bundledir,gzfiles[i],
241                 tarfile);
242 
243         if(system(command))
244         {
245             fprintf(stderr,"Can't execute %s\n",command);
246             exit(-1);
247         }
248 
249         ndirs = find_dirname(bundledir,&dnames);
250         if(ndirs != 1)
251         {
252             fprintf(stderr,"Fatal: Too many directories in %s\n",bundledir);
253             exit(-1);
254         }
255 
256 
257         sprintf(command,"cd %s/%s; ./configure --prefix=%s; "
258                 "make; make install",bundledir,dnames[0],argv[2]);
259 
260         if(system(command))
261         {
262             fprintf(stderr,"Can't execute %s\n",command);
263             exit(-1);
264         }
265 
266         sprintf(command,"cd %s; rm -rf %s; gzip %s",
267                 bundledir,dnames[0],tarfile);
268 
269         if(system(command))
270         {
271             fprintf(stderr,"Can't execute %s\n",command);
272             exit(-1);
273         }
274 
275 
276         if(ndirs)
277         {
278             j = 0;
279 
280             while(dnames[j])
281                 free(dnames[j++]);
282 
283             free(dnames);
284         }
285     }
286 
287 
288     if(nemb)
289     {
290         i = 0;
291 
292         while(gzfiles[i])
293             free(gzfiles[i++]);
294 
295         free(gzfiles);
296     }
297 
298     return 0;
299 }
300