#!/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
}


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

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

case $(auto-ostype) in
FreeBSD)
    : ${PORTSDIR:=/usr/ports}
    export PORTSDIR
    
    repo_branch=$(auto-pkg-branch)
    # Debug: 
    # repo_branch=2021Q1
    if [ -e $PORTSDIR/wip/.svn ]; then
	have_wip=1
	wip_vcs=svn
    elif [ -e $PORTSDIR/wip/.git ]; then
	have_wip=1
	wip_vcs=git
    fi
    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 '.'
	    save_cwd=$(pwd)
	    cd /usr
	    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
	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
	    find $PORTSDIR -mindepth 0 -maxdepth 0 -exec rm -rf '{}' \;
	    auto-ports-checkout
	    if [ -n "$have_wip" ]; then
		auto-freebsd-wip-checkout
	    fi
	else
	    printf "OK, keeping $PORTSDIR for now.\n"
	fi
    else
	ports_branch=$(auto-ports-branch)
	if [ $ports_branch != $repo_branch ]; then
	    cat << EOM

Your binary packages are currently from the $repo_branch branch (or they
will be shortly when the new bulk builds complete).

$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
		# FIXME: Look into changing branches with git
		find $PORTSDIR -mindepth 0 -maxdepth 0 -exec rm -rf '{}' \;
		auto-ports-checkout
		if [ -n "$have_wip" ]; then
		    auto-freebsd-wip-checkout
		fi
	    fi
	fi
    fi
    ;;

*)
    printf "$0: Not supported on $(auto-ostype).\n"
    exit 1
    ;;

esac
