#!/bin/sh -e

##########################################################################
#   Script description:
#       Mark a package critical for auto-update-system
#       
#   History:
#   Date        Name        Modification
#   2020-08-13  Jason Bacon Begin
##########################################################################

usage()
{
    printf "Usage: $0 package [package ...]\n"
    exit 1
}


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

if [ $# -lt 1 ]; then
    usage
fi

case $(auto-ostype) in
DragonFly|FreeBSD)
    mkdir -p $(auto-localbase)/etc/auto-admin
    for pkg in "$@"; do
	auto-append-line $pkg $(auto-localbase)/etc/auto-admin/critical-packages nocomment
    done
    if ! pkg info -q $pkg; then
	printf "Warning: $pkg is not currently installed.\n"
	exit 1
    fi
    ;;

*)
    # FIXME: Can this be merged with the FreeBSD case?
    # auto-pkgsrc-prefix exits non-zero if no tree found
    PKGSRC=$(auto-pkgsrc-prefix) || true
    if [ -z "$PKGSRC" ]; then
	printf "$0: No active pkgsrc tree found.\n"
	exit 1
    fi
    mkdir -p $PKGSRC/etc/auto-admin
    for pkg in "$@"; do
	auto-append-line $pkg $PKGSRC/etc/auto-admin/critical-packages nocomment
    done
    
    # FIXME: Exit non-zero if package is not installed
    # Add support to auto-package installed?
    ;;

esac
