setup/setup.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jul 09 08:23:50 2009 +0100 (2009-07-09)
changeset 0 49fc8f60e4a5
child 3 868db5c1f2d7
permissions -rw-r--r--
Initial checkin
     1 /*
     2  * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include <stdlib.h>
    20 #include <stdio.h>
    21 #include <string.h>
    22 #include <limits.h>
    23 #ifdef WIN32
    24 #include <windows.h>
    25 #include <shlobj.h>
    26 #endif
    27 #include <lua.h>
    28 #include "config.h"
    29 #include "plover/plover.h"
    30 #include "whelk/whelk.h"
    31 
    32 LUALIB_API int luaopen_posix(lua_State *L);
    33 
    34 struct vector {
    35     int len,alloc;
    36     char **strings;
    37 };
    38 
    39 struct vector *vector_new(void)
    40 {
    41     struct vector *vector;
    42     vector=malloc(sizeof(*vector));
    43     vector->len=0;
    44     vector->alloc=16;
    45     vector->strings=calloc(vector->alloc,sizeof(char *));
    46     return vector;
    47 }
    48 
    49 void vector_append(struct vector *vector,const char *str)
    50 {
    51     if (++(vector->len)>=vector->alloc)
    52     {
    53 	vector->alloc*=2;
    54 	vector->strings=realloc(vector->strings,vector->alloc*sizeof(char *));
    55     }
    56     vector->strings[vector->len-1]=strdup(str);
    57     vector->strings[vector->len]=NULL;
    58 }
    59 
    60 int vector_contains(struct vector *vector,const char *str)
    61 {
    62     int i;
    63     for(i=0;i<vector->len;i++)
    64 	if (!strcmp(vector->strings[i],str))
    65 	    return 1;
    66     return 0;
    67 }
    68 
    69 void vector_free(struct vector *vector)
    70 {
    71     int i;
    72     for(i=0;i<vector->len;i++)
    73 	free(vector->strings[i]);
    74     free(vector->strings);
    75     free(vector);
    76 }
    77 
    78 static void *alloc_lua(void *user_data,void *ptr,size_t osize,size_t nsize)
    79 {
    80     if (!nsize)
    81     {
    82 	free(ptr);
    83 	return NULL;
    84     }
    85     else
    86 	return realloc(ptr,nsize);
    87 }
    88 
    89 void setup()
    90 {
    91     char path[PATH_MAX],*s,*t,*prefix;
    92     int changed;
    93     struct comps *comps;
    94     struct comps_group *group;
    95     struct comps_requirement *pkg;
    96     struct vector *packages=NULL;
    97 #ifdef WIN32
    98     SHGetFolderPath(NULL,CSIDL_PROGRAM_FILES|CSIDL_FLAG_DONT_VERIFY,NULL,0,
    99       path);
   100     prefix=strdup(path);
   101     GetModuleFileName(NULL,path,sizeof(path));
   102     s=strrchr(path,'/');
   103     if (s)
   104     {
   105 	t=strrchr(s,'\\');
   106 	if (t)
   107 	    s=t;
   108     }
   109     else
   110 	s=strrchr(path,'\\');
   111     if (s)
   112 	*s='\0';
   113 #else
   114     strcpy(path,"/tmp");
   115     prefix=NULL;
   116 #endif
   117     s=plover_strconcat(path,"/repodata/comps.xml",NULL);
   118     comps=plover_comps_new_from_file(s);
   119     if (!comps)
   120     {
   121 	perror(s);
   122 	exit(1);
   123     }
   124     free(s);
   125 #ifdef WIN32
   126     s=plover_strconcat(prefix,"\\",comps->vendor?comps->vendor:"Plover",NULL);
   127     free(prefix);
   128     prefix=s;
   129 #endif
   130     group=plover_comps_lookup_group(comps,"base");
   131     if (!group)
   132     {
   133 	fprintf(stderr,"No base group found in comps.xml\n");
   134 	exit(1);
   135     }
   136     packages=vector_new();
   137     do
   138     {
   139 	changed=0;
   140 	for(pkg=group->packages;pkg;pkg=pkg->next)
   141 	{
   142 	    if (vector_contains(packages,pkg->name))
   143 		continue;
   144 	    if (pkg->type==COMPS_REQUIREMENT_DEFAULT ||
   145 	      pkg->type==COMPS_REQUIREMENT_MANDATORY ||
   146 	      pkg->type==COMPS_REQUIREMENT_CONDITIONAL &&
   147 	      vector_contains(packages,pkg->requires))
   148 	    {
   149 		changed++;
   150 		vector_append(packages,pkg->name);
   151 	    }
   152 	}
   153     } while(changed);
   154     if (!packages->len)
   155     {
   156 	fprintf(stderr,"No packages to install\n");
   157 	exit(1);
   158     }
   159     plover_install(path,prefix,packages->strings);
   160     vector_free(packages);
   161     plover_comps_free(comps);
   162 #ifdef WIN32
   163     free(prefix);
   164 #endif
   165 }
   166 
   167 int main(int argc,char **argv)
   168 {
   169     razor_set_lua_loader("posix",luaopen_posix);
   170     razor_set_lua_loader("whelk",luaopen_whelk);
   171     if (argc>1 && !strcmp(argv[1],"-u"))
   172 	plover_remove(NULL);
   173     else
   174 	setup();
   175 }