pre-inst/pre-inst.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Jul 14 13:17:21 2020 +0100 (2020-07-14)
changeset 95 212150407fcc
parent 94 0e1a57225c62
child 96 d2d88f14283e
permissions -rw-r--r--
Add support to pre-inst for %REPOSITORY%
ali@24
     1
/*
ali@24
     2
 * Copyright (C) 2014  J. Ali Harlow <ali@juiblex.co.uk>
ali@24
     3
 *
ali@24
     4
 * This program is free software; you can redistribute it and/or modify
ali@24
     5
 * it under the terms of the GNU General Public License as published by
ali@24
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@24
     7
 * (at your option) any later version.
ali@24
     8
 *
ali@24
     9
 * This program is distributed in the hope that it will be useful,
ali@24
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@24
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@24
    12
 * GNU General Public License for more details.
ali@24
    13
 *
ali@24
    14
 * You should have received a copy of the GNU General Public License along
ali@24
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@24
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@24
    17
 *
ali@24
    18
 * References:
ali@24
    19
 *	http://www.transmissionzero.co.uk/computing/win32-apps-with-mingw/
ali@24
    20
 */
ali@24
    21
ali@24
    22
#include "config.h"
ali@24
    23
#ifndef WIN32
ali@24
    24
#define _XOPEN_SOURCE 500
ali@24
    25
#endif
ali@24
    26
#include <stdlib.h>
ali@24
    27
#include <stdio.h>
ali@24
    28
#include <string.h>
ali@24
    29
#include <lua.h>
ali@24
    30
#include <razor.h>
ali@24
    31
#include <plover/plover.h>
ali@24
    32
#include <whelk/whelk.h>
ali@24
    33
#ifdef WIN32
ali@24
    34
#include <windows.h>
ali@24
    35
#include <process.h>
ali@24
    36
#include <commctrl.h>
ali@24
    37
#include "resource.h"
ali@24
    38
ali@24
    39
#ifndef FOF_NO_UI
ali@24
    40
#define FOF_NO_UI	(FOF_SILENT|FOF_NOCONFIRMATION|FOF_NOERRORUI|\
ali@24
    41
			FOF_NOCONFIRMMKDIR)
ali@24
    42
#endif
ali@24
    43
ali@24
    44
#else	/* WIN32 */
ali@24
    45
#include <ftw.h>
ali@24
    46
#endif	/* WIN32 */
ali@24
    47
ali@24
    48
#ifdef WIN32
ali@24
    49
/* Under WIN32, g_spawn requires a helper program which we'd rather avoid */
ali@24
    50
#undef USE_G_SPAWN
ali@24
    51
#else
ali@24
    52
#define USE_G_SPAWN
ali@24
    53
#endif
ali@24
    54
ali@24
    55
LUALIB_API int luaopen_posix(lua_State *L);
ali@24
    56
ali@24
    57
#ifdef WIN32
ali@24
    58
DWORD main_thread_id;
ali@24
    59
#endif
ali@24
    60
ali@24
    61
gchar *prefix;
ali@24
    62
ali@24
    63
int verify_and_fix(const char *root)
ali@24
    64
{
ali@24
    65
    return 0;
ali@24
    66
}
ali@24
    67
ali@24
    68
#ifdef WIN32
ali@24
    69
INT_PTR CALLBACK ProgressDialogProc(HWND dialog,UINT msg,WPARAM w_param,
ali@24
    70
  LPARAM l_param)
ali@24
    71
{
ali@24
    72
    HWND progress;
ali@24
    73
    DWORD style;
ali@24
    74
    switch (msg)
ali@24
    75
    {
ali@24
    76
	case WM_INITDIALOG:
ali@24
    77
	    progress=GetDlgItem(dialog,IDC_PROGRESS); 
ali@24
    78
	    style=GetWindowLong(progress,GWL_STYLE);
ali@24
    79
	    SetWindowLong(progress,GWL_STYLE,style|PBS_MARQUEE);
ali@24
    80
	    SendMessage(progress,PBM_SETMARQUEE,(WPARAM)TRUE,(LPARAM)30);
ali@24
    81
	    return (INT_PTR)TRUE;
ali@24
    82
    }
ali@24
    83
    return (INT_PTR)FALSE;
ali@24
    84
}
ali@24
    85
#endif
ali@24
    86
ali@24
    87
#ifdef WIN32
ali@24
    88
__stdcall
ali@24
    89
#endif
ali@24
    90
unsigned pre_install_thread(void *data)
ali@24
    91
{
ali@24
    92
    int retval;
ali@95
    93
    const char *repository=data;
ali@46
    94
    gchar *s,*uri;
ali@24
    95
    char *install[]={"plover-gtkui",NULL};
ali@24
    96
    GError *error=NULL;
ali@94
    97
    plover__uri_handler_init();
ali@24
    98
    prefix=plover_pre_install_prefix();
ali@24
    99
    s=g_strconcat(prefix,"/var/log/pre-install",NULL);
ali@24
   100
    plover_log_open(s);
ali@24
   101
    g_free(s);
ali@24
   102
    s=g_strconcat(prefix,"/var/lib/razor",NULL);
ali@46
   103
    uri=razor_path_to_uri(s);
ali@24
   104
    g_free(s);
ali@46
   105
    razor_set_database_uri(uri);
ali@46
   106
    free(uri);
ali@24
   107
    if (verify_and_fix(prefix))
ali@24
   108
    {
ali@24
   109
	g_free(prefix);
ali@24
   110
	return -1;
ali@24
   111
    }
ali@95
   112
    retval=!plover_install_uri(repository,prefix,install,&error);
ali@24
   113
    if (!retval)
ali@95
   114
	retval=!plover_update_uri(repository,prefix,NULL,&error);
ali@24
   115
    if (error)
ali@24
   116
    {
ali@24
   117
	fprintf(stderr,"%s\n",error->message);
ali@24
   118
	g_error_free(error);
ali@24
   119
    }
ali@24
   120
#ifdef WIN32
ali@24
   121
    PostQuitMessage(retval);
ali@24
   122
    PostThreadMessage(main_thread_id,WM_QUIT,retval,0);
ali@24
   123
    _endthreadex(retval);
ali@24
   124
#endif
ali@24
   125
    return retval;
ali@24
   126
}
ali@24
   127
ali@24
   128
/*
ali@24
   129
 * The idea is that if pre_install() fails, update/setup should fall back
ali@24
   130
 * to console interfaces.
ali@24
   131
 */
ali@24
   132
#ifdef WIN32
ali@24
   133
HANDLE
ali@24
   134
#else
ali@24
   135
void *
ali@24
   136
#endif
ali@95
   137
pre_install(const char *repository)
ali@24
   138
{
ali@24
   139
#ifdef WIN32
ali@24
   140
    HANDLE retval;
ali@24
   141
#else
ali@24
   142
    void *retval;
ali@24
   143
#endif
ali@38
   144
    razor_set_lua_loader("posix",(void (*)())luaopen_posix);
ali@38
   145
    razor_set_lua_loader("whelk",(void (*)())luaopen_whelk);
ali@24
   146
#ifdef WIN32
ali@95
   147
    retval=(HANDLE)_beginthreadex(NULL,0,pre_install_thread,(void *)repository,
ali@95
   148
      0,NULL);
ali@24
   149
#else
ali@95
   150
    if (pre_install_thread((void *)repository))
ali@24
   151
	retval=NULL;
ali@24
   152
    else
ali@24
   153
	retval=(void *)1;		/* Non-NULL to indicate success */
ali@24
   154
#endif
ali@24
   155
    return retval;
ali@24
   156
}
ali@24
   157
ali@24
   158
#ifndef WIN32
ali@24
   159
int remove_ignore(const char *fpath,const struct stat *sb,int typeflag,
ali@24
   160
  struct FTW *ftwbuf)
ali@24
   161
{
ali@24
   162
    (void)remove(fpath);
ali@24
   163
    return 0;
ali@24
   164
}
ali@24
   165
#endif
ali@24
   166
ali@24
   167
gboolean deltree(const char *path)
ali@24
   168
{
ali@24
   169
#ifdef WIN32
ali@24
   170
    /* Based on g_local_file_trash() */
ali@24
   171
    SHFILEOPSTRUCTW op={0};
ali@24
   172
    gboolean success;
ali@24
   173
    wchar_t *wfilename;
ali@24
   174
    long len;
ali@24
   175
    wfilename=g_utf8_to_utf16(path,-1,NULL,&len,NULL);
ali@24
   176
    /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
ali@24
   177
    wfilename=g_renew(wchar_t,wfilename,len+2);
ali@24
   178
    wfilename[len+1]=0;
ali@24
   179
    op.wFunc=FO_DELETE;
ali@24
   180
    op.pFrom=wfilename;
ali@24
   181
    op.fFlags=FOF_NO_UI;
ali@24
   182
    success=!SHFileOperationW(&op);
ali@24
   183
    if (success && op.fAnyOperationsAborted)
ali@24
   184
	success=FALSE;
ali@24
   185
    g_free(wfilename);
ali@24
   186
    return success;
ali@24
   187
#else
ali@24
   188
    return nftw(path,remove_ignore,64,FTW_DEPTH|FTW_PHYS);
ali@24
   189
#endif
ali@24
   190
}
ali@24
   191
ali@24
   192
gboolean pre_uninstall(void)
ali@24
   193
{
ali@24
   194
    gboolean success;
ali@46
   195
    gchar *s,*uri;
ali@24
   196
    GError *error=NULL;
ali@38
   197
    razor_set_lua_loader("posix",(void (*)())luaopen_posix);
ali@38
   198
    razor_set_lua_loader("whelk",(void (*)())luaopen_whelk);
ali@24
   199
    prefix=plover_pre_install_prefix();
ali@24
   200
    s=g_strconcat(prefix,"/var/lib/razor",NULL);
ali@46
   201
    uri=razor_path_to_uri(s);
ali@24
   202
    g_free(s);
ali@46
   203
    razor_set_database_uri(uri);
ali@46
   204
    free(uri);
ali@24
   205
    success=plover_remove(NULL,&error);
ali@24
   206
    if (error)
ali@24
   207
    {
ali@24
   208
	fprintf(stderr,"%s\n",error->message);
ali@24
   209
	g_error_free(error);
ali@24
   210
    }
ali@24
   211
    deltree(prefix);
ali@24
   212
    return success;
ali@24
   213
}
ali@24
   214
ali@24
   215
#if defined(WIN32) && !defined(USE_G_SPAWN)
ali@24
   216
/* Based on glib's g_spawn_win32.c */
ali@24
   217
ali@24
   218
static gchar *
ali@24
   219
win32_cmdline_quote(const char *string)
ali@24
   220
{
ali@24
   221
    const gchar *p=string;
ali@24
   222
    gchar *retval,*q;
ali@24
   223
    gint len=0;
ali@24
   224
    gboolean need_dblquotes=FALSE;
ali@24
   225
    while (*p)
ali@24
   226
    {
ali@24
   227
	if (*p==' ' || *p=='\t')
ali@24
   228
	    need_dblquotes=TRUE;
ali@24
   229
	else if (*p=='"')
ali@24
   230
	    len++;
ali@24
   231
	else if (*p=='\\')
ali@24
   232
	{
ali@24
   233
	    const gchar *pp=p;
ali@24
   234
	    while (*pp && *pp=='\\')
ali@24
   235
		pp++;
ali@24
   236
	    if (*pp=='"')
ali@24
   237
		len++;
ali@24
   238
	}
ali@24
   239
	len++;
ali@24
   240
	p++;
ali@24
   241
    }
ali@24
   242
    q=retval=g_malloc(len+need_dblquotes*2+1);
ali@24
   243
    p=string;
ali@24
   244
    if (need_dblquotes)
ali@24
   245
	*q++='"';
ali@24
   246
    while (*p)
ali@24
   247
    {
ali@24
   248
	if (*p=='"')
ali@24
   249
	    *q++='\\';
ali@24
   250
	else if (*p=='\\')
ali@24
   251
	{
ali@24
   252
	    const gchar *pp=p;
ali@24
   253
	    while (*pp && *pp=='\\')
ali@24
   254
		pp++;
ali@24
   255
	    if (*pp=='"')
ali@24
   256
		*q++='\\';
ali@24
   257
	}
ali@24
   258
	*q++=*p;
ali@24
   259
	p++;
ali@24
   260
    }
ali@24
   261
    if (need_dblquotes)
ali@24
   262
	*q++='"';
ali@24
   263
    *q++='\0';
ali@24
   264
    return retval;
ali@24
   265
}
ali@24
   266
ali@24
   267
/* Create a win32-style wide-character argv with suitable quoting */
ali@24
   268
wchar_t **win32_argv_import(char **argv,GError **error)
ali@24
   269
{
ali@24
   270
    int i,n;
ali@24
   271
    gchar *s;
ali@24
   272
    wchar_t **wargv;
ali@24
   273
    GError *tmp_error=NULL;
ali@24
   274
    n=g_strv_length(argv);
ali@24
   275
    wargv=g_new(wchar_t *,n+1);
ali@24
   276
    for(i=0;i<n;i++)
ali@24
   277
    {
ali@24
   278
	s=win32_cmdline_quote(argv[i]);
ali@24
   279
	wargv[i]=g_utf8_to_utf16(s,-1,NULL,NULL,&tmp_error);
ali@24
   280
	g_free(s);
ali@24
   281
	if (!wargv[i])
ali@24
   282
	{
ali@24
   283
	    g_set_error(error,G_SPAWN_ERROR,G_SPAWN_ERROR_FAILED,
ali@24
   284
	      "Invalid argument #%d: %s",i,tmp_error->message);
ali@24
   285
	    g_error_free(tmp_error);
ali@24
   286
	    for(i--;i>=0;i--)
ali@24
   287
		g_free(wargv[i]);
ali@24
   288
	    g_free(wargv);
ali@24
   289
	    return FALSE;
ali@24
   290
	}
ali@24
   291
    }
ali@24
   292
    wargv[i]=NULL;
ali@24
   293
    return wargv;
ali@24
   294
}
ali@24
   295
ali@24
   296
gboolean spawn_sync(char **argv,GError **error)
ali@24
   297
{
ali@24
   298
    wchar_t *wargv0,**wargv;
ali@24
   299
    gintptr rc;
ali@24
   300
    GError *tmp_error=NULL;
ali@24
   301
    wargv0=g_utf8_to_utf16(argv[0],-1,NULL,NULL,&tmp_error);
ali@24
   302
    if (!wargv0)
ali@24
   303
    {
ali@24
   304
	fprintf(stderr,"Conversion error in post\n");
ali@24
   305
	g_set_error(error,G_SPAWN_ERROR,G_SPAWN_ERROR_FAILED,
ali@24
   306
	  "Invalid program name: %s",tmp_error->message);
ali@24
   307
	g_error_free(tmp_error);
ali@24
   308
	return FALSE;
ali@24
   309
    }
ali@24
   310
    wargv=win32_argv_import(argv,error);
ali@24
   311
    if (!wargv)
ali@24
   312
    {
ali@24
   313
	fprintf(stderr,"Conversion error in post\n");
ali@24
   314
	g_free(wargv0);
ali@24
   315
	return FALSE;
ali@24
   316
    }
ali@24
   317
    errno=0;
ali@24
   318
    rc=_wspawnvp(P_WAIT,wargv0,(const wchar_t **)wargv);
ali@24
   319
    g_free(wargv0);
ali@24
   320
    g_strfreev((gchar **)wargv);
ali@24
   321
    if (rc==-1 && errno!=0)
ali@24
   322
    {
ali@24
   323
	fprintf(stderr,"Failed to start post command (%s)\n",g_strerror(errno));
ali@24
   324
	g_set_error(error,G_SPAWN_ERROR,G_SPAWN_ERROR_FAILED,
ali@24
   325
	  "Failed to execute post command: %s",g_strerror(errno));
ali@24
   326
	return FALSE;
ali@24
   327
    }
ali@24
   328
    if (rc!=EXIT_SUCCESS)
ali@24
   329
    {
ali@24
   330
	fprintf(stderr,"post command failed (%ld)\n",(long)rc);
ali@24
   331
	g_set_error(error,G_SPAWN_ERROR,G_SPAWN_ERROR_FAILED,
ali@24
   332
	  "Post command exited with code %ld",(long)rc);
ali@24
   333
	return FALSE;
ali@24
   334
    }
ali@24
   335
    return TRUE;
ali@24
   336
}
ali@24
   337
ali@24
   338
#endif /* defined(WIN32) && !defined(USE_G_SPAWN) */
ali@24
   339
ali@24
   340
/*
ali@24
   341
 * Run a command after completing request.
ali@24
   342
 *
ali@24
   343
 * Command may refer to %INSTALL_PREFIX% which will be replaced by the
ali@95
   344
 * (first) install prefix used, by %TEST_RESULT% which will be replaced
ali@80
   345
 * by either "pass" or "fail" depending as to whether the request succeeded
ali@95
   346
 * or not and/or by %REPOSITORY% which will be replaced by the URI of the
ali@95
   347
 * repository used. Command may also include double quotes which will be used
ali@95
   348
 * to affect how the command is split into arguments much like a shell does.
ali@24
   349
 */
ali@95
   350
gboolean run_post(int argc,char **argv,gboolean test_result,
ali@95
   351
  const char *repository,GError **error)
ali@24
   352
{
ali@24
   353
    int i,post_argc;
ali@24
   354
    char *s;
ali@24
   355
    gchar *expanded;
ali@24
   356
    gchar **post_argv;
ali@24
   357
#ifdef USE_G_SPAWN
ali@24
   358
    gchar *standard_output,*standard_error;
ali@24
   359
    int exit_status;
ali@24
   360
#endif
ali@24
   361
    GError *tmp_error=NULL;
ali@87
   362
    if (argc<2)
ali@24
   363
    {
ali@24
   364
	g_set_error_literal(error,G_FILE_ERROR,G_FILE_ERROR_NOENT,
ali@24
   365
	  "--post: No command given");
ali@24
   366
	return FALSE;
ali@24
   367
    }
ali@24
   368
    printf("Running post command: %s\n",argv[1]);
ali@24
   369
    if (!g_shell_parse_argv(argv[1],&post_argc,&post_argv,&tmp_error))
ali@24
   370
    {
ali@24
   371
	g_propagate_prefixed_error(error,tmp_error,"%s: ",argv[1]);
ali@24
   372
	return FALSE;
ali@24
   373
    }
ali@24
   374
    for(i=0;i<post_argc;i++)
ali@24
   375
    {
ali@24
   376
	s=strstr(post_argv[i],"%INSTALL_PREFIX%");
ali@24
   377
	if (s)
ali@24
   378
	{
ali@24
   379
	    *s='\0';
ali@24
   380
	    s+=strlen("%INSTALL_PREFIX%");
ali@24
   381
	    expanded=g_strconcat(post_argv[i],prefix,s,NULL);
ali@24
   382
	    g_free(post_argv[i]);
ali@24
   383
	    post_argv[i]=expanded;
ali@24
   384
	}
ali@24
   385
	s=strstr(post_argv[i],"%TEST_RESULT%");
ali@24
   386
	if (s)
ali@24
   387
	{
ali@24
   388
	    *s='\0';
ali@24
   389
	    s+=strlen("%TEST_RESULT%");
ali@24
   390
	    expanded=g_strconcat(post_argv[i],test_result?"pass":"fail",s,NULL);
ali@24
   391
	    g_free(post_argv[i]);
ali@24
   392
	    post_argv[i]=expanded;
ali@24
   393
	}
ali@95
   394
	s=strstr(post_argv[i],"%REPOSITORY%");
ali@95
   395
	if (s)
ali@95
   396
	{
ali@95
   397
	    *s='\0';
ali@95
   398
	    s+=strlen("%REPOSITORY%");
ali@95
   399
	    expanded=g_strconcat(post_argv[i],repository,s,NULL);
ali@95
   400
	    g_free(post_argv[i]);
ali@95
   401
	    post_argv[i]=expanded;
ali@95
   402
	}
ali@24
   403
    }
ali@24
   404
#ifdef USE_G_SPAWN
ali@24
   405
    if (!g_spawn_sync(NULL,post_argv,NULL,G_SPAWN_SEARCH_PATH,NULL,NULL,
ali@24
   406
      &standard_output,&standard_error,&exit_status,&tmp_error))
ali@24
   407
    {
ali@24
   408
	fprintf(stderr,"Failed to start post command\n");
ali@24
   409
	g_propagate_prefixed_error(error,tmp_error,"%s: ",post_argv[0]);
ali@24
   410
	return FALSE;
ali@24
   411
    }
ali@24
   412
    if (standard_output && *standard_output)
ali@24
   413
    {
ali@24
   414
	printf("Output from post command %s:\n",post_argv[0]);
ali@24
   415
	fputs(standard_output,stdout);
ali@24
   416
    }
ali@24
   417
    g_free(standard_output);
ali@24
   418
    if (standard_error && *standard_error)
ali@24
   419
    {
ali@24
   420
	printf("Error output from post command %s:\n",post_argv[0]);
ali@24
   421
	fputs(standard_error,stdout);
ali@24
   422
    }
ali@24
   423
    g_free(standard_error);
ali@24
   424
    if (!g_spawn_check_exit_status(exit_status,&tmp_error))
ali@24
   425
    {
ali@24
   426
	fprintf(stderr,"post command failed\n");
ali@24
   427
	g_propagate_prefixed_error(error,tmp_error,"%s: ",post_argv[0]);
ali@24
   428
	return FALSE;
ali@24
   429
    }
ali@24
   430
#else
ali@24
   431
    if (!spawn_sync(post_argv,&tmp_error))
ali@24
   432
    {
ali@24
   433
	g_propagate_prefixed_error(error,tmp_error,"%s: ",post_argv[0]);
ali@24
   434
	return FALSE;
ali@24
   435
    }
ali@24
   436
#endif
ali@24
   437
    return TRUE;
ali@24
   438
}
ali@24
   439
ali@24
   440
#ifdef WIN32
ali@95
   441
DWORD win32_pre_install_gui(char *repository)
ali@24
   442
{
ali@24
   443
    HANDLE thread;
ali@24
   444
    INITCOMMONCONTROLSEX icc={0,};
ali@24
   445
    MSG msg;
ali@24
   446
    DWORD retval;
ali@24
   447
    main_thread_id=GetCurrentThreadId();
ali@95
   448
    thread=(HANDLE)pre_install(repository);
ali@24
   449
    if (!thread)
ali@24
   450
	return EXIT_FAILURE;
ali@24
   451
    icc.dwSize=sizeof(icc);
ali@24
   452
    icc.dwICC=ICC_WIN95_CLASSES;
ali@24
   453
    InitCommonControlsEx(&icc);
ali@24
   454
    DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_PROGRESSDIALOG),NULL,
ali@24
   455
      &ProgressDialogProc);
ali@24
   456
    while(GetMessage(&msg,NULL,0,0)>0)
ali@24
   457
    {
ali@24
   458
	TranslateMessage(&msg);
ali@24
   459
	DispatchMessage(&msg);
ali@24
   460
    }
ali@24
   461
    WaitForSingleObject(thread,INFINITE);
ali@24
   462
    GetExitCodeThread(thread,&retval);
ali@24
   463
    CloseHandle(thread);
ali@24
   464
    return retval;
ali@24
   465
}
ali@24
   466
#endif	/* WIN32 */
ali@24
   467
ali@95
   468
gchar *pre_install_default_repository(const char *argv0)
ali@94
   469
{
ali@94
   470
    size_t length;
ali@94
   471
    void *contents;
ali@94
   472
    gchar *path;
ali@94
   473
    gchar *s,*uri;
ali@94
   474
    struct razor_error *tmp_error=NULL;
ali@94
   475
    /*
ali@95
   476
     * The default repository is the executable itself if it's an archive
ali@94
   477
     * or otherwise the directory in which the executable is stored.
ali@94
   478
     */
ali@94
   479
    path=plover_get_program(argv0);
ali@94
   480
    uri=razor_path_to_uri(path);
ali@95
   481
    free(path);
ali@94
   482
    s=g_strconcat(uri,"/repodata/comps.xml",NULL);
ali@94
   483
    contents=razor_uri_get_contents(s,&length,FALSE,&tmp_error);
ali@94
   484
    g_free(s);
ali@94
   485
    if (contents)
ali@94
   486
	razor_uri_free_contents(contents,length);
ali@94
   487
    else
ali@94
   488
    {
ali@94
   489
	if (razor_error_matches(tmp_error,RAZOR_GENERAL_ERROR,
ali@94
   490
	  RAZOR_GENERAL_ERROR_UNSUPPORTED_ARCHIVE))
ali@94
   491
	{
ali@94
   492
	    path=plover_get_program_directory(argv0);
ali@95
   493
	    uri=razor_path_to_uri(path);
ali@95
   494
	    free(path);
ali@94
   495
	}
ali@94
   496
	razor_error_free(tmp_error);
ali@94
   497
    }
ali@95
   498
    return uri;
ali@94
   499
}
ali@94
   500
ali@24
   501
int main(int argc,char **argv)
ali@24
   502
{
ali@80
   503
    gboolean success,uninstall=FALSE,enable_post=FALSE;
ali@24
   504
    GError *error=NULL;
ali@95
   505
    gchar *path=NULL,*repository=NULL;
ali@80
   506
    GOptionContext *context;
ali@80
   507
    GOptionEntry options[]={
ali@95
   508
	{"repository",0,0,G_OPTION_ARG_STRING,&repository,
ali@95
   509
	  "Repository location","uri"},
ali@80
   510
	{"path",0,0,G_OPTION_ARG_FILENAME,&path,
ali@80
   511
	  "Repository path","path"},
ali@80
   512
	{"uninstall",'u',0,G_OPTION_ARG_NONE,&uninstall,
ali@80
   513
	  "Uninstall all packages",NULL},
ali@80
   514
	{"post",0,0,G_OPTION_ARG_NONE,&enable_post,
ali@80
   515
	  "Run command after request is processed",NULL},
ali@80
   516
	{NULL}
ali@80
   517
    };
ali@24
   518
#ifdef WIN32
ali@24
   519
    /*
ali@24
   520
     * pre-inst is normally a GUI application, but rpm scripts may well
ali@24
   521
     * call console applications and it looks ugly if console windows keep
ali@24
   522
     * popping up. Avoid this by allocating our own console and hiding it.
ali@24
   523
     * Note:
ali@24
   524
     *	- If pre-inst is a console application (typically for debugging),
ali@24
   525
     *    then skip this step.
ali@24
   526
     *  - Call ShowWindow twice to negate special handling on first call.
ali@24
   527
     */
ali@24
   528
    if (!GetConsoleWindow())
ali@24
   529
    {
ali@24
   530
	AllocConsole();
ali@24
   531
	ShowWindow(GetConsoleWindow(),SW_HIDE);
ali@24
   532
	ShowWindow(GetConsoleWindow(),SW_HIDE);
ali@24
   533
    }
ali@24
   534
#endif
ali@31
   535
    plover_exception_handler_init();
ali@80
   536
    context=g_option_context_new("[command] - install the main installer");
ali@80
   537
    g_option_context_add_main_entries(context,options,NULL);
ali@80
   538
    g_option_context_set_description(context,
ali@80
   539
      "If --post is specified, then the command to run and its arguments\n"
ali@80
   540
      "should be listed at the end of the command line.\n"
ali@80
   541
      "Command may refer to %INSTALL_PREFIX% which will be replaced by the\n"
ali@95
   542
      "(first) install prefix used, by %TEST_RESULT% which will be\n"
ali@80
   543
      "replaced by either \"pass\" or \"fail\" depending as to whether the\n"
ali@95
   544
      "request succeeded or not and/or by %REPOSITORY% which will be\n"
ali@95
   545
      "replaced by the URI of the repository used. Command may also include\n"
ali@95
   546
      "double quotes which will be used to affect how the command is split\n"
ali@95
   547
      "into arguments much like a shell does.");
ali@80
   548
    g_option_context_set_strict_posix(context,TRUE);
ali@80
   549
    g_option_context_set_ignore_unknown_options(context,TRUE);
ali@80
   550
    if (!g_option_context_parse(context,&argc,&argv,&error))
ali@24
   551
    {
ali@80
   552
	g_printerr("pre-install: %s\n",error->message);
ali@80
   553
	g_printerr("Use \"%s --help\" for help\n",(*argv)[0]);
ali@80
   554
	exit(1);
ali@80
   555
    }
ali@95
   556
    if (repository && path)
ali@95
   557
    {
ali@95
   558
	g_printerr("pre-install: "
ali@95
   559
	  "Only one of --repository and --path can be specified\n");
ali@95
   560
	exit(1);
ali@95
   561
    }
ali@95
   562
    if (path)
ali@95
   563
	repository=razor_path_to_uri(path);
ali@95
   564
    else if (!repository)
ali@95
   565
	repository=pre_install_default_repository(argv[0]);
ali@80
   566
    if (uninstall)
ali@24
   567
	success=pre_uninstall();
ali@80
   568
    else
ali@80
   569
    {
ali@80
   570
#ifdef WIN32
ali@95
   571
	success=win32_pre_install_gui(repository)==EXIT_SUCCESS;
ali@80
   572
#else
ali@95
   573
	success=!!pre_install(repository);
ali@80
   574
#endif
ali@24
   575
    }
ali@95
   576
    if (enable_post && !run_post(argc,argv,success,repository,&error))
ali@24
   577
    {
ali@24
   578
#ifndef WIN32
ali@24
   579
	fprintf(stderr,"Error in post: %s\n",error->message);
ali@24
   580
#else
ali@24
   581
	MessageBox(NULL,error->message,"Error in post",MB_ICONERROR|MB_OK);
ali@24
   582
#endif
ali@24
   583
	g_error_free(error);
ali@24
   584
	success=FALSE;
ali@24
   585
    }
ali@24
   586
#ifdef WIN32
ali@24
   587
    return success?EXIT_SUCCESS:EXIT_FAILURE;
ali@24
   588
#else
ali@24
   589
    return success?0:1;
ali@24
   590
#endif
ali@24
   591
}