#!/usr/local/bin/cbsd
#v11.0.6
globalconf="${workdir}/cbsd.conf";
MYARG=""
MYOPTARG="ver stable rev scmbase dst patch"
MYDESC="Update FreeBSD source tree in $srcdir"
ADDHELP="ver=head for current.\n\
stable=1 for STABLE_X\n\
rev=XXX where XXX - svn revision\n\
scmbase - alternative svn base URL\n\
dst - alternative dst dir\n\
patchset=apply patchet\n"

set -e
. ${globalconf}
set +e

. ${subr}
readconf srcup.conf
init $*

[ -z "${cbsd_queue_name}" ] && cbsd_queue_name="/clonos/sources/"
over="${ver}"
oarch="${arch}"
ostable="${stable}"

init_svn()
{
	SCM=""

	if [ -f "/usr/bin/svnlite" ]; then
		SCM="/usr/bin/svnlite"
	elif [ -f "/usr/local/bin/svn" ]; then
		SCM="/usr/local/bin/svn"
	else
		err 1 "${MAGENTA}No svn in the base. Please install devel/subversion${NORMAL}"
	fi

	[ -z "${scmbase}" ] && scmbase="${SVNBASE}"

	if [ "${stable}" = "1" ]; then
		SCM_URL="${scmbase}/stable/${ver}"
	else
		SCM_URL="${scmbase}/releng/${ver}"
	fi

	[ "${ver}" = "12.0" -o "${ver}" = "12" ] && SCM_URL="${scmbase}/head"
	return 0
}

init_git()
{
	SCM=""

	if [ -f "/usr/local/bin/git" ]; then
		SCM="/usr/local/bin/git"
	else
		err 1 "${MAGENTA}No git in the base. Please install devel/git${NORMAL}"
	fi

	[ -z "${scmbase}" ] && scmbase="${GITBASE}"
	SCM_URL="${scmbase}"

	[ -d "${dst}/.git" ] && return 0

	cd ${dst} && ${SCM} init
	${SCM} remote add origin ${SCM_URL}
	echo "${SCM} fetch --depth=1 origin"
	${SCM} fetch ${git_flags} origin

	return 0
}

svn_checkout()
{
	# repair and upgrade
	if [ -d "${dst}/.svn" ]; then
		cd ${dst}
		${ECHO} "${MAGENTA}Processing svn cleanup, please wait...${NORMAL}"
		${SCM} cleanup
	fi
	${ECHO} "${MAGENTA}Processing svn update...${NORMAL}"
	echo "${SCM} checkout -r ${rev} ${SCM_URL} ${dst}"
	${SCM} checkout -r ${rev} ${SCM_URL} ${dst}
}

git_checkout()
{
	local _depth _branch

	[ "${depth}" != "0" ] && _depth="${depth}"

	[ ! -f ${sharedir}/${platform}-git_branches.conf ] && err 1 "${MAGENTA}No ${sharedir}/${platform}-git_branches.conf for git branch map${NORMAL}"
	. ${sharedir}/${platform}-git_branches.conf

	if [ -z "${GITBRANCH}" ]; then
		_branch=$( init_git_branch )
	else
		_branch="${GITBRANCH}"
	fi

	[ -z "${_branch}" ] && err 1 "${MAGENTA}Empty branch from init_git_branch${NORMAL}"

	${ECHO} "${MAGENTA}Selected branch: ${GREEN}${_branch}${NORMAL}"

	if [ -d "${dst}/.git" ]; then
		echo "cd ${dst} && ${SCM} ${git_flags} branch ${_branch}"
		cd ${dst} && ${SCM} checkout ${_branch}
		${SCM} pull
	else
		echo "Clone error: no .git"
	fi
}

status_is_maintenance_soft()
{
	cbsdsql local "UPDATE bsdsrc SET status='1' WHERE ver=\"${ver}\""
}

status_is_maintenance_hard()
{
	cbsdsql local "UPDATE bsdsrc SET status='2' WHERE ver=\"${ver}\""

}

status_is_normal()
{
	cbsdsql local "UPDATE bsdsrc SET status='0' WHERE ver=\"${ver}\""
}


### MAIN ###
. ${buildconf}
readconf buildworld.conf
. ${workdir}/universe.subr

init_distcc
init_notify
init_target_arch
init_srcdir
init_supported_arch
init_make_flags

set +e
. ${buildconf}
set -e

if [ -n "${dst}" ]; then
	dst="${srcdir}/src_${ver}/src"
else
	dst="${SRC_DIR}"
fi

cleanup_sources()
{
	[ -f ${LOCKFILE} ] && /bin/rm -f ${LOCKFILE}

	if [ ! -f "${dst}/Makefile" ]; then
		/bin/rm -rf ${dst}
		unregister_src
		err 1 "No source code for ${ver}"
	fi

	# restore operability status
	status_is_normal
}

LOCKFILE=${ftmpdir}/src_$( /sbin/md5 -qs ${dst} ).lock
makelock ${LOCKFILE}

[ ! -d "${dst}" ] && /bin/mkdir -p ${dst}

trap "cleanup_sources" HUP INT ABRT BUS TERM EXIT

case "${checkout_method}" in
	svn*)
		init_svn

		# conditional registration:
		init_scm_and_version
		register_src

		# 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=srcup id=src${ver} platform=${platform} ver=${ver} rev="-" date="-" stable="${stable}" status=1 ||true
		fi

		if [ ! -f "${dst}/Makefile" ]; then
			status_is_maintenance_hard
		else
			status_is_maintenance_soft
		fi

		svn_checkout
		init_scm_and_version
		;;
	git*)

		init_git

		# conditional registration:
		init_scm_and_version
		register_src

		if [ ! -f "${dst}/Makefile" ]; then
			status_is_maintenance_hard
		else
			status_is_maintenance_soft
		fi

		git_checkout
		init_scm_and_version
		;;
	*)
		err 1 "${ECHO}Unknown checkout method. Please specify it via: ${GREEN}srcup.conf${NORMAL}"
esac

# todo: apply_cbsd_patch should be x11, not sign for global patch
[ "${apply_cbsd_patch}" = "1" -o "${apply_cbsd_9p_patch}" = "1" ] && /usr/local/bin/cbsd srcpatch $*
register_src

# 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=update id=src${ver} platform=${platform} ver=${ver} rev="${SCM_REV}" date="${date}" stable="${stable}" status=1 ||true
	[ "${cbsd_queue_name}" != "none" ] && cbsd_queue_sys cbsd_queue_name=${cbsd_queue_name} cmd=srcup id=src${ver} status=2 || true
fi

status_is_normal

