#!/bin/sh -e

##########################################################################
#   Synopsis:
#       
#   Description:
#       
#   Arguments:
#       
#   Returns:
#
#   Examples:
#
#   Files:
#
#   Environment:
#
#   See also:
#       
#   History:
#   Date        Name        Modification
#   2021-09-23  Jason Bacon Begin
##########################################################################

usage()
{
    cat << EOM

Usage: $0 dist [dist ...]

FreeBSD dists:  base-dbg,kernel-dbg,lib32,lib32-dbg,src,tests
NetBSD dists:   base,comp,debug,etc,games,man,misc,modules,rescue,
		tests,text,xbase,xcomp,xdebug,xetc,xfont,xserver

EOM
    exit 1
}


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

if [ $# -lt 1 ]; then
    usage
fi
dists="$@"

auto-root-check $0

case $(auto-ostype) in
FreeBSD)
    for dist in $dists; do
	case $dist in
	base-dbg)
	    target=/usr/lib/debug/bin
	    ;;
	src)
	    target=/usr/src/sys
	    ;;
	lib32)
	    target=/usr/lib32
	    ;;
	lib32-dbg)
	    target=/usr/lib/debug/usr/lib32
	    ;;
	kernel-dbg)
	    target=/usr/lib/debug/boot/kernel
	    ;;
	tests)
	    target=/usr/tests
	    ;;
	*)
	    printf "Unsupported component: $dist\n"
	    exit 1
	    ;;
	esac
	if [ -e $target ]; then
	    printf "$dist already installed.\n"
	else
	    cd /
	    if uname -r | egrep -q 'RELEASE|BETA|RC'; then
		url=ftp://ftp.freebsd.org/pub/`uname -s`/releases/`uname -m`/`uname -r | cut -d'-' -f1,2`/$dist.txz
	    else
		url=ftp://ftp.freebsd.org/pub/`uname -s`/snapshots/`uname -m`/`uname -r | cut -d'-' -f1,2`/$dist.txz
	    fi
	    printf "Fetching $url...\n"
	    fetch $url
	    printf "Extracting $dist...\n"
	    tar Jxf $dist.txz
	    rm $dist.txz
	fi
    done
    ;;

NetBSD)
    cd /
    sets="$@"
    for set in $sets; do
	if [ -e /usr/$set ]; then
	    printf "$set already installed.\n"
	else
	    tarball=$set.tar.xz
	    url=http://ftp.netbsd.org/pub/NetBSD/NetBSD-`uname -r`/`uname -m`/binary/sets/$tarball
	    if [ ! -e $tarball ]; then
		printf "Fetching $url...\n"
		fetch $url
	    fi
	    printf "Extracting $set...\n"
	    tar Jxf $tarball
	    rm -f $tarball
	fi
    done
    ;;

*)
    auto-unsupported-os $0
    ;;

esac
