#!/usr/bin/env cbsd
#v11.1.16
CIXARG="ver"
CIXOPTARG="arch platform target_arch basename"
MYDESC="Unregister bases (without removing from storage) for jails from the CBSD DB"
CBSDMODULE="jail,sys"
ADDHELP="

${H3_COLOR}Description${N0_COLOR}:

Register basedir for jails in CBSD CB

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}basename${N0_COLOR}       - <path> - alternative DESTDIR= path instead of ~cbsd/basejail/;

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd unregister_base ver=empty basename="ca10502801e3b89a4d4f94f3a4dcc0d7" platform='*' arch='*' target_arch='*'

${H3_COLOR}See also${N0_COLOR}:

  cbsd register_base --help
  cbsd bases --help

"

basename=
arch=
xplatform="${platform}"
platform=
ver=
cixinit

[ -n "${platform}" ] && oplatform="${platform}"
[ -z "${oplatform}" ] && oplatform="${xplatform}"
[ -z "${arch}" ] && capture arch ${UNAME_CMD} -m
. ${tools}
. ${strings}
. ${system}

[ -z "${target_arch}" ] && target_arch="${arch}"

. ${subrdir}/universe.subr
[ -n "${oplatform}" ] && platform="${oplatform}"

_get_idx_args="-s *"
[ -n "${arch}" ] && _get_idx_args="${_get_idx_args} -a ${arch}"
[ -n "${target_arch}" ] && _get_idx_args="${_get_idx_args} -t ${target_arch}"
[ -n "${platform}" ] && _get_idx_args="${_get_idx_args} -p ${platform}"
[ -n "${basename}" ] && _get_idx_args="${_get_idx_args} -b ${basename}"
[ -n "${ver}" ] && _get_idx_args="${_get_idx_args} -v ${ver}"

set -f
capture idx get_baseidx ${_get_idx_args}
set +f

for _i in ${idx}; do
	if [ -n "${_i}" ]; then
		cbsdsqlrw local "DELETE FROM bsdbase WHERE idx='${_i}'"
		${ECHO} "${N1_COLOR}Unregistered base${N0_COLOR}"
	else
		${ECHO} "${N1_COLOR}unregister_base: No idx for platform:${platform}, arch:${arch}, target_arch:${target_arch}, ver:${ver}, stable:${stable}, basename:${basename}${N0_COLOR}"
	fi
done

exit 0
