#!/bin/sh 
# $Id$
# Originally written by Karl Berry. Public domain.
# 
# Front-front-end script for c2l to update
# (with first arg "e") LaTeX2e,
# or (with first arg "dev") LaTeX-dev,
# etc.,
# each of which is comprised of several packages to be updated together.
# 
# Or, generally, c2lx PKG1 PKG2...
# 
# You have to look at the end of the output for the summarized results
# for each package, e.g., to see if any files were misplaced.
# 
# Final argument "p" runs place also (same as c2l --place).

placebase=${TMPDIR-/tmp}/`id -u`.tlplace
dirs_file=$placebase.dirs
first_file=$placebase.first
tmp_file=$placebase.tmp
alldiff_file=$placebase.alldiff
c2lout_file=$placebase.c2lout

if test "x$1" = xe || test "x$1" = x2e; then
  shift; label=l2e
  pkgs="amsmath cyrillic graphics tools latex firstaid latex-lab l3kernel"
elif test "x$1" = xdev; then
  shift; label=ldev
  pkgs="latex-amsmath-dev latex-firstaid-dev latex-graphics-dev latex-lab-dev"
  pkgs="$pkgs latex-tools-dev latex-base-dev l3kernel-dev l3backend-dev"
elif test "x$1" = x3; then
  shift; label=l3
  pkgs="l3backend l3experimental l3kernel l3packages"
elif test "x$1" = xctx; then
  shift; label=ConTeXt
  pkgs="mptopdf context-legacy context"
elif test "x$1" = xelegant; then
  shift; label=elegant
  pkgs="elegantbook elegantnote elegantpaper"
elif test "x$1" = xfundus; then
  shift; label=fundus
  pkgs="fundus-calligra fundus-cyr fundus-suetterlin" # other fonts nonfree
elif test "x$1" = xhep; then
  shift; label=hep
  pkgs="hep-font hep-math-font hep-acronym hep-bibliography hep-float"
  pkgs="$pkgs hep-graphic hep-math hep-paper hep-reference hep-text hep-title"
elif test "x$1" = xjxu; then
  shift; label=jxu
  pkgs="colorist einfart lebhart minimalist simplivre"
  pkgs="$pkgs beaulivre homework projlib"
  #pkgs="crefthe create-theorem q-and-a"
elif test "x$1" = xminim-all; then
  shift; label=minim
  pkgs="minim-hatching minim-math minim-mp minim-pdf minim-xmp minim"
elif test $# -eq 1; then
  echo "$0: unknown latex or group type: $1" >&2
  echo "$0: must be one of: e dev 3 ... minim-all" >&2
  echo "$0: or: PKG1 PKG2..." >&2
  echo "$0: USTL (sorry)." >&2
  exit 1
else
  label="explicitly given"
  count=0
  pkgs=
  for arg in "$@"; do
    test $count -eq $# && break # need to check for possible p
    pkgs="$pkgs $arg" # assume no whitespace in pkgnames as usual
    shift
    count=`expr $count + 1`
  done
  if test "x$1" != xp; then
    pkgs="$pkgs $1"
    shift
  # else leave p as remaining arg
  fi
fi

echo "`basename $0`: $label pkgs: $pkgs"

# 
>$first_file
>$alldiff_file
>$tmp_file
for p in $pkgs; do
  echo "${label} pkg $p" >&2 # progress report of a sort
  printf "\f ${label} pkg $p\n"
  # each c2l call creates various; combine the directory lists and output.
  c2l "$@" $p | tee $c2lout_file # include p(lace) arg
  sed -n "1,/`printf '\f'`/p" $c2lout_file >>$first_file # first part of output
  echo >>$first_file
  cat $dirs_file >>$tmp_file
  cat $placebase.diff >>$alldiff_file
done

sort -u $tmp_file >$dirs_file
wc -l $dirs_file
echo
cat $first_file
