#!/bin/sh -e

##########################################################################
#   Script description:
#       Make sure ports branch matches pkg repo
#       
#   History:
#   Date        Name        Modification
#   2020-04-16  J Bacon     Begin
##########################################################################

usage()
{
    printf "Usage: $0\n"
    exit 1
}


##########################################################################
#   Function description:
#       Replace the ports tree and wip if present
#       
#   History:
#   Date        Name        Modification
#   2022-03-20  Charlie &   Begin
##########################################################################

replace_ports()
{
    # In case CWD is in $PORTSDIR
    local save_cwd=$(pwd)
    if [ -e $PORTSDIR/wip/.svn ]; then
	local have_wip=1
	local wip_vcs=svn
    elif [ -e $PORTSDIR/wip/.git ]; then
	local have_wip=1
	local wip_vcs=git
    fi
    cd `dirname $PORTSDIR`  # Parent of PORTSDIR
    find $PORTSDIR -mindepth 0 -maxdepth 0 -exec rm -rf '{}' \;
    auto-ports-checkout
    if [ -n "$have_wip" ]; then
	auto-freebsd-wip-checkout
    fi
    cd $save_cwd
}


##########################################################################
#   Main
##########################################################################

if [ $# != 0 ]; then
    usage
fi

case $(auto-ostype) in
FreeBSD)
    : ${PORTSDIR:=/usr/ports}
    export PORTSDIR

    if ! which git > /dev/null 2>&1; then
	printf "$0: Installing git...\n" >> /dev/stderr
	pkg install -y git > /dev/null 2>&1
    fi
    
    repo_branch=$(auto-pkg-branch)
    # Debug: 
    # repo_branch=2021Q1
    if [ ! -e $PORTSDIR/Makefile ]; then
	printf "No ports tree at $PORTSDIR.  Checking one out now...\n"
	auto-ports-checkout
    elif [ ! -e $PORTSDIR/.git ]; then
	cat << EOM

All ports checkout methods other than Git are deprecated and your ports tree
is not a Git clone.  Replacing your old ports tree with a Git shallow clone.

Before proceeding, make sure you have no uncommitted changes in
$PORTSDIR or $PORTSDIR/wip.

EOM
	printf "OK to replace $PORTSDIR? y/[n] "
	read replace_ok
	if [ 0$replace_ok = 0y ]; then
	    # Make sure we don't remove ports while CWD is in it, or
	    # subsequent processes will fail due to nonexistent '.'
	    replace_ports
	fi
    elif (cd $PORTSDIR && git remote -v | fgrep 'fetch' | fgrep 'cgit-beta.FreeBSD.org'); then
	cat << EOM

Your $PORTSDIR is from the beta Git repository.  Would you like to try to
replace it with a new tree from the permanent Git repository?

Before proceeding, make sure you have no uncommitted changes in
$PORTSDIR or $PORTSDIR/wip.

EOM
	printf "OK to replace $PORTSDIR? y/[n] "
	read replace_ok
	if [ 0$replace_ok = 0y ]; then
	    replace_ports
	else
	    printf "OK, keeping $PORTSDIR for now.\n"
	fi
    else
	ports_branch=$(auto-ports-branch)
	# Make sure new branch is available in ports repo
	new_branch_hash="$(git ls-remote --heads https://git.FreeBSD.org/ports.git $repo_branch)"
	if [ $repo_branch == latest -o 0"$new_branch_hash" != 0'' ] \
	    && [ $ports_branch != $repo_branch ]; then
	    cat << EOM

Binary packages are currently from the $repo_branch branch.

$PORTSDIR is currently using the $ports_branch branch.

Software installed via $PORTSDIR will likely have version mismatches
with the $repo_branch binary packages.  To avoid this, you should replace
$PORTSDIR with the $repo_branch ports branch.

Before replacing it, make sure you have no uncommitted changes in
$PORTSDIR or $PORTSDIR/wip.

Before replacing a quarterly branch with a newer quarterly branch, make sure
that the package repository has actually been updated to the new quarter.
This switch could be delayed for non-mainstream platforms such as powerpc*
and arm*.

EOM
	    printf "Replace $PORTSDIR with $repo_branch? (y/n) [y] "
	    read replace_ok
	    if [ 0$replace_ok != 0n ]; then
		replace_ports
	    fi
	fi
    fi
    ;;

*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
