setup/setup.c
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Jan 30 13:35:28 2012 +0000 (2012-01-30)
changeset 17 43df682785f5
parent 3 868db5c1f2d7
child 24 2b9f54d14cc2
permissions -rw-r--r--
Don't assume -lcrypt32 is available
     1 /*
     2  * Copyright (C) 2009, 2011  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 void setup(const char *argv0)
    79 {
    80     char *path,*s,*prefix;
    81     int ch,changed;
    82     struct comps *comps;
    83     struct comps_group *group;
    84     struct comps_requirement *pkg;
    85     struct vector *packages=NULL;
    86     path=plover_get_program_directory(argv0);
    87     s=plover_strconcat(path,"/repodata/comps.xml",NULL);
    88     comps=plover_comps_new_from_file(s);
    89     if (!comps)
    90     {
    91 	perror(s);
    92 	exit(1);
    93     }
    94     free(s);
    95     prefix=plover_default_prefix_for_vendor(comps->vendor);
    96     if (!plover_installed_files_match_prefix(prefix))
    97     {
    98 	printf("The existing installation is not under %s\n"
    99 	  "In order to continue, all the existing packages must be removed.\n"
   100 	  "Do you want to remove all existing packages? ",prefix);
   101 	ch=getchar();
   102 	if (ch!='y' && ch!='Y' && ch!=EOF && ch!='\n')
   103 	    exit(1);
   104 	while(ch!='\n' && ch!=EOF)
   105 	    ch=getchar();
   106 	plover_remove(NULL);
   107     }
   108     group=plover_comps_lookup_group(comps,"base");
   109     if (!group)
   110     {
   111 	fprintf(stderr,"No base group found in comps.xml\n");
   112 	exit(1);
   113     }
   114     packages=vector_new();
   115     do
   116     {
   117 	changed=0;
   118 	for(pkg=group->packages;pkg;pkg=pkg->next)
   119 	{
   120 	    if (vector_contains(packages,pkg->name))
   121 		continue;
   122 	    if (pkg->type==COMPS_REQUIREMENT_DEFAULT ||
   123 	      pkg->type==COMPS_REQUIREMENT_MANDATORY ||
   124 	      pkg->type==COMPS_REQUIREMENT_CONDITIONAL &&
   125 	      vector_contains(packages,pkg->requires))
   126 	    {
   127 		changed++;
   128 		vector_append(packages,pkg->name);
   129 	    }
   130 	}
   131     } while(changed);
   132     plover_comps_free(comps);
   133     if (!packages->len)
   134     {
   135 	fprintf(stderr,"No packages to install\n");
   136 	exit(1);
   137     }
   138     plover_install(path,prefix,packages->strings);
   139     vector_free(packages);
   140     free(prefix);
   141     free(path);
   142 }
   143 
   144 int main(int argc,char **argv)
   145 {
   146     razor_set_lua_loader("posix",luaopen_posix);
   147     razor_set_lua_loader("whelk",luaopen_whelk);
   148     if (argc>1 && !strcmp(argv[1],"-u"))
   149 	plover_remove(NULL);
   150     else
   151 	setup(argv[0]);
   152     exit(0);
   153 }