setup/setup.c
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Aug 31 07:07:40 2020 +0100 (2020-08-31)
changeset 103 c4b0d5cc34bc
parent 61 31fb35727621
permissions -rw-r--r--
Add support for --default-action in app-manager
ali@0
     1
/*
ali@61
     2
 * Copyright (C) 2009, 2011, 2016  J. Ali Harlow <ali@juiblex.co.uk>
ali@0
     3
 *
ali@0
     4
 * This program is free software; you can redistribute it and/or modify
ali@0
     5
 * it under the terms of the GNU General Public License as published by
ali@0
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@0
     7
 * (at your option) any later version.
ali@0
     8
 *
ali@0
     9
 * This program is distributed in the hope that it will be useful,
ali@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@0
    12
 * GNU General Public License for more details.
ali@0
    13
 *
ali@0
    14
 * You should have received a copy of the GNU General Public License along
ali@0
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@0
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@0
    17
 */
ali@0
    18
ali@0
    19
#include <stdlib.h>
ali@0
    20
#include <stdio.h>
ali@0
    21
#include <string.h>
ali@0
    22
#include <limits.h>
ali@0
    23
#ifdef WIN32
ali@0
    24
#include <windows.h>
ali@0
    25
#include <shlobj.h>
ali@0
    26
#endif
ali@0
    27
#include <lua.h>
ali@0
    28
#include "config.h"
ali@0
    29
#include "plover/plover.h"
ali@0
    30
#include "whelk/whelk.h"
ali@0
    31
ali@0
    32
LUALIB_API int luaopen_posix(lua_State *L);
ali@0
    33
ali@3
    34
void setup(const char *argv0)
ali@0
    35
{
ali@99
    36
    char *yum_uri,*local_database;
ali@99
    37
    char *active_database=NULL,*alternate_database=NULL;
ali@61
    38
    gchar *s,*prefix,*distribution,*vendor_prefix;
ali@13
    39
    int ch,changed;
ali@0
    40
    struct comps *comps;
ali@0
    41
    struct comps_group *group;
ali@0
    42
    struct comps_requirement *pkg;
ali@24
    43
    struct plover_vector *packages=NULL;
ali@24
    44
    GError *error=NULL;
ali@61
    45
    s=plover_get_program(argv0);
ali@61
    46
    yum_uri=razor_path_to_uri(s);
ali@61
    47
    g_free(s);
ali@61
    48
    s=g_strconcat(yum_uri,"/repodata/comps.xml",NULL);
ali@61
    49
    comps=plover_comps_new_from_uri(s,&error);
ali@61
    50
    g_free(s);
ali@61
    51
    if (g_error_matches(error,PLOVER_RAZOR_ERROR,
ali@61
    52
      RAZOR_GENERAL_ERROR_UNSUPPORTED_ARCHIVE))
ali@61
    53
    {
ali@61
    54
	g_clear_error(&error);
ali@61
    55
	free(yum_uri);
ali@61
    56
	s=plover_get_program_directory(argv0);
ali@61
    57
	yum_uri=razor_path_to_uri(s);
ali@61
    58
	g_free(s);
ali@61
    59
	s=g_strconcat(yum_uri,"/repodata/comps.xml",NULL);
ali@61
    60
	comps=plover_comps_new_from_uri(s,&error);
ali@61
    61
    }
ali@0
    62
    if (!comps)
ali@0
    63
    {
ali@61
    64
	fprintf(stderr,"%s\n",error->message);
ali@61
    65
	g_error_free(error);
ali@0
    66
	exit(1);
ali@0
    67
    }
ali@61
    68
    prefix=plover_comps_get_default_prefix(comps);
ali@61
    69
    if (prefix)
ali@61
    70
    {
ali@61
    71
	s=g_strconcat(prefix,"/var/lib/razor",NULL);
ali@61
    72
	local_database=razor_path_to_uri(s);
ali@61
    73
	g_free(s);
ali@61
    74
    }
ali@61
    75
    else
ali@61
    76
	local_database=NULL;
ali@61
    77
    switch(comps->database)
ali@61
    78
    {
ali@61
    79
	case COMPS_DATABASE_DISTRIBUTION_LOCAL:
ali@61
    80
	    active_database=local_database;
ali@61
    81
	    break;
ali@61
    82
	case COMPS_DATABASE_GLOBAL:
ali@61
    83
	    alternate_database=local_database;
ali@61
    84
	    break;
ali@61
    85
    }
ali@61
    86
    if (prefix)
ali@61
    87
    {
ali@61
    88
	distribution=g_strdup(comps->distribution);
ali@61
    89
	plover_comps_set_distribution(comps,NULL);
ali@61
    90
	vendor_prefix=plover_comps_get_default_prefix(comps);
ali@61
    91
	plover_comps_set_distribution(comps,distribution);
ali@61
    92
	g_free(distribution);
ali@61
    93
	razor_set_database_uri(alternate_database);
ali@61
    94
	if (plover_installed_files_match_prefix(vendor_prefix)==1)
ali@61
    95
	{
ali@61
    96
	    printf("There is an existing installation under %s\n"
ali@61
    97
	      "which is not compatible with this distribution. In order\n"
ali@61
    98
	      "to continue, the existing installation must be uninstalled.\n"
ali@61
    99
	      "Do you want to remove all packages in the existing installion? ",
ali@61
   100
	      prefix);
ali@61
   101
	    ch=getchar();
ali@61
   102
	    if (ch!='y' && ch!='Y' && ch!=EOF && ch!='\n')
ali@61
   103
		exit(1);
ali@61
   104
	    while(ch!='\n' && ch!=EOF)
ali@61
   105
		ch=getchar();
ali@61
   106
	    if (plover_remove(NULL,&error))
ali@61
   107
	    {
ali@61
   108
		fprintf(stderr,"%s\n",error->message);
ali@61
   109
		g_error_free(error);
ali@61
   110
		exit(1);
ali@61
   111
	    }
ali@61
   112
	}
ali@61
   113
	g_free(vendor_prefix);
ali@61
   114
    }
ali@61
   115
    razor_set_database_uri(active_database);
ali@61
   116
    free(local_database);
ali@61
   117
    if (prefix && !plover_installed_files_match_prefix(prefix))
ali@13
   118
    {
ali@13
   119
	printf("The existing installation is not under %s\n"
ali@13
   120
	  "In order to continue, all the existing packages must be removed.\n"
ali@13
   121
	  "Do you want to remove all existing packages? ",prefix);
ali@13
   122
	ch=getchar();
ali@13
   123
	if (ch!='y' && ch!='Y' && ch!=EOF && ch!='\n')
ali@13
   124
	    exit(1);
ali@13
   125
	while(ch!='\n' && ch!=EOF)
ali@13
   126
	    ch=getchar();
ali@24
   127
	if (plover_remove(NULL,&error))
ali@24
   128
	{
ali@24
   129
	    fprintf(stderr,"%s\n",error->message);
ali@24
   130
	    g_error_free(error);
ali@24
   131
	    exit(1);
ali@24
   132
	}
ali@13
   133
    }
ali@0
   134
    group=plover_comps_lookup_group(comps,"base");
ali@0
   135
    if (!group)
ali@0
   136
    {
ali@0
   137
	fprintf(stderr,"No base group found in comps.xml\n");
ali@0
   138
	exit(1);
ali@0
   139
    }
ali@24
   140
    packages=plover_vector_new();
ali@0
   141
    do
ali@0
   142
    {
ali@0
   143
	changed=0;
ali@0
   144
	for(pkg=group->packages;pkg;pkg=pkg->next)
ali@0
   145
	{
ali@24
   146
	    if (plover_vector_contains(packages,pkg->name))
ali@0
   147
		continue;
ali@0
   148
	    if (pkg->type==COMPS_REQUIREMENT_DEFAULT ||
ali@0
   149
	      pkg->type==COMPS_REQUIREMENT_MANDATORY ||
ali@0
   150
	      pkg->type==COMPS_REQUIREMENT_CONDITIONAL &&
ali@24
   151
	      plover_vector_contains(packages,pkg->requires))
ali@0
   152
	    {
ali@0
   153
		changed++;
ali@24
   154
		plover_vector_append(packages,pkg->name);
ali@0
   155
	    }
ali@0
   156
	}
ali@0
   157
    } while(changed);
ali@3
   158
    plover_comps_free(comps);
ali@0
   159
    if (!packages->len)
ali@0
   160
    {
ali@0
   161
	fprintf(stderr,"No packages to install\n");
ali@0
   162
	exit(1);
ali@0
   163
    }
ali@61
   164
    if (!plover_install_uri(yum_uri,prefix,packages->strings,&error))
ali@24
   165
    {
ali@24
   166
	fprintf(stderr,"%s\n",error->message);
ali@24
   167
	g_error_free(error);
ali@24
   168
	exit(1);
ali@24
   169
    }
ali@24
   170
    plover_vector_free(packages);
ali@24
   171
    g_free(prefix);
ali@61
   172
    free(yum_uri);
ali@0
   173
}
ali@0
   174
ali@0
   175
int main(int argc,char **argv)
ali@0
   176
{
ali@24
   177
    GError *error=NULL;
ali@31
   178
    plover_exception_handler_init();
ali@38
   179
    razor_set_lua_loader("posix",(void (*)())luaopen_posix);
ali@38
   180
    razor_set_lua_loader("whelk",(void (*)())luaopen_whelk);
ali@0
   181
    if (argc>1 && !strcmp(argv[1],"-u"))
ali@24
   182
    {
ali@24
   183
	if (!plover_remove(NULL,&error))
ali@24
   184
	{
ali@24
   185
	    fprintf(stderr,"%s\n",error->message);
ali@24
   186
	    g_error_free(error);
ali@24
   187
	    exit(1);
ali@24
   188
	}
ali@24
   189
    }
ali@0
   190
    else
ali@3
   191
	setup(argv[0]);
ali@13
   192
    exit(0);
ali@0
   193
}