#!/bin/sh

# List iso's
__iso_list() {
	echo "Listing ISO's..."
	zfs list -H | grep iohyve/ISO | cut -f 1 | cut -d '/' -f 4 | sed 1d
}


# Fetch ISO
__iso_fetch() {
	local url="$2"
	if [ -z "$url" ]; then
		printf "missing argument\nusage:\n"
		printf "\tfetchiso <URL>\n"
		exit 1
	fi
	local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
	local name="$(basename $2)"
	echo "Fetching $url..."
	zfs create $pool/iohyve/ISO/$name
	fetch $url -o /iohyve/ISO/$name
}

# Copy ISO from local machine
__iso_copy() {
	local loc="$2"
	if [ -z "$loc" ]; then
		printf "missing argument\nusage:\n"
		printf "\tcpiso <path>\n"
		exit 1
	fi
	local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
	local name="$(basename $loc)"
	echo "Copying $name from $loc..."
	zfs create $pool/iohyve/ISO/$name
	cp $loc /iohyve/ISO/$name
}

# Rename an ISO
__iso_rename() {
	local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
	local iso="$2"
	local name="$3"
	if [ -z "$name" ]; then
		printf "missing argument\nusage:\n"
		printf "\trenameiso <ISO> <newname>\n"
		exit 1
	fi
	echo "Renaming ISO $2 to $3..."
	mv /iohyve/ISO/$iso/$iso /iohyve/ISO/$iso/$name
	zfs rename $pool/iohyve/ISO/$iso $pool/iohyve/ISO/$name
}

# Delete ISO
__iso_delete() {
	local name="$2"
	if [ -z "$name" ]; then
		printf "missing argument\nusage:\n"
		printf "\trmiso <name>\n"
		exit 1
	fi
	local pool="$(zfs list -H | grep iohyve | cut -d '/' -f 1 | head -n1)"
	echo "Deleting $name..."
	zfs destroy -rR $pool/iohyve/ISO/$name
}
