shithub: mc

ref: 1100eb4309cef7fbc7f488a7d066d1f16d80409f
dir: /install.myr/

View raw version
use std

use "config.use"
use "deps.use"
use "opts.use"
use "parse.use"
use "subdir.use"
use "types.use"
use "util.use"

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

const install = {p
	-> movetargs(p, false)
}

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

const movetargs = {p, delete
	var libarchive

	for t in p.targs
		match t
		| `Bin [.name=bin, .inputs=leaves, .install=true]:
			movefile(delete, bin, opt_instroot, opt_destdir, "bin")
		| `Lib [.name=lib, .inputs=leaves, .install=true]:
			movefile(delete, lib, opt_instroot, opt_destdir, "lib/myr")
			libarchive = std.fmt("lib%s.a", lib)
			movefile(delete, libarchive, opt_instroot, opt_destdir, "lib/myr")
			std.slfree(libarchive)
		| `Sub subs:
			subdirs(p, subs, `std.None)
		| `Man mans:
			/* FIXME: figure out man section by number */
			for m in mans
				moveman(delete, m)
			;;
		;;
	;;
	-> true
}


const movefile = {delete, file, instdir, destdir, prefix
	var path

	path = std.pathjoin([destdir, instdir, prefix, file][:])
	if delete
		std.put("\t%s deleted\n", path)
		if (std.unlink(path) < 0)
			std.put("\t\tno such file %s\n", file)
		;;
	else
		std.put("\t%s => %s\n", file, path)
		std.unlink(path)
		match std.slurp(file)
		| `std.Fail m:	std.fatal(1, "Could not open %s for reading", file)
		| `std.Ok buf:
			std.blat(path, buf)
			std.slfree(buf)
		;;
	;;
	std.slfree(path)
}

const moveman = {delete, man
	var sect, manrel

	std.put("man = %s\n", man)
	match std.strrfind(man, ".")
	| `std.None:
		std.fatal(1, "manpage %s has no section\n", man)
	| `std.Some s:
		sect = s + 1
		if s + 1 == man.len
			std.fatal(1, "manpage %s missing suffix\n", man)
		;;
	;;

	manrel = std.fmt("share/man%s", man[sect:])
	movefile(delete, man, opt_instroot, opt_destdir, manrel)
	std.slfree(manrel)
}