

unloadGDALdrivers <- function(x) {
	.removeDriver(x)
}

proj_ok <- function() {
	.proj_conf_test()
}


projNetwork <- function(enable, url="") {
	if (missing(enable)) {
		as.logical(.PROJ_network(-1, ""))
	} else {
		if (enable) {
			out <- .PROJ_network(1, url)
			if (out != "") {
				message("PROJ network enabled. Using CDN URL: ", out)
			} else {
				message("PROJ network enabled")
			}
		} else {
			.PROJ_network(0, "")
			message("PROJ network disabled")
		}
		invisible(NULL)
	}
}


projPaths <- function(paths, with_proj = TRUE) {
	if (missing(paths)) {
		.get_proj_search_paths()
	} else {
		paths <- as.character(paths)
		.set_proj_search_paths(paths, with_proj)
		invisible(.get_proj_search_paths())
	}
}


fileBlocksize <- function(x) {
	v <- x@pntr$getFileBlocksize()
	m <- matrix(v, ncol=2)
	colnames(m) <- c("rows", "cols")
	m
}


clearVSIcache <- function() {
	.clearVSIcache(TRUE)
}

gdalCache <- function(size=NA) {
	vsi <- FALSE # vsi not working
	if (is.null(size) || is.na(size)) {
		.getGDALCacheSizeMB(vsi)
	} else if (size > 0) {
		.setGDALCacheSizeMB(size, vsi)
	}
}


getGDALconfig <- function(option) {
	sapply(option, .gdal_getconfig)
}

setGDALconfig <- function(option, value="") {
	value <- rep_len(value, length(option))
	for (i in 1:length(option)) {
		if (grepl("=", option[i])) {
			opt <- trimws(unlist(strsplit(option[i], "="))[1:2])
			.gdal_setconfig(opt[1], opt[2])
		} else {
			.gdal_setconfig(trimws(option[i]), trimws(value[i]))
		}
	}
}


libVersion <- function(lib="all", parse=FALSE) {
	lib <- tolower(lib)
	if (lib=="gdal") {
		out <- .gdal_version()
	} else if (lib=="proj") {
		out <- proj_version()
	} else if (lib=="geos") {
		out <- .geos_version()
	} else if (lib=="TBB") {
		return(.have_TBB())
	} else {
		out <- data.frame(gdal=.gdal_version(), proj=proj_version(), geos=.geos_version(), TBB=.have_TBB())
	}
	if (parse) {
		if (lib=="all") {
			out$TBB <- NULL
		} else if (lib == "tbb") {
			return(out)
		}
		nms <- names(out)
		out <- unlist(out)
		out <- data.frame(matrix(as.numeric(unlist(strsplit(out, "\\."))), ncol=3, byrow=TRUE), row.names=nms)
		names(out) <- c("major", "minor", "sub")
	}
	out
}




gdal <- function(warn=NA, drivers=FALSE, ...) {
	if (!is.na(warn)) {
		warn <- as.integer(warn)
		stopifnot(warn %in% (1:4))
		.set_gdal_warnings(warn)
	} else if (drivers) {
		x <- .gdaldrivers()
		x <- do.call(cbind, x)
		x <- data.frame(x)
		x[,2] = c(FALSE, TRUE)[as.integer(x[,2])+1]
		x[,3] = c(FALSE, TRUE)[as.integer(x[,3])+1]
		x[,4] = c("read", "read/write", "read/write")[as.integer(x[,4])+1]
		colnames(x) <- c("name", "raster", "vector", "can", "vsi", "long.name")
		x[,5] <- x[,5] == 1
		x <- x[order(x$name), ]
		rownames(x) <- NULL
		x
	} else {
		dots <- list(...)
		if (length(dots) > 0) {
			libVersion(dots[1])
		} else {
			libVersion("gdal")		
		}
	}
}



.describe_sds <- function(x, print=FALSE) {
	x <- .sdinfo(x)
	if (length(x[[1]]) == 1 & length(x[[2]]) == 0) {
		error("gdal (sds)", x[[1]])
	}
	names(x) <- c("name", "var", "desc", "nrow", "ncol", "nlyr")
	m <- do.call(cbind, x)
	m <- data.frame(id=1:nrow(m), m, stringsAsFactors=FALSE)
	ii <- which(colnames(m) %in% c("nrow", "ncol", "nlyr"))
	for (i in ii) m[,i] <- as.integer(m[,i])
	if (print) {
		print(m)
		invisible(m)
	} else {
		m
	}
}



.meta_sds <- function(x, parse=FALSE, ...) {
	if (parse) {
		m <- .parsedsdsmetadata(x)
		m <- do.call(cbind, m)
		if (nrow(m) > 0) {
			m <- data.frame(1:nrow(m), m, stringsAsFactors=FALSE)
		} else {
			m <- data.frame(0[0], m, stringsAsFactors=FALSE)
		}
		for (i in 5:7) m[,i] <- as.integer(m[,i])
		colnames(m) <- c("id", "name", "var", "desc", "nrow", "ncol", "nlyr")
	} else {
		m <- .sdsmetadata(x)
	}
	m
}

mdinfo_simplify <- function(p) {
	p <- gsub('\"', "", strsplit(p, "\n")[[1]])
	p <- p[-c(1, length(p))]
	p <- gsub("^  ", "", p)
	p <- gsub(",$", "", p)
	  
	s <- grep("\\[", p)[-1]
	e <- grep("]", p)
	i <- which(s > e[-length(e)])[1]
	e <- e[-i]
	if (max(e-s) < 8) {
		for (i in 1:length(s)) {
			p[s[i]] <- paste0(p[s[i]], paste(trimws(p[(s[i]+1):e[i]]), collapse=""))
		}
		s <- s + 1
		r <- unlist(lapply(1:length(s), function(i) s[i]:e[i]))
		p <- p[-r]
	}
	p
}

ar_info <- function(x, what="describe", simplify=TRUE, filter=TRUE, array="") {
	what <- match.arg(tolower(what), c("describe", "arrays", "dimensions"))
	if (what == "describe") {
		p <- .gdalmdinfo(x, ""[0])
		if (simplify) {
			pp <- try(mdinfo_simplify(p), silent=TRUE)
			if (!inherits(pp, "try-error")) {
				p <- pp
			}
		}
		return(p)
	} else if (what == "arrays") {
		.arnames(x, filter);
	} else if (what == "dimensions") {
		d <- .dimfo(x, array)
		if (d$size[1] == -99) {
			error("descar", d$name[1])
		}
		data.frame(d);
	}
}


setMethod("describe", signature(x="character"),
	function(x, sds=FALSE, meta=FALSE, parse=FALSE, options="", print=FALSE, open_opt="") {

		x <- .fullFilename(x[1], FALSE)
		#x <- x[1]
		if (meta) {
			if (sds) {
				return(.meta_sds(x, parse))
			} else {
				return(.metadata(x))
			}
		}

		if (sds) {
			return(.describe_sds(x, print=print))
		}
		options <- unique(trimws(options))
		options <- options[options != ""]
		if (length(options) > 0) {
			options <- paste0("-", options)
			options <- gsub("^--", "-", options)
		}
		open_opt <- unique(trimws(open_opt))
		open_opt <- open_opt[open_opt != ""]
		g <- .gdalinfo(x, options, open_opt)

		if (g == "") {
			add <- ifelse(!file.exists(x), "\n", "\nThe file does not exist\n")
			x <- paste0("GDAL cannot open: ", x, add)
		}
		y <- unlist(strsplit(g, "\n"))
		if (print) {
			cat(g, "\n")
			invisible(y)
		} else {
			return(y)
		}
	}
)

setMethod("describe", signature(x="SpatRaster"),
	function(x, source=1, ...) {
		if (!hasValues(x)) return(NULL)
		source <- round(source)
		if ((source < 1) || (source > nsrc(x))) {
			error("describe", "source should be >= 1 and <= nsrc()")
		}
		describe(sources(x)[source])
	}
)
