setup/setup.c
changeset 0 49fc8f60e4a5
child 3 868db5c1f2d7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/setup/setup.c	Thu Jul 09 08:23:50 2009 +0100
     1.3 @@ -0,0 +1,175 @@
     1.4 +/*
     1.5 + * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     1.6 + *
     1.7 + * This program is free software; you can redistribute it and/or modify
     1.8 + * it under the terms of the GNU General Public License as published by
     1.9 + * the Free Software Foundation; either version 2 of the License, or
    1.10 + * (at your option) any later version.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License along
    1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
    1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.20 + */
    1.21 +
    1.22 +#include <stdlib.h>
    1.23 +#include <stdio.h>
    1.24 +#include <string.h>
    1.25 +#include <limits.h>
    1.26 +#ifdef WIN32
    1.27 +#include <windows.h>
    1.28 +#include <shlobj.h>
    1.29 +#endif
    1.30 +#include <lua.h>
    1.31 +#include "config.h"
    1.32 +#include "plover/plover.h"
    1.33 +#include "whelk/whelk.h"
    1.34 +
    1.35 +LUALIB_API int luaopen_posix(lua_State *L);
    1.36 +
    1.37 +struct vector {
    1.38 +    int len,alloc;
    1.39 +    char **strings;
    1.40 +};
    1.41 +
    1.42 +struct vector *vector_new(void)
    1.43 +{
    1.44 +    struct vector *vector;
    1.45 +    vector=malloc(sizeof(*vector));
    1.46 +    vector->len=0;
    1.47 +    vector->alloc=16;
    1.48 +    vector->strings=calloc(vector->alloc,sizeof(char *));
    1.49 +    return vector;
    1.50 +}
    1.51 +
    1.52 +void vector_append(struct vector *vector,const char *str)
    1.53 +{
    1.54 +    if (++(vector->len)>=vector->alloc)
    1.55 +    {
    1.56 +	vector->alloc*=2;
    1.57 +	vector->strings=realloc(vector->strings,vector->alloc*sizeof(char *));
    1.58 +    }
    1.59 +    vector->strings[vector->len-1]=strdup(str);
    1.60 +    vector->strings[vector->len]=NULL;
    1.61 +}
    1.62 +
    1.63 +int vector_contains(struct vector *vector,const char *str)
    1.64 +{
    1.65 +    int i;
    1.66 +    for(i=0;i<vector->len;i++)
    1.67 +	if (!strcmp(vector->strings[i],str))
    1.68 +	    return 1;
    1.69 +    return 0;
    1.70 +}
    1.71 +
    1.72 +void vector_free(struct vector *vector)
    1.73 +{
    1.74 +    int i;
    1.75 +    for(i=0;i<vector->len;i++)
    1.76 +	free(vector->strings[i]);
    1.77 +    free(vector->strings);
    1.78 +    free(vector);
    1.79 +}
    1.80 +
    1.81 +static void *alloc_lua(void *user_data,void *ptr,size_t osize,size_t nsize)
    1.82 +{
    1.83 +    if (!nsize)
    1.84 +    {
    1.85 +	free(ptr);
    1.86 +	return NULL;
    1.87 +    }
    1.88 +    else
    1.89 +	return realloc(ptr,nsize);
    1.90 +}
    1.91 +
    1.92 +void setup()
    1.93 +{
    1.94 +    char path[PATH_MAX],*s,*t,*prefix;
    1.95 +    int changed;
    1.96 +    struct comps *comps;
    1.97 +    struct comps_group *group;
    1.98 +    struct comps_requirement *pkg;
    1.99 +    struct vector *packages=NULL;
   1.100 +#ifdef WIN32
   1.101 +    SHGetFolderPath(NULL,CSIDL_PROGRAM_FILES|CSIDL_FLAG_DONT_VERIFY,NULL,0,
   1.102 +      path);
   1.103 +    prefix=strdup(path);
   1.104 +    GetModuleFileName(NULL,path,sizeof(path));
   1.105 +    s=strrchr(path,'/');
   1.106 +    if (s)
   1.107 +    {
   1.108 +	t=strrchr(s,'\\');
   1.109 +	if (t)
   1.110 +	    s=t;
   1.111 +    }
   1.112 +    else
   1.113 +	s=strrchr(path,'\\');
   1.114 +    if (s)
   1.115 +	*s='\0';
   1.116 +#else
   1.117 +    strcpy(path,"/tmp");
   1.118 +    prefix=NULL;
   1.119 +#endif
   1.120 +    s=plover_strconcat(path,"/repodata/comps.xml",NULL);
   1.121 +    comps=plover_comps_new_from_file(s);
   1.122 +    if (!comps)
   1.123 +    {
   1.124 +	perror(s);
   1.125 +	exit(1);
   1.126 +    }
   1.127 +    free(s);
   1.128 +#ifdef WIN32
   1.129 +    s=plover_strconcat(prefix,"\\",comps->vendor?comps->vendor:"Plover",NULL);
   1.130 +    free(prefix);
   1.131 +    prefix=s;
   1.132 +#endif
   1.133 +    group=plover_comps_lookup_group(comps,"base");
   1.134 +    if (!group)
   1.135 +    {
   1.136 +	fprintf(stderr,"No base group found in comps.xml\n");
   1.137 +	exit(1);
   1.138 +    }
   1.139 +    packages=vector_new();
   1.140 +    do
   1.141 +    {
   1.142 +	changed=0;
   1.143 +	for(pkg=group->packages;pkg;pkg=pkg->next)
   1.144 +	{
   1.145 +	    if (vector_contains(packages,pkg->name))
   1.146 +		continue;
   1.147 +	    if (pkg->type==COMPS_REQUIREMENT_DEFAULT ||
   1.148 +	      pkg->type==COMPS_REQUIREMENT_MANDATORY ||
   1.149 +	      pkg->type==COMPS_REQUIREMENT_CONDITIONAL &&
   1.150 +	      vector_contains(packages,pkg->requires))
   1.151 +	    {
   1.152 +		changed++;
   1.153 +		vector_append(packages,pkg->name);
   1.154 +	    }
   1.155 +	}
   1.156 +    } while(changed);
   1.157 +    if (!packages->len)
   1.158 +    {
   1.159 +	fprintf(stderr,"No packages to install\n");
   1.160 +	exit(1);
   1.161 +    }
   1.162 +    plover_install(path,prefix,packages->strings);
   1.163 +    vector_free(packages);
   1.164 +    plover_comps_free(comps);
   1.165 +#ifdef WIN32
   1.166 +    free(prefix);
   1.167 +#endif
   1.168 +}
   1.169 +
   1.170 +int main(int argc,char **argv)
   1.171 +{
   1.172 +    razor_set_lua_loader("posix",luaopen_posix);
   1.173 +    razor_set_lua_loader("whelk",luaopen_whelk);
   1.174 +    if (argc>1 && !strcmp(argv[1],"-u"))
   1.175 +	plover_remove(NULL);
   1.176 +    else
   1.177 +	setup();
   1.178 +}