1#  File share/R/REMOVE.R
2#  Part of the R package, https://www.R-project.org
3#
4#  Copyright (C) 1995-2020 The R Core Team
5#
6#  This program is free software; you can redistribute it and/or modify
7#  it under the terms of the GNU General Public License as published by
8#  the Free Software Foundation; either version 2 of the License, or
9#  (at your option) any later version.
10#
11#  This program is distributed in the hope that it will be useful,
12#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#  GNU General Public License for more details.
15#
16#  A copy of the GNU General Public License is available at
17#  https://www.r-project.org/Licenses/
18
19Usage <- function() {
20    cat("Usage: R CMD REMOVE [options] pkgs",
21        "",
22        "Remove the add-on packages specified by pkgs.  The library tree to",
23        "remove from can be specified via '--library'.  By default, packages are",
24        "removed from the library tree rooted at the first directory in",
25        ".libPaths() for an R session run in the current environment.",
26        "",
27        "Options:",
28        "  -h, --help		print short help message and exit",
29        "  -v, --version		print REMOVE version info and exit",
30        "  -l, --library=LIB	remove packages from library tree LIB",
31        "",
32        "Report bugs at <https://bugs.R-project.org>.", sep="\n")
33}
34
35options(showErrorCalls = FALSE, warn = 1) # deferred warnings will not be seen
36pkgs <- character(0)
37lib <- ""
38args <- commandArgs(TRUE)
39while(length(args)) {
40    a <- args[1]
41    if(a %in% c("-h", "--help")) {
42        Usage()
43        q("no")
44    }
45    else if(a %in% c("-v", "--version")) {
46        cat("R add-on package remover: ",
47            R.version[["major"]], ".",  R.version[["minor"]],
48            " (r", R.version[["svn rev"]], ")\n", sep = "")
49        cat("",
50            "Copyright (C) 2000-2009 The R Core Team.",
51            "This is free software; see the GNU General Public License version 2",
52            "or later for copying conditions.  There is NO warranty.",
53            sep="\n")
54        q("no")
55    }
56    else if(a == "-l") {
57        if(length(args) >= 2) {lib <- args[2]; args <- args[-1]}
58        else stop("-l option without value", call. = FALSE)
59    } else if(substr(a, 1, 10) == "--library=")
60        lib <- substr(a, 11, 1000)
61    else pkgs <- c(pkgs, a)
62    args <- args[-1]
63}
64if(!length(pkgs))
65    stop("ERROR: no packages specified", call.=FALSE)
66if(!nzchar(lib)) {
67    lib <- .libPaths()[1]
68    message("Removing from library ", sQuote(lib))
69} else {
70    ## lib is allowed to be a relative path.
71    ## should be OK below, but be sure.
72    cwd <- try(setwd(path.expand(lib)), silent = TRUE)
73    if(inherits(cwd, "try-error"))
74        stop("ERROR: cannot cd to directory ", sQuote(lib), call. = FALSE)
75    lib <- getwd()
76    setwd(cwd)
77}
78if(!utils::file_test("-d", lib) || file.access(lib, 2L))
79    stop("ERROR: no permission to remove from directory ", sQuote(lib),
80         call. = FALSE)
81utils::remove.packages(pkgs, lib)
82q("no")
83