#!/bin/sh -e

##########################################################################
#   Script description:
#       Switch system to use latest packages rather than quarterly
#
#   Arguments:
#       Any arguments to pass to auto-update-system
#       
#   History:
#   Date        Name        Modification
#   2019-06-16  Jason Bacon Begin
##########################################################################

usage()
{
    printf "Usage: $0 [--no-reboot] [--yes] [arguments to pass to auto-update-system]\n"
    exit 1
}


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

case $(auto-ostype) in
FreeBSD)
    while [ 0$(echo $1 | cut -c 1,2) = 0'--' ]; do
	if [ 0$1 = 0'--help' ]; then
	    usage
	elif [ 0$1 = 0'--no-reboot' ]; then
	    no_reboot=1
	    shift
	elif [ 0$1 = 0'--yes' ]; then
	    proceed=1
	    shift
	else
	    usage
	fi
    done

    dir=/usr/local/etc/pkg/repos
    file=$dir/bleeding-edge.conf
    if [ -e $file ]; then
	printf "$0: You are already using latest packages.\n"
	exit 0
    fi
    if [ 0$proceed != 01 ]; then
	cat << EOM

			       *** WARNING ***
				 
Switching from quarterly to latest is not easily reversible.  Once applications
such as Firefox, Chromium, and Thunderbird are upgraded to the latest release,
their configuration files may not be readable to older versions present in
the quarterly branch.

Also be aware that you will likely experience occasional regressions (new bugs)
when using the latest packages.  You can be of service to the FreeBSD project
by using the latest packages and reporting regressions you encounter.  If it
is more important that software on this system be as stable as possible, you
may want to stay with the quarterly packages instead.

EOM
	printf "Are you sure you want to switch to latest? y/n [n] "
	read proceed
	if [ 0$proceed != 0y ]; then
	    exit
	fi
    fi
    
    mkdir -p $dir
    auto-backup-file $file
    cat << EOM > $file
FreeBSD: {
  url: "pkg+http://pkg.FreeBSD.org/\${ABI}/latest"
}
EOM
    pkg upgrade -y
    auto-check-ports-branch
    
    if [ 0$no_reboot != 01 ]; then
	printf "Reboot to reload services? (y/n) [y] "
	read reboot
	if [ 0$reboot != 0n ]; then
	    shutdown -r now
	fi
    fi
    ;;
    
*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
