#!/bin/bash
# -*- mode: sh; fill-column: 75; tab-width: 8; coding: utf-8-unix -*-
# from https://github.com/earlruby/create-vm/

[ -f /usr/local/bin/usr_local_tput.bash ] && \
    . /usr/local/bin/usr_local_tput.bash || {
	DBUG() { echo DEBUG $* ; }
	INFO() { echo INFO $* ; }
	WARN() { echo WARN $* ; }
	ERROR() { echo ERROR $* ; }
    }

prog=`basename $0 .bash`
PREFIX=/usr/local
ROLE=toxcore
BOX=gentoo

export BASE_SRC_ANSIBLE=/o/var/local/src/play_tox
yamllint -c $BASE_SRC_ANSIBLE/.yamllint.rc $BASE_SRC_ANSIBLE/hosts.yml|| {
    ERROR
    exit 1
}

# put these values in $BASE_SRC_ANSIBLE/hosts.yml
[ -n "$BOX_NBD_OVERLAY_NAME" ] || \
    BOX_NBD_OVERLAY_NAME=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_OVERLAY_NAME $BOX)

if virsh list | grep  "$BOX_NBD_OVERLAY_NAME" ; then
    ERROR "$BOX_NBD_OVERLAY_NAME" is running - please virsh destroy "$BOX_NBD_OVERLAY_NAME" 
    exit 2
fi
if virsh list --all | grep  "$BOX_NBD_OVERLAY_NAME" ; then
    ERROR "$BOX_NBD_OVERLAY_NAME" is running - please virsh undefine "$BOX_NBD_OVERLAY_NAME" 
    exit 3
fi
    

[ -n "$BOX_NBD_BASE_PUBKEY" ] || \
  BOX_NBD_BASE_PUBKEY=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_BASE_PUBKEY $BOX)
[ -n "$BOX_NBD_OVERLAY_BASE" ] || \
  BOX_NBD_OVERLAY_BASE=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_OVERLAY_BASE $BOX)
[ -n "$BOX_NBD_OVERLAY_GB" ] || \
  BOX_NBD_OVERLAY_GB=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_OVERLAY_GB $BOX)
[ -n "$BOX_NBD_OVERLAY_CPUS" ] || \
  BOX_NBD_OVERLAY_CPUS=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_OVERLAY_CPUS $BOX)
[ -n "$BOX_NBD_OVERLAY_RAM" ] || \
  BOX_NBD_OVERLAY_RAM=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_OVERLAY_RAM $BOX)
[ -n "$BOX_NBD_OVERLAY_DIR" ] || \
  BOX_NBD_OVERLAY_DIR=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_OVERLAY_DIR $BOX)
[ -n "$BOX_NBD_OVERLAY_BR" ] || \
  BOX_NBD_OVERLAY_BR=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_OVERLAY_BR $BOX)
#[ -n "$BOX_NBD_OVERLAY_NETWORK" ] || \
#  BOX_NBD_OVERLAY_NETWORK=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_OVERLAY_NETWORK $BOX)
[ -n "$BOX_NBD_OVERLAY_PASS" ] || \
  BOX_NBD_OVERLAY_PASS=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_OVERLAY_PASS $BOX)

[ ! -f "$BOX_NBD_OVERLAY_BASE" ] && \
    ERROR BOX_NBD_OVERLAY_BASE=$BOX_NBD_OVERLAY_BASE must exist && exit 3
[ ! -d "$BOX_NBD_OVERLAY_DIR" ] && \
    ERROR BOX_NBD_OVERLAY_DIR=$BOX_NBD_OVERLAY_DIR must exist && exit 5

[ -z "$BOX_NBD_OVERLAY_BR" ] && \
    ERROR BOX_NBD_OVERLAY_BR=$BOX_NBD_OVERLAY_BR must not be null && exit 5
 
[ -n "$BOX_NBD_DEV" ] && grep "$BOX_NBD_DEV" /proc/mounts && WARN "$BOX_NBD_DEV" is mounted
export BOX_NBD_OVERLAY_DIR

# libvirt.libvirtError: Network not found: no network with matching name 'default'a
DBUG bash toxcore_create-vm.bash \
             -n $BOX_NBD_OVERLAY_NAME \
             -k $BOX_NBD_BASE_PUBKEY \
             -i $BOX_NBD_OVERLAY_BASE \
             -s $BOX_NBD_OVERLAY_GB \
             -b $BOX_NBD_OVERLAY_BR \
             -c $BOX_NBD_OVERLAY_CPUS \
             -r $BOX_NBD_OVERLAY_RAM \
             -d $BOX_NBD_OVERLAY_DIR \
             -p $BOX_NBD_OVERLAY_PASS \
             -o gentoo 
bash toxcore_create-vm.bash \
             -n $BOX_NBD_OVERLAY_NAME \
             -k $BOX_NBD_BASE_PUBKEY \
             -i $BOX_NBD_OVERLAY_BASE \
             -s $BOX_NBD_OVERLAY_GB \
             -b $BOX_NBD_OVERLAY_BR \
             -c $BOX_NBD_OVERLAY_CPUS \
             -r $BOX_NBD_OVERLAY_RAM \
             -d $BOX_NBD_OVERLAY_DIR \
             -p $BOX_NBD_OVERLAY_PASS \
             -o gentoo < /dev/null
retval=$?
[ $retval -gt 0 ] && exit 1$retval
[ -f $BOX_NBD_OVERLAY_DIR/images/$BOX_NBD_OVERLAY_NAME.img ] && \
    INFO $BOX_NBD_OVERLAY_DIR/images/$BOX_NBD_OVERLAY_NAME.img || {
    ERROR NO $BOX_NBD_OVERLAY_DIR/images/$BOX_NBD_OVERLAY_NAME.img  ; exit 2$retval ; }

INFO virsh define $BOX_NBD_OVERLAY_DIR/xml/$BOX_NBD_OVERLAY_NAME.xml
virsh define $BOX_NBD_OVERLAY_DIR/xml/$BOX_NBD_OVERLAY_NAME.xml 
sleep 5
INFO virsh net-dhcp-leases default
sudo  virsh net-dhcp-leases default
sudo find /var/lib/libvirt/qemu/channel/target/*${BOX_NBD_OVERLAY_NAME}* \
     -name org.qemu.guest_agent.0
