View source with raw comments or as raw
    1/*  Part of ClioPatria SeRQL and SPARQL server
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2018, University of Amsterdam,
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(cp_setup,
   37          [ setup/0
   38          ]).   39:- prolog_load_context(directory, Dir),
   40   directory_file_path(Dir, lib, LibDir),
   41   asserta(user:file_search_path(library, LibDir)).   42
   43user:message_hook(git(update_versions), informational, _).
   44
   45:- use_module(library(setup)).   46:- use_module(library(option)).   47:- use_module(library(lists)).   48:- use_module(library(apply)).   49:- use_module(library(filesex)).   50
   51:- multifile
   52    user:file_search_path/2.   53:- dynamic
   54    user:file_search_path/2.   55
   56:- prolog_load_context(directory, Dir),
   57   asserta(user:file_search_path(cliopatria, Dir)).   58
   59:- initialization(setup, main).
 setup
Setup ClioPatria. This installs files *.in from the ClioPatria after localization and creates config-enabled.
   66setup :-
   67    options(Options),
   68    setup(Options).
   69
   70setup(Options) :-
   71    cliopatria_dir(ClioDir),
   72    install_dir(Dir),
   73    (   option(help(true), Options)
   74    ->  true
   75    ;   setup_scripts(ClioDir, Dir)
   76    ),
   77    directory_file_path(ClioDir, 'lib/APPCONF.txt.in', ReadmeIn),
   78    directory_file_path(ClioDir, 'config-available', ConfigAvail),
   79    directory_file_path(Dir,     'config-enabled', ConfigEnabled),
   80    setup_default_config(ConfigEnabled, ConfigAvail,
   81                         [ readme(ReadmeIn)
   82                         | Options
   83                         ]),
   84    setup_goodbye.
   85
   86cliopatria_dir(Dir) :-
   87    absolute_file_name(cliopatria(.),
   88                       Dir,
   89                       [ file_type(directory),
   90                         access(read)
   91                       ]).
   92
   93install_dir(Dir) :-
   94    current_prolog_flag(windows, true),
   95    !,
   96    working_directory(CWD, CWD),
   97    (   get(@(display), win_directory,
   98            'Create ClioPatria project in', CWD, Dir)
   99    ->  true
  100    ;   halt(1)
  101    ).
  102install_dir(DIR) :-
  103    working_directory(DIR, DIR).
  104
  105
  106setup:substitutions([ 'SWIPL'=PL,               % Prolog executable (for #!...)
  107                      'CLIOPATRIA'=ClioDir,     % ClioPatria directory
  108                      'CWD'=CWD,                % This directory
  109                      'PARENTDIR'=Parent,       % Parent of CWD
  110                      'HASHBANG'=HashBang,      % #! (or not)
  111                      'LOADOPTIONS'=LoadOptions % -s (or not)
  112                    ]) :-
  113    cliopatria_dir(ClioDir),
  114    working_directory(CWD, CWD),
  115    file_directory_name(CWD, Parent),
  116    setup_prolog_executable(PL),
  117    hashbang(HashBang),
  118    load_options(LoadOptions).
  119
  120hashbang('%!') :- current_prolog_flag(windows, true), !.
  121hashbang('#!').
  122
  123load_options('').
 options(-Options) is det
Options is a list of (long) commandline options. This uses a simple generic conversion between command-line argument and option value, defined as follows:
--without-Xwithout(X)
--with-Xwith(X)
--Name=ValueName(Value)
--NameName(true)
  137options(Options) :-
  138    current_prolog_flag(argv, Argv),
  139    (   append(_, [--|AV], Argv)
  140    ->  true
  141    ;   AV = Argv
  142    ),
  143    maplist(cmd_option, AV, Options).
  144
  145cmd_option(Text, Option) :-
  146    atom_concat('--without-', Module, Text),
  147    !,
  148    Option = without(Module).
  149cmd_option(Text, Option) :-
  150    atom_concat('--with-', Module, Text),
  151    !,
  152    Option = with(Module).
  153cmd_option(Text, Option) :-
  154    atom_concat(--, Rest, Text),
  155    !,
  156    (   sub_atom(Rest, B, _, A, =)
  157    ->  sub_atom(Rest, 0, B, _, Name),
  158        sub_atom(Rest, _, A, 0, OptVal),
  159        canonical_value(OptVal, Value),
  160        Option =.. [Name,Value]
  161    ;   Option =.. [Rest,true]
  162    ).
  163cmd_option(Text, Text).
  164
  165canonical_value(Text, Number) :-
  166    catch(atom_number(Text, Number), _, true),
  167    !.
  168canonical_value(Text, Text)