setup/setup.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jul 16 19:54:45 2020 +0100 (2020-07-16)
changeset 99 0121592e2512
parent 61 31fb35727621
permissions -rw-r--r--
Fix most compiler warnings
     1 /*
     2  * Copyright (C) 2009, 2011, 2016  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 void setup(const char *argv0)
    35 {
    36     char *yum_uri,*local_database;
    37     char *active_database=NULL,*alternate_database=NULL;
    38     gchar *s,*prefix,*distribution,*vendor_prefix;
    39     int ch,changed;
    40     struct comps *comps;
    41     struct comps_group *group;
    42     struct comps_requirement *pkg;
    43     struct plover_vector *packages=NULL;
    44     GError *error=NULL;
    45     s=plover_get_program(argv0);
    46     yum_uri=razor_path_to_uri(s);
    47     g_free(s);
    48     s=g_strconcat(yum_uri,"/repodata/comps.xml",NULL);
    49     comps=plover_comps_new_from_uri(s,&error);
    50     g_free(s);
    51     if (g_error_matches(error,PLOVER_RAZOR_ERROR,
    52       RAZOR_GENERAL_ERROR_UNSUPPORTED_ARCHIVE))
    53     {
    54 	g_clear_error(&error);
    55 	free(yum_uri);
    56 	s=plover_get_program_directory(argv0);
    57 	yum_uri=razor_path_to_uri(s);
    58 	g_free(s);
    59 	s=g_strconcat(yum_uri,"/repodata/comps.xml",NULL);
    60 	comps=plover_comps_new_from_uri(s,&error);
    61     }
    62     if (!comps)
    63     {
    64 	fprintf(stderr,"%s\n",error->message);
    65 	g_error_free(error);
    66 	exit(1);
    67     }
    68     prefix=plover_comps_get_default_prefix(comps);
    69     if (prefix)
    70     {
    71 	s=g_strconcat(prefix,"/var/lib/razor",NULL);
    72 	local_database=razor_path_to_uri(s);
    73 	g_free(s);
    74     }
    75     else
    76 	local_database=NULL;
    77     switch(comps->database)
    78     {
    79 	case COMPS_DATABASE_DISTRIBUTION_LOCAL:
    80 	    active_database=local_database;
    81 	    break;
    82 	case COMPS_DATABASE_GLOBAL:
    83 	    alternate_database=local_database;
    84 	    break;
    85     }
    86     if (prefix)
    87     {
    88 	distribution=g_strdup(comps->distribution);
    89 	plover_comps_set_distribution(comps,NULL);
    90 	vendor_prefix=plover_comps_get_default_prefix(comps);
    91 	plover_comps_set_distribution(comps,distribution);
    92 	g_free(distribution);
    93 	razor_set_database_uri(alternate_database);
    94 	if (plover_installed_files_match_prefix(vendor_prefix)==1)
    95 	{
    96 	    printf("There is an existing installation under %s\n"
    97 	      "which is not compatible with this distribution. In order\n"
    98 	      "to continue, the existing installation must be uninstalled.\n"
    99 	      "Do you want to remove all packages in the existing installion? ",
   100 	      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 	    if (plover_remove(NULL,&error))
   107 	    {
   108 		fprintf(stderr,"%s\n",error->message);
   109 		g_error_free(error);
   110 		exit(1);
   111 	    }
   112 	}
   113 	g_free(vendor_prefix);
   114     }
   115     razor_set_database_uri(active_database);
   116     free(local_database);
   117     if (prefix && !plover_installed_files_match_prefix(prefix))
   118     {
   119 	printf("The existing installation is not under %s\n"
   120 	  "In order to continue, all the existing packages must be removed.\n"
   121 	  "Do you want to remove all existing packages? ",prefix);
   122 	ch=getchar();
   123 	if (ch!='y' && ch!='Y' && ch!=EOF && ch!='\n')
   124 	    exit(1);
   125 	while(ch!='\n' && ch!=EOF)
   126 	    ch=getchar();
   127 	if (plover_remove(NULL,&error))
   128 	{
   129 	    fprintf(stderr,"%s\n",error->message);
   130 	    g_error_free(error);
   131 	    exit(1);
   132 	}
   133     }
   134     group=plover_comps_lookup_group(comps,"base");
   135     if (!group)
   136     {
   137 	fprintf(stderr,"No base group found in comps.xml\n");
   138 	exit(1);
   139     }
   140     packages=plover_vector_new();
   141     do
   142     {
   143 	changed=0;
   144 	for(pkg=group->packages;pkg;pkg=pkg->next)
   145 	{
   146 	    if (plover_vector_contains(packages,pkg->name))
   147 		continue;
   148 	    if (pkg->type==COMPS_REQUIREMENT_DEFAULT ||
   149 	      pkg->type==COMPS_REQUIREMENT_MANDATORY ||
   150 	      pkg->type==COMPS_REQUIREMENT_CONDITIONAL &&
   151 	      plover_vector_contains(packages,pkg->requires))
   152 	    {
   153 		changed++;
   154 		plover_vector_append(packages,pkg->name);
   155 	    }
   156 	}
   157     } while(changed);
   158     plover_comps_free(comps);
   159     if (!packages->len)
   160     {
   161 	fprintf(stderr,"No packages to install\n");
   162 	exit(1);
   163     }
   164     if (!plover_install_uri(yum_uri,prefix,packages->strings,&error))
   165     {
   166 	fprintf(stderr,"%s\n",error->message);
   167 	g_error_free(error);
   168 	exit(1);
   169     }
   170     plover_vector_free(packages);
   171     g_free(prefix);
   172     free(yum_uri);
   173 }
   174 
   175 int main(int argc,char **argv)
   176 {
   177     GError *error=NULL;
   178     plover_exception_handler_init();
   179     razor_set_lua_loader("posix",(void (*)())luaopen_posix);
   180     razor_set_lua_loader("whelk",(void (*)())luaopen_whelk);
   181     if (argc>1 && !strcmp(argv[1],"-u"))
   182     {
   183 	if (!plover_remove(NULL,&error))
   184 	{
   185 	    fprintf(stderr,"%s\n",error->message);
   186 	    g_error_free(error);
   187 	    exit(1);
   188 	}
   189     }
   190     else
   191 	setup(argv[0]);
   192     exit(0);
   193 }