use std

use "config"
use "opts"
use "types"
use "util"
use "build"

pkg bld =
	const install	: (p : build#	-> bool)
	const uninstall	: (p : build#	-> bool)
;;

const install = {b
	-> buildtarg(b, "all") && movetargs(b, false)
}

const uninstall = {b
	-> movetargs(b, true)
}

const movetargs = {b, rm
	var inst, name

	inst = std.htgetv(b.deps.targs, "all", [][:])
	for n : inst
		if n.instdir.len == 0
			continue
		;;
		for g : n.gen
			name = n.instname
			if name.len == 0
				name = std.basename(g)
			;;
			if !movefile(b, rm, g, n.instdir, name, n.instmode)
				-> false
			;;
		;;
	;;
	-> true

}

const movefile = {b, rm, file, prefix, instname, perm
	var path, ok

	ok = true
	path = std.pathjoin([opt_destdir, opt_instbase, prefix, instname][:])
	if rm
		mbldput("\trm {}\n", path)
		if !std.remove(path)
			mbldput("\t\tno such file {}\n", file)
		;;
	else
		std.remove(path)
		std.mkpath(std.dirname(path))
		match std.slurp(file)
		| `std.Err m:	std.fatal("could not open {} for reading\n", file)
		| `std.Ok buf:
			mbldput("\t{} => {}\n", file, path)
			if !std.blat(path, buf, perm)
				mbldput("could not write {}\n", file)
				ok = false
			;;
		;;
	;;
	std.slfree(path)
	-> ok
}

