#!/usr/local/bin/cbsd
#v11.1.16
CBSDMODULE="build"
MYARG=""
MYOPTARG="ver arch target_arch target_cputype maxjobs clean basename stable distcc ccache notify"
MYDESC="Buildworld from sources"
ADDHELP="ver=head for current.\n\
stable=1 for RELENG_X\n\
distcc=1 - enable distcc\n\
ccache=1 - enable ccache\n"

. ${subr}
. ${system}
. ${strings}
. ${tools}
init $*

# MAIN
[ -z "${cbsd_queue_name}" ] && cbsd_queue_name="/clonos/bases/"
over="${ver}"
oarch="${arch}"
[ -z "${NICE}" ] && NICE="0"

. ${buildconf}
. ${distccacheconf}
. ${mailconf}
readconf buildworld.conf
. ${workdir}/universe.subr

# for checkout_method
readconf srcup.conf

init_distcc
init_notify
init_target_arch
init_srcdir
init_supported_arch
init_make_flags
init_basedir

if [ "${ccache}" = "1"  ]; then
	init_ccache_dir
	if ! ccache_check; then
		${ECHO} "${MAGENTA}*** ${GREEN}Ccache disabled: no binaries at /usr/local/bin/ccache${MAGENTA} ***${NORMAL}"
		ccache=0
	fi

	${ECHO} "${MAGENTA}*** ${GREEN}Ccache enabled${MAGENTA} ***${NORMAL}"
	export PATH=/usr/local/libexec/ccache:${PATH}
	export CCACHE_PATH=/usr/bin:/usr/local/bin
	export CCACHE_DIR=${ccache_dir_source}
	CCACHE_SIZE="8"
	/usr/local/bin/ccache -M ${CCACHE_SIZE}
else
	ccache=0
fi

/usr/bin/truncate -s0 ${ftmpdir}/make.$$.conf

export __MAKE_CONF=${ftmpdir}/make.$$.conf
prepare_make_conf ${origmakeconf} ${__MAKE_CONF}

init_make_flags

LOCKFILE=${ftmpdir}/$( /sbin/md5 -qs ${MAKEOBJDIRPREFIX} ).lock
[ $notify -eq 1 ] && BLDLOG="${tmpdir}/build.$$.log"
makelock ${LOCKFILE} "rm -f ${__MAKE_CONF} ${BLDLOG}; cleanup_bases"

[ ${distcc} -eq 1 ] && export DISTCC_HOSTS="${CBSD_DISTCCHOST}"

register_base arch=${arch} ver=${ver}

# CBSD QUEUE
if [ -x "${moduledir}/cbsd_queue.d/cbsd_queue" ]; then
	[ "${cbsd_queue_name}" != "none" ] && cbsd_queue_sys cbsd_queue_name=${cbsd_queue_name} cmd=world id=base${ver}-${arch}-${stable} platform=${platform} arch=${arch} targetarch=${targetarch} ver=${ver} stable="${stable}" date="-" elf="-" status=1
fi

if [ -x "${BASE_DIR}/bin/sh" ]; then
	baseelf=$( ${miscdir}/elf_tables --ver ${BASE_DIR}/bin/sh 2>/dev/null )
	[ -z "${baseelf}" ] && baseelf="0"
	register_base arch=${arch} ver=${ver}	# again: update elf
	base_status_is_maintenance_soft
else
	base_status_is_maintenance_hard
fi

${ECHO} "${MAGENTA}buildworld for TARGET:${GREEN}${arch} ${MAGENTA}TARGET_ARCH:${GREEN}${target_arch}${NORMAL}"

st_date=$( /bin/date +%s )
if [ $notify -eq 1 ]; then
	[ -z "$TAILSTRING" ] && TAILSTRING=50
	script ${BLDLOG} /usr/bin/nice -n ${NICE} /usr/bin/make $NUMJOBS -C ${SRC_DIR} buildworld ${NOCLEANUP} TARGET=${arch} TARGET_ARCH="${target_arch}"
	res=$?
else
	/usr/bin/nice -n ${NICE} /usr/bin/make $NUMJOBS -C ${SRC_DIR} buildworld ${NOCLEANUP} TARGET=${arch} TARGET_ARCH="${target_arch}"
	res=$?
fi
end_date=$( /bin/date +%s )

diff_time=$(( end_date - st_date ))

run_time=$(( diff_time / 60 ))

if [ ${notify} -eq 1 ]; then
cat >> ${BLDLOG} << EOF
Start build date: ${st_date}
End build date: ${end_date}
Runtime minutes: ${run_time} min ( ${diff_time} seconds )
EOF
fi

cat <<EOF
Start build date: ${st_date}
End build date: ${end_date}
Runtime minutes: ${run_time} min ( ${diff_time} seconds )
EOF

cleanup_bases

DT_END=$( /bin/date +%s )
init_scm_and_version

if [ $res -ne 0 ]; then
	[ $notify -eq 1 ] && send_notification -s "[CBSD ${nodename}] buildworld ${basename} ${ver} ${arch} ${target_arch} r${svnrev} failed." -b "`tail -n${TAILSTRING} ${BLDLOG}`"
	exit 1
fi

if [ $res -eq 0 -a $notify -eq 1 ]; then
	send_notification -s "[CBSD ${nodename}] buildworld ${basename} ${ver} ${arch} ${target_arch} r${svnrev} complete." -f ${BLDLOG}
	return 0
fi

# CBSD QUEUE
if [ -x "${moduledir}/cbsd_queue.d/cbsd_queue" ]; then

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

	[ "${cbsd_queue_name}" != "none" ] && cbsd_queue_sys cbsd_queue_name=${cbsd_queue_name} cmd=update id=base${ver}-${arch}-${stable} platform=${platform} arch=${arch} targetarch=${targetarch} ver=${ver} stable="${stable}" date="${DT_END}" elf="${baseelf}" status=1

	sleep 2
	[ "${cbsd_queue_name}" != "none" ] && cbsd_queue_sys cbsd_queue_name=${cbsd_queue_name} cmd=world id=base${ver}-${arch}-${stable} status=2
fi
