#!/bin/sh -e

##########################################################################
#   Script description:
#       Mark a package to be rebuilt from source by auto-update-system
#       
#   History:
#   Date        Name        Modification
#   2021-01-13  Jason Bacon Begin
##########################################################################

usage()
{
    cat << EOM

Usage: $0 --list

Lists ports/packages currently marked for install from source

Usage: $0 category/name[@flavor] reason when

Marks a port/package for reinstalling from source by auto-update-system.
This may be desirable to restore options clobbered by "pkg[in] upgrade",
upgrade ports that cannot be packaged due to licensing restrictions, or
build with optimized CFLAGS set in /etc/make.conf.

FreeBSD ports: If the port/package supports flavors and no flavor is
specified, the default flavor is assumed.

The "reason" argument must not contain spaces.  It is a brief message that
will be displayed by auto-upgrade-system when rebuilding a package from
source.

"when" must be either "newer", in which case the port is reinstalled
from source only if newer than the installed package, or "always", in
which case the port is reinstalled from source regardless.  The latter
is useful when there are KBI incompatibilities between two extant
FreeBSD versions.  This occasionally affects kernel modules, such
that the package built on FreeBSD x.y is incompatible with FreeBSD x.(y+1).
Packages are built on x.y until it reaches EOL, about 3 months after
the release of x.(y+1).

Examples:

    $0 audio/lame license newer
    $0 math/openblas optimization newer
    $0 math/R build-options newer
    $0 wip/auto-admin work-in-progress newer
    $0 emulators/virtualbox-ose-kmod kbi always
EOM
    exit 1
}


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

if [ $# = 1 ] && [ $1 = '--list' ]; then
    if $(auto-using-pkgsrc); then
	prefix=$(auto-pkgsrc-prefix)
    elif [ $(auto-ostype) = FreeBSD ]; then
	prefix=$(auto-localbase)
    else
	printf "$0: Cannot determine install prefix.\n"
	exit 1
    fi
    config=$prefix/etc/auto-admin/install-from-source
    if [ -e $config ]; then
	cat $config
    fi
    exit 0
fi

if [ $# -ne 3 ]; then
    usage
fi
pkg=$1
if ! echo $pkg | fgrep -q '/'; then
    usage
fi
if echo $pkg | fgrep -q '@'; then
    flavor=$(echo $pkg | cut -d @ -f 2)
    pkg=$(echo $pkg | cut -d @ -f 1)
fi
reason=$2
when=$3

case $(auto-ostype) in
DragonFly|FreeBSD|OpenBSD)
    if [ $(auto-ostype) = FreeBSD ] || [ $(auto-ostype) = OpenBSD ]; then
	: ${PORTSDIR:=/usr/ports}
    else
	: ${PORTSDIR:=/usr/dports}
    fi
    if [ ! -e "$PORTSDIR/$pkg" ]; then
	printf "$PORTSDIR/$pkg: No such port.\n"
	exit 1
    fi
    mkdir -p $(auto-localbase)/etc/auto-admin
    # Check for port/package name only.  Don't duplicate if reason differs.
    auto-append-line $pkg "$pkg $reason $when $flavor" \
	$(auto-localbase)/etc/auto-admin/install-from-source nocomment
    ;;

*)
    # FIXME: Can this be merged with the FreeBSD case?
    # auto-pkgsrc-{dir|prefix} exit non-zero if no tree found
    PKGSRC=$(auto-pkgsrc-dir) || true
    if [ ! -e "$PKGSRC/$pkg" ]; then
	printf "$PKGSRC/$pkg: No such package.\n"
	exit 1
    fi
    PREFIX=$(auto-pkgsrc-prefix) || true
    if [ -z "$PREFIX" ]; then
	printf "$0: No active pkgsrc tree found.\n"
	exit 1
    fi
    mkdir -p $PREFIX/etc/auto-admin
    auto-append-line $pkg "$pkg $reason" \
	$PREFIX/etc/auto-admin/install-from-source nocomment
    ;;

esac
