#!/usr/local/bin/cbsd
#v10.1.2
MYARG=""
MYOPTARG="dst rev checkout_method"
MYDESC="Update FreeBSD ports tree in /usr/ports"
ADDHELP="

${H3_COLOR}Description${N0_COLOR}:

Checkout and/or update FreeBSD ports tree ( /usr/ports dir )

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}checkout_method=${N0_COLOR}  - <method, alternative checkout method. Default is: 'gitup git';
 ${N2_COLOR}dst=${N0_COLOR}              - <path>, alternative path, default: /usr/ports;
 ${N2_COLOR}rev=${N0_COLOR}              - <revision>, alternative checkout commit, default: 'head';

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd portsup checkout_method=gitup

${H3_COLOR}See also${N0_COLOR}:

  cbsd srcup --help

"

. ${subrdir}/nc.subr
checkout_method=
ocheckout_method=
. ${cbsdinit}

[ -n "${checkout_method}" ] && ocheckout_method="${checkout_method}"
readconf portsup.conf
[ -n "${ocheckout_method}" ] && checkout_method="${ocheckout_method}"

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
		${ECHO} "${N1_COLOR}No svn in the base. Please install devel/subversion${N0_COLOR}"
		return 1
	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
		${ECHO} "${N1_COLOR}no git in the base. please install: ${N2_COLOR}pkg install -y devel/git${N0_COLOR}"
		return 1
	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
}

init_gitup()
{
	SCM=

	if [ -f "/usr/local/bin/gitup" ]; then
		SCM="/usr/local/bin/gitup"
	else
		${ECHO} "${N1_COLOR}no gitup in the base. please install: ${N2_COLOR}pkg install -y net/gitup${N0_COLOR}"
		return 1
	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}"

	if [ -d "${dst}/.git" ]; then
		${ECHO} "A .git directory was found -- gitup does not update this directory which will cause problems for the official Git client."
		${ECHO} "If you wish to use gitup, please remove /usr/ports/.git and rerun gitup"
		return 1
	fi

	return 0
}

checkout_svn()
{
	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}
}

checkout_git()
{
	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 ${CIX_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}${CIX_APP} error: empty branch var SCM_GIT_BRANCH${N0_COLOR}"
		${ECHO} "${N1_COLOR}${CIX_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}${CIX_APP}: clone error: no ${N2_COLOR}.git${N0_COLOR}"
		_ret=1
	fi

	return ${_ret}
}

checkout_gitup()
{
	local _ret=
	${ECHO} "${N1_COLOR}selected branch: ${N2_COLOR}${SCM_GIT_BRANCH}${N0_COLOR}"
	nice -n 19 ${IDLE_IONICE} ${SCM} ports
	_ret=$?

	return ${_ret}
}

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

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

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

. ${subrdir}/time.subr
gettime st_time

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

	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 $?
fi


[ -z "${checkout_method}" ] && err 1 "${N1_COLOR}empty checkout method (valid: git,gitup,svn). Please specify it via: ${N2_COLOR}portsup.conf${N0_COLOR}"

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

for cur_checkout_method in ${checkout_method}; do
	${ECHO} "${N1_COLOR}Using method: ${N2_COLOR}${cur_checkout_method}${N0_COLOR}"
	case "${cur_checkout_method}" in
		git|gitup|svn)
			init_${cur_checkout_method}
			_ret=$?
			[ ${_ret} -ne 0 ] && continue
			checkout_${cur_checkout_method}
			;;
		*)
			err 1 "${N1_COLOR}unknown checkout method (${cur_checkout_method}, valid: git,gitup,svn). Please specify it via: ${N2_COLOR}portsup.conf${N0_COLOR}"
			;;
	esac
done

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
