#!/bin/sh

# iohyve v0.7.9 2017/06/26 "I Believe in You, You Can Do the Thing Edition"

# Set proper path thanks to iocage
PATH=${PATH}:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin

# Check if libs are available (stolen from iocage)
if [ -e "./lib/ioh-cmd" ] ; then
	LIB="./lib"
elif [ -e "/usr/local/lib/iohyve" ] ; then
	LIB="/usr/local/lib/iohyve"
else
	echo "ERROR: missing libraries"
	exit 1
fi

. /etc/rc.subr

. "${LIB}/ioh-cmd"
. "${LIB}/ioh-setup"
. "${LIB}/ioh-zfs"
. "${LIB}/ioh-console"
. "${LIB}/ioh-iso"
. "${LIB}/ioh-firmware"
. "${LIB}/ioh-guest"
. "${LIB}/ioh-disk"
. "${LIB}/ioh-tap"

# Show version
__version() {
	echo "iohyve v0.7.9 2017/06/26 I Believe in You, You Can Do the Thing Edition"
}

# Print help page
__help() {
cat << 'EOT'
iohyve  version
	setup <pool=poolname> [kmod=0|1] [net=iface]
	list [-l]
	info [-vsdl]
	isolist
	fwlist
	fetchiso <URL>
	cpiso <path>
	renameiso <ISO> <newname>
	rmiso <ISO>
	fetchfw <URL>
	cpfw <path>
	renamefw <firmware> <newname>
	rmfw <firmware>
	create <name> <size> [pool]
	install <name> <ISO>
	load <name> <path/to/bootdisk>
	boot <name> [runmode] [pcidevices]
	start <name> [-s | -a]
	stop <name>
	forcekill <name>
	scram
	destroy <name>
	rename <name> <newname>
	delete [-f] <name>
	set <name> <property=value> ...
	get <name> <prop>
	rmprop [-f] <name> <property>
	getall <name>
	add <name> <size>
	remove [-f] <name> <diskN>
	resize <name> <diskN> <size>
	disks <name>
	snap <name>@<snap>
	roll <name>@<snap>
	rmsnap [-f] <name>@<snap>
	clone [-r] <name> <clonename>
	export <name>
	snaplist
	taplist
        tapadd <name> [iface]
        tapdel <name> <tap>
	activetaps
	conlist
	console <name>
	conreset
	help
EOT
}

if [ -z "$1" ] ; then
	__help
	exit 0
fi

if __root_req_cmd "$1" && [ "$(id -u)" != "0" ]; then
    echo "The $1 command needs root credentials!"
    exit 1
fi

__parse_cmd "$@"
