setup/setup.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Jul 16 11:07:18 2016 +0100 (2016-07-16)
changeset 61 31fb35727621
parent 38 a29623b68ca2
child 99 0121592e2512
permissions -rw-r--r--
Support parallel installations. The idea is that for CAD screener, we want
to be able to install this on the same machine as a standard AVOT setup
(most notably for John's laptop). To allow for the possibility of a second
application that might have the same requirements, we add the concept of
vendor-specific distributions. Thus we can have one distribution for CAD
screener and one for The Next Big Thing. It doesn't seem trivial to have
both CAD screener and AVOT under the same vendor tag so we'll have to have
AVOT under "City Occupational" and CAD screener under "City Occupational Ltd"
or some such kludge.

Most of this is done although we are very short of test cases (in particular
we don't test that it's actually possible to install CAD screener in parallel
with AVOT or to update either of them once installed, which is fundamental).

We also have a lot of baggage left over, including an intercept of razor_set.
The problem that this was introduced to debug has been fixed but it looks
like there are a number of memory leaks which it might be useful to help
track down so it has been left in place for now.

There is still a lot of confusion in plover between path-based and URI-based
API. We should review the API, decide what we want and have a general clear up.

There is also confusion as to the purpose of RAZOR_ROOT (and meaning; path or
URI). This is not used at all in librazor (although it is used in razor.exe).
Ideally we shouldn't use it in plover or plover-gtk either although again, we
might want to support it or an equivalent in (some of) the various executables.

Work that would still to nice to do for CAD screener:

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