#!/bin/sh

# This is for remote host initialization
# example: iocell init-host IP-ADDRESS
# will set up the host for iocell

__init_host () {
    local _fs _fs_list _host _remote_pool

    _host="$1"
    _pool="$2"
    _fs_list="download
              jails
              releases
              templates"

    if [ -z $_host ] || [ -z $_pool ] ; then
        echo "ERROR: missing host or pool name" >&2
        exit 1
    fi

    _remote_pool="$(ssh root@$_host zpool get -H -o value name $_pool)"

    if [ -z "$_remote_pool" ] ; then
        echo "ERROR: no remote pool $_pool found" >&2
        exit 1
    fi

    ssh root@$_host "zpool set comment=iocell $_remote_pool"
    ssh root@$_host "zfs create ${_remote_pool}/iocell
                     && zfs set mountpoint=/iocell ${_remote_pool}/iocell
                     && mkdir ${iocroot}/log"

    for _fs in $(echo $_fs_list) ; do
        zfs snapshot -r ${pool}/iocell/${_fs}@init
        zfs send -vR ${pool}/iocell/${_fs}@init | \
            ssh root@$_host "zfs recv ${_remote_pool}/iocell/${_fs}"
        zfs destroy -r ${pool}/iocell/${_fs}@init
    done
}
