# Create the body of the 'packages' appendix
# Build a new index.mm.  If an old one exists and has the same contents,
# don't replace it (otherwise we'll rebuild the book for nothing).
FILE=packages_body_sorted.mm
if [ ! -f $FILE ]; then 		# no $FILE file yet,
  touch $FILE
fi
categories=`awk -F\| < /usr/ports/INDEX '{print $7}' | tr ' ' '\n' | sort | uniq`
echo >>$FILE.new The Ports collection contains software in the following categories:
set $categories
(while [ $# -gt 2 ]; do
  echo \\fB$1\\fP,
  shift
done
echo \\fB$1\\fP and \\fB$2\\fP.) >>$FILE.new
cat <<FOO >>$FILE.new
The rest of this chapter consists of tables showing the ports
currently available in each category.  Ports may appear in multiple
categories.
FOO
(for c in $categories; do
  cat <<FOO
.nh
.ad l
.TB "Software packages\(emcategory \s+2\fB$c\s0\fP"
.TS H
center,box,tab(#) ;
| lI | lw50 | .
Package name#Description
=
.TH
FOO
grep ".*|.*|.*|.*|.*|.*|[^|]*$c" /usr/ports/INDEX | sort -t\| \
  | awk -F\| '{print $1 "#T{\n" $4 "\nT}" }'
echo .TE
done) >>$FILE.new
if [ -f $FILE ]; then	# already have one, is it the same?
  cmp $FILE $FILE.new 2> /dev/null  >/dev/null
  if [ $? -ne 0 ]; then		# changed
    echo +++ $FILE changed - need to repeat format
    mv $FILE.new $FILE
  else
    rm $FILE.new
  fi
else
  mv $FILE.new $FILE
fi

  
