#!/bin/sh
#
# destroy an existing jail

JAIL_NAME=$1
JAIL_PATH=$2
PREFIX=/usr/local
JLS_NAME="/usr/sbin/jls name"
JLS_PATH="/usr/sbin/jls path"
PLATFORM=${PREFIX}/bastille
FSTAB_PATH=${PLATFORM}/fstab/$1.fstab
JAIL_PATH=${PLATFORM}/jails/$1

if [ $# -lt 2 ]; then
    printf "Required: name, path.\n"
    exit 1
fi

if [ ! -d ${JAIL_PATH} ]; then
    printf "Path (${JAIL_PATH}) not found.\n"
    exit 1
fi

if [ $(${JLS_NAME} | grep ${JAIL_NAME}) ]; then
    printf "Jail is running.\n"
    printf "Stop jail first with bbsd-stop ${JAIL_NAME}.\n"
    exit 1
fi

if [ $(${JLS_PATH} | grep ${JAIL_PATH}) ]; then
    printf "Jail is running.\n"
    printf "Stop jail first with bbsd-stop ${JAIL_NAME}.\n"
    exit 1
fi

if [ -d ${JAIL_PATH} ]; then
    zfs destroy -r zroot${JAIL_PATH} || printf "Unable to destroy zroot${JAIL_PATH}."
    rm -rf ${JAIL_PATH} || printf "Unable to delete ${JAIL_PATH}."
fi
printf "Jail destroyed. RIP.\n"
