#!/bin/sh -e

##########################################################################
#   Script description:
#       Check out a free ports tree
#       
#   History:
#   Date        Name        Modification
#   2017-02-15  Jason Bacon Begin
##########################################################################

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


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

: ${PORTSDIR:=/usr/ports}
export PORTSDIR

case $(auto-ostype) in
FreeBSD)
    if [ -e $PORTSDIR/CHANGES ]; then
	printf "Ports tree already installed in $PORTSDIR.\n"
	exit 1
    fi

    repo_branch=$(auto-pkg-branch)
    # Debug:
    # repo_branch=2021Q1
    printf "Binary packages are using the $repo_branch branch.\n"
    case $repo_branch in
    latest)
	git_branch=''
	;;
    
    *)
	git_branch="--branch $repo_branch"
	;;

    esac

    if ! which git; then
	pkg install -y git
    fi
    if ! git clone --depth 1 \
	    https://git.FreeBSD.org/ports.git $git_branch --single-branch \
	    $PORTSDIR; then
	git clone --depth 1 \
	    https://cgit-beta.FreeBSD.org/ports.git \
	    $git_branch --single-branch $PORTSDIR
    fi
    ;;
    
*)
    printf "$0: Not supported on $(auto-ostype).\n"
    exit 1
    ;;

esac
