#!/usr/local/bin/cbsd
#v11.1.16
MYARG="arch ver"
MYOPTARG="platform target_arch source basename baseupdate"
MYDESC="Register bases for jails in the databases"
CBSDMODULE="build"
ADDHELP="auto_baseupdate - overwrite \*-bases.conf settings, 1 - update base\n"

. ${subrdir}/nc.subr

source=
basename=
auto_baseupdate=
oauto_baseupdate=
. ${cbsdinit}

[ -z "${source}" ] && source="unknown"
[ -n "${auto_baseupdate}" ] && oauto_baseupdate="${baseupdate}"

. ${tools}
. ${strings}
. ${system}

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

. ${subrdir}/universe.subr

strpos --str="${ver}" --search="."
# auto-detect for stable/release
pos=$?
if [ ${pos} -eq 0 ]; then
	stable=1
	ostable=1
else
	stable=0
	ostable=0
fi

init_basedir

[ ! -x "${BASE_DIR}/bin/sh" ] && err 1 "${N1_COLOR}register_base: no such bases here: ${N2_COLOR}${BASE_DIR}${N0_COLOR}"

baseelf=$( ${miscdir}/elf_tables --ver ${BASE_DIR}/bin/sh 2>/dev/null )

if [ -n "${basename}" ]; then
	_basename_args="-b ${basename}"
else
	_basename_args=
fi

idx=$( get_baseidx -a ${arch} -p ${platform} -s ${stable} -t ${target_arch} -v ${ver} ${_basename_args} )

if [ -n "${idx}" ]; then
	cbsdsqlrw local "UPDATE bsdbase SET elf=\"${baseelf}\",date=datetime('now','localtime'),source=\"${source}\" WHERE idx=\"${idx}\""
	dt="$( cbsdsqlro local SELECT date FROM bsdbase WHERE elf=\"${baseelf}\" AND idx=\"${idx}\" )"
	date=$( ${DATE_CMD} -j -f "%Y-%m-%d %H:%M:%S" "${dt}" "+%s" )
	${ECHO} "${N1_COLOR}Bases updated: ${N2_COLOR}${BASE_DIR}${N0_COLOR}"
else
	if [ -n "${basename}" ]; then
		cbsdsqlrw local "INSERT INTO bsdbase ( platform,name,arch,targetarch,ver,stable,elf,source,basename ) VALUES ( \"${platform}\", \"base\", \"${arch}\", \"${target_arch}\", \"${ver}\", \"${stable}\", \"${baseelf}\", \"${source}\", \"${basename}\" )"
	else
		cbsdsqlrw local "INSERT INTO bsdbase ( platform,name,arch,targetarch,ver,stable,elf,source ) VALUES ( \"${platform}\", \"base\", \"${arch}\", \"${target_arch}\", \"${ver}\", \"${stable}\", \"${baseelf}\", \"${source}\" )"
	fi
	${ECHO} "${N1_COLOR}Bases registered: ${N2_COLOR}${BASE_DIR}${N0_COLOR}"
fi

# not for DflyBSD platform
if [ "${platform}" != "DragonFly" ]; then
	# auto baseupdate? for stable=0 only
	if [ "${stable}" = "0" ]; then
		readconf ${platform}-bases.conf
		[ -n "${oauto_baseupdate}" ] && auto_baseupdate="${oauto_baseupdate}"
		if [ "${auto_baseupdate}" = "1" ]; then
			${ECHO} "${N1_COLOR}register_base: ${H5_COLOR}auto_baseupdate=1${N1_COLOR} via ${platform}-bases.conf, trying to perform baseupdate, please wait...${N0_COLOR}"
			if [ -n "${basename}" ]; then
				baseupdate arch=${arch} ver=${ver} target_arch=${TARGET_ARCH} basename="${basename}"
			else
				baseupdate arch=${arch} ver=${ver} target_arch=${TARGET_ARCH}
			fi
		else
			[ -z "${oauto_baseupdate}" ] && ${ECHO} "${N1_COLOR}register_base: ${H5_COLOR}auto_baseupdate=0${N1_COLOR} via ${platform}-bases.conf, updates disabled${N0_COLOR}"
			[ -z "${oauto_baseupdate}" ] && ${ECHO} "${N1_COLOR}register_base: you might want to do ${N2_COLOR}cbsd baseupdate${N1_COLOR} by hand to fetch latest patches${N0_COLOR}"
		fi
	fi
fi

exit 0
