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