#!/bin/sh

##########################################################################
#   Script description:
#       Remove all installed packages
#       
#   History:
#   Date        Name        Modification
#   2016-01-07  Jason Bacon Begin
##########################################################################

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


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

if [ $# != 0 ]; then
    usage
fi

packages=`pkg_info | awk '{print $1}' | egrep -v 'bmake|pkg_install|openssl|bootstrap-mk-files|cwrappers'`
pkgsrc_dir=$(auto-pkgsrc-dir)

# First do make replace on not-for-delete packages, before removing anything
make=$(auto-pkgsrc-make)
for pkg in $packages; do
    if pkg_info -B $pkg | fgrep -q 'PACKAGE MAY NOT BE DELETED'; then
	dir=$pkgsrc_dir/$(pkg_info -B $pkg | awk -F = '$1 == "PKGPATH" { print $2 }')
	printf "Rebuilding $dir...\n"
	cd $dir
	$make clean clean-depends replace
    else
	pkg_delete -f $pkg
    fi
done
pkg_info
