#!/usr/local/bin/cbsd
#v10.1.2
MYARG=""
MYOPTARG="dst rev"
MYDESC="Update FreeBSD ports tree in /usr/ports"
ADDHELP="\
 dst= alternative path instead of /usr/ports\n\
 rev=XXX where XXX - scm revision (req. for turn off --depth=1 via config\n\
"

. ${subrdir}/nc.subr
readconf portsup.conf
. ${cbsdinit}

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 "${N1_COLOR}No svn in the base. Please install devel/subversion${N0_COLOR}"
	fi

	if [ -z "${scmbase}" ]; then
		SCM_URL="${SVNBASE}"
	else
		SCM_URL="${scmbase}"
	fi

	return 0
}

init_git()
{
	SCM=

	if [ -f "/usr/local/bin/git" ]; then
		SCM="/usr/local/bin/git"
	else
		err 1 "${N1_COLOR}no git in the base. please install: ${N2_COLOR}pkg install -y devel/git${N0_COLOR}"
	fi

	if [ -z "${scmbase}" ]; then
		[ -z "${GITBASE}" ] && err 1 "${N1_COLOR}empty GITBASE variable. Please specify GITBASE via ${N2_COLOR}srcup.conf${N0_COLOR}"
		scmbase="${GITBASE}"
	fi
	SCM_URL="${scmbase}"

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

	local CBSDPATH="${PATH}"
	# reset CBSD PATH
	# it is necessary that the calling of any commands via git
	# does not conflict with the same CBSD commands
	export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"

	set -o xtrace
	${SCM} clone -b ${SCM_GIT_BRANCH} ${git_flags} "${GITBASE}" ${dst}
	set +o xtrace

	# restore CBSD PATH
	export PATH="${CBSDPATH}"

	return 0
}

svn_checkout()
{
	local _srcdir

	_srcdir=$( ${REALPATH_CMD} ${dst} )

	# repair and upgrade
	if [ -d "${_srcdir}/.svn" ]; then
		cd ${_srcdir}
		${ECHO} "${N1_COLOR}Processing svn cleanup, please wait...${N0_COLOR}"
		nice -n 19 ${IDLE_IONICE} ${SCM} cleanup
	fi
	${ECHO} "${N1_COLOR}Processing svn update...${N0_COLOR}"
	[ -z "${rev}" ] && rev="head"
	nice -n 19 ${IDLE_IONICE} ${SCM} checkout -r ${rev} ${SCM_URL} ${_srcdir}
}

git_checkout()
{
	local _depth _remotes _origin
	local _ret=0 _p2 _branch

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

	[ -z "${SCM_GIT_BRANCH}" ] && err 1 "${N1_COLOR}empty SCM_GIT_BRANCH${N0_COLOR}"

	${ECHO} "${N1_COLOR}selected branch: ${N2_COLOR}${SCM_GIT_BRANCH}${N0_COLOR}"

	if [ -d "${dst}/.git" ]; then
		echo "cd ${dst} && ${SCM} ${git_flags} branch ${SCM_GIT_BRANCH}"
		cbsdlogger NOTICE ${CBSD_APP}: processing git checkout: ${SCM_GIT_BRANCH} in ${dst}

		local CBSDPATH="${PATH}"
		# reset CBSD PATH
		# it is necessary that the calling of any commands via git
		# does not conflict with the same CBSD commands
		export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"

		set -o xtrace
		cd ${dst}
		nice -n 19 ${IDLE_IONICE} ${SCM} reset --hard				# reset to source default

		_remotes="remotes/"
		_origin="origin/"

		# remove 'remotes/' + 'origin/'
		_p2=${SCM_GIT_BRANCH##*${_remotes}}
		_branch=${_p2##*${_origin}}

		[ -z "${_branch}" ] && err 1 "${N1_COLOR}${CBSD_APP} error: empty branch var SCM_GIT_BRANCH${N0_COLOR}"
		${ECHO} "${N1_COLOR}${CBSD_APP} swith to branch: ${N2_COLOR}${_branch}${N0_COLOR}"

		nice -n 19 ${IDLE_IONICE} ${SCM} pull ${git_pull_flags}
		_ret=$?

		if [ -n "${rev}" ]; then
			nice -n 19 ${IDLE_IONICE} ${SCM} checkout ${rev}
			_ret=$?
		else
			nice -n 19 ${IDLE_IONICE} ${SCM} checkout ${_branch}
			_ret=$?
		fi
		set +o xtrace
		# restore CBSD PATH
		export PATH="${CBSDPATH}"
	else
		${ECHO} "${N1_COLOR}${CBSD_APP}: clone error: no ${N2_COLOR}.git${N0_COLOR}"
		_ret=1
	fi

	return ${_ret}
}


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

set +e
. ${subrdir}/build.subr
set -e

[ -z "${dst}" ] && dst="/usr/ports"

LOCKFILE=${ftmpdir}/ports_$( ${miscdir}/cbsd_md5 ${dst} ).lock
makelock ${LOCKFILE}

. ${subrdir}/time.subr
st_time=$( ${DATE_CMD} +%s )

if [ "${platform}" = "DragonFly" ]; then
	if [ ! -d "/usr/dports/.git" ]; then
		${MAKE_CMD} -C /usr dports-create
		res=$?
	else
		${MAKE_CMD} -C /usr dports-update
		res=$?
	fi

	end_time=$( ${DATE_CMD} +%s )
	diff_time=$(( end_time - st_time ))
	diff_time=$( displaytime ${diff_time} )
	${ECHO} "${N1_COLOR}${CBSD_APP} done ${N2_COLOR}in ${diff_time}${N0_COLOR}"

	exit $?
fi

[ ! -d "${dst}" ] && ${MKDIR_CMD} -p ${dst}

case "${checkout_method}" in
	svn*)
		init_svn
		svn_checkout
		;;
	git*)
		init_git
		git_checkout
		;;
	*)
		err 1 "${ECHO}Unknown checkout method. Please specify it via: ${N2_COLOR}portsup.conf${N0_COLOR}"
esac

end_time=$( ${DATE_CMD} +%s )
diff_time=$(( end_time - st_time ))
diff_time=$( displaytime ${diff_time} )
${ECHO} "${N1_COLOR}${CBSD_APP} done ${N2_COLOR}in ${diff_time}${N0_COLOR}"
