#!/bin/sh
#
#  PKGCLEAN -- Clean the named package, or all packages from the system.
#
#  Usage:     pkgclean [-all] [-dist] [-init] <pkg>
#
#  Where	-all	clean all packages
#		<pkg>	package to be removed
#
# ----------------------------------------------------------------------------


all=no
init=no

# Process cmdline flags.
while [ -n "$1" ]; do
    case "$1" in
    "-all")                          # clean all package .s
        all=yes
	;;
    "-init")                         # init all packages
        init=yes
	;;
    *)
        pkg=$1
        break
	;;
    esac
    shift
done


if [ "$all" = "yes" ]; then
  if [ -e ".repo_pkgs" ]; then
    files=$(cat .repo_pkgs)
    for p in $files; do
      if [ -e "$p" ]; then
        rm -rf "$p"
        mkdir "$p"

        echo  > .installed
        echo  > .zzexport .def=
      fi
    done
  fi

elif [ "$init" = "yes" ]; then
  if [ -e ".repo_pkgs" ]; then
    files=$(cat .repo_pkgs)
    for p in $files; do
      if [ -e "$p" ]; then
        rm -rf "$p"
      fi
    done
  fi
  rm -rf Makefile .installed .repo*
  echo > .zzexport .def=

else
   if [ -e "$pkg" ]; then
      rm -rf "$pkg"
      mkdir "$pkg"
   else
     echo "Package '$pkg' is not currently installed"
     exit 1
   fi
fi


#  Normal exit.
exit 0



#=============================================================================
# Usage
#=============================================================================

Usage:
    echo "Usage: pkgclean [-all] [-dist] [-init] <pkg>"
    echo ""
    echo "    Where    -all	clean all packages"
    echo "	       <pkg>	package to be removed"

exit 0
