#!/usr/local/bin/cbsd
#v12.1.8
MYARG=""
MYOPTARG="basename ver"
MYDESC="Remove src-dir"
ADDHELP="

${H3_COLOR}Description${N0_COLOR}:

Remove and unregister OS source tree directory which are physically located in the 
~cbsd/src/ directory.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}basename=${N0_COLOR} - alternative name of basejail;
 ${N2_COLOR}ver=${N0_COLOR}      - e.g.: '15.2' or 'ver=15' for stable;

${H3_COLOR}Examples${N0_COLOR}:

  # cbsd removesrc
  # cbsd removesrc ver=15       # for FreeBSD 15-RELENG (STABLE)

${H3_COLOR}See also${N0_COLOR}:

 cbsd sources --help
 cbsd srcup --help
 cbsd bases --help
 cbsd buildworld --help
 ls -la ~cbsd/src/

"

. ${subrdir}/nc.subr
. ${cbsdinit}

if [ "${mod_cbsd_queue_enabled}" = "YES" -a -z "${MOD_CBSD_QUEUE_DISABLED}" ]; then
	readconf cbsd_queue.conf
	[ -z "${cbsd_queue_backend}" ] && MOD_CBSD_QUEUE_DISABLED="1"
fi

over="${ver}"
oarch="${arch}"

. ${subrdir}/time.subr
gettime st_time

. ${subrdir}/build.subr
readconf buildworld.conf
. ${subrdir}/universe.subr

init_distcc
init_notify
init_target_arch
init_srcdir
init_supported_arch
init_make_flags

LOCKFILE=${ftmpdir}/src_$( ${miscdir}/cbsd_md5 ${SRC_DIR} ).lock
makelock ${LOCKFILE}

SCM_REV="any"

# CBSD QUEUE
if [ "${mod_cbsd_queue_enabled}" = "YES" -a -z "${MOD_CBSD_QUEUE_DISABLED}" ]; then
	[ -n "${cbsd_removesrc_queue_name}" ] && ${cbsd_queue_backend} cbsd_queue_name=${cbsd_removesrc_queue_name} cmd=removesrc id=src${ver} status=1
fi

[ -d "${SRC_DIR}" ] && ${RM_CMD} -rf ${SRC_DIR}
if [ -d "${srcdir}/src_${ver}" ]; then
	# may contain etcupdate files
	${RMDIR_CMD} ${srcdir}/src_${ver} > /dev/null 2>&1 || true
fi

unregister_src

# CBSD QUEUE
if [ "${mod_cbsd_queue_enabled}" = "YES" -a -z "${MOD_CBSD_QUEUE_DISABLED}" ]; then
	[ -n "${cbsd_removesrc_queue_name}" ] && ${cbsd_queue_backend} cbsd_queue_name=${cbsd_removesrc_queue_name} cmd=removesrc id=src${ver} status=2
fi

gettime end_time
diff_time=$(( end_time - st_time ))
diff_time=$( displaytime ${diff_time} )
${ECHO} "${N1_COLOR}${CIX_APP} done ${N2_COLOR}in ${diff_time}${N0_COLOR}"

exit 0
