#!/bin/sh

# Please read the instructions in file README before installing this
# update.

# make backup copies of some files?
backup=true
#backup=false

# we really need the new binaries due to Kpathsea changes:
if test x`(TEXINPUTS='{a,b}' kpsetool -p tex)` != xa:b; then
  echo "You do not have the teTeX-0.4 binaries installed."
  echo "Please upgrade your binaries for all platforms you are using"
  echo "before installing this update."
  exit 1
fi

: ${TEXMF=`kpsetool -v '$TEXMF'`}
: ${TETEXDIR=`kpsetool -v '$TETEXDIR'`}
: ${SELFAUTODIR=`kpsetool -v '$SELFAUTODIR'`}
: ${SELFAUTOPARENT=`kpsetool -v '$SELFAUTOPARENT'`}
: ${VARFONTS=`kpsetool -v '$VARFONTS'`}

{ test -n "$TEXMF" && test -d "$TEXMF"; } ||
  {
    echo 'Fatal error: could not auto-detect your $TEXMF directory.' >&2
    exit 1
  }

test -w "$TEXMF" || 
  {
    echo "Fatal error: $TEXMF: no permission to write this directory"
    exit 1
  }

{ test -n "$TETEXDIR" && test -d "$TETEXDIR"; } ||
  {
    echo 'Fatal error: could not auto-detect your $TETEXDIR directory.' >&2
    exit 1
  }

test -w "$TETEXDIR" || 
  {
    echo "Fatal error: $TETEXDIR: no permission to write this directory"
    exit 1
  }

test -f base.tar.gz ||
  {
     echo "Cannot install this update without file base.tar.gz."
     exit 1
  }

here=`pwd`

cat <<eof
Welcome to the teTeX 0.3.4 -> 0.4 update procedure!
 
This script will update your teTeX system installed with
  TETEXDIR=$TETEXDIR
  TEXMF=$TEXMF
 
Some configuration files have changed (e.g. texmf.cnf, modes.mf,
psfonts.map), and these files must be updated to get a working 0.4
system. This update script will just overwrite your files with
new versions without preserving your changes. A backup copy will
be made for
  config.ps, psfonts.map, special.map, texmf.cnf
(with -BAK appended to the old filename).
 
If you have made other changes which you want to backup, interrupt
here (Control-C), backup your files and start over again. 

Press RETURN to continue.
eof
 
read a

tars="
ams-fonts.tar.gz
fonts-doc.tar.gz
general-doc.tar.gz
generic-doc.tar.gz
latex-base.tar.gz
latex-doc.tar.gz
latex-extra.tar.gz
metapost-doc.tar.gz
metapost.tar.gz
misc-fonts.tar.gz
postscript-fonts.tar.gz
programs-doc.tar.gz
pstricks.tar.gz
sauter-fonts.tar.gz
tetex-base.tar.gz
xypic.tar.gz
"

umask 022
cd $TEXMF
echo "Fixing permissions in $TEXMF..."
chmod -R u+w . 2>/dev/null

cd $TEXMF

$backup && test -f dvips/config.ps && { test -d dvips/config || mkdir dvips/config; cp dvips/config.ps dvips/config/config.ps-BAK; }
$backup && test -f dvips/psfonts.map && { test -d dvips/misc || mkdir dvips/misc; cp dvips/psfonts.map dvips/config/psfonts.map-BAK; }
$backup && test -f fontname/special.map && cp fontname/special.map fontname/special.map-BAK

test -d dvips/config || mkdir dvips/config
mv dvips/config.* dvips/config 2>/dev/null

rm -f doc/help/faq/de-tex-faq 2>/dev/null
rm -f doc/dutch/latex/ntgclass doc/fonts/fontname/NEWS doc/generic/babel/tb1604.dvi doc/german/allgemein/l2kurz.dvi doc/german/dinbrief doc/german/g-brief doc/german/koma-script doc/german/styles/germdoc.dvi doc/german/styles/picinpar.dvi doc/german/styles/script.dvi doc/help/TeX-index doc/help/companion.ctan doc/help/ctan/ctan.dat doc/latex/styles/myletter.dvi dvips/8a.enc dvips/8r.enc dvips/ad.enc dvips/asex.enc dvips/asexp.enc dvips/color.pro dvips/config.dfaxhigh dvips/config.dvired dvips/config.ps dvips/cork.enc dvips/crop.pro dvips/dc.enc dvips/dvips.enc dvips/finclude.pro dvips/funky.enc dvips/ps2frag.ps dvips/psfonts.map
rm -f dvips/render.ps dvips/resolution400.ps dvips/special.pro dvips/tex.pro dvips/texc.pro dvips/texmext.enc dvips/texmital.enc dvips/texmsym.enc dvips/texps.pro dvips/xl2.enc dvips/xt2.enc fonts/source/jknappen/sauter/b-cspex.mf lists/adobe-fonts lists/alpha-osf2.0 lists/alpha-osf3.2 lists/babel-doc lists/bitstrea-fonts lists/concrete-fonts lists/dcfonts-doc lists/fontname-doc lists/gothic-fonts lists/hppa1.1-hpux10.01 lists/hppa1.1-hpux9.01 lists/i386-bsdi2.0 lists/i386-freebsd2.0.5 lists/i386-freebsd2.1.0 lists/i386-netbsd1.0 lists/i386-netbsd1.1 lists/i486-linux lists/i486-linuxaout lists/m68k-linux lists/m68k-linuxoldld lists/mips-irix5.2 lists/mips-irix5.3 lists/mips-ultrix4.4 lists/oldgerman-doc lists/pandora-fonts lists/rs6000-aix3.2 lists/rs6000-aix4.1.1
rm -f lists/sparc-solaris2.4 lists/sparc-solaris2.5 lists/sparc-sunos4.1.3 lists/texdraw-doc lists/urw-fonts lists/wasy-fonts tex/generic/babel/russian.sty tex/generic/hyphen/dkhyph.tex tex/latex/README tex/latex/base/gglo.ist tex/latex/base/gind.ist tex/latex/base/source2e.tex tex/latex/fancyheadings/fixmarks.sty tex/latex/misc/README tex/latex/misc/atari.def tex/latex/misc/isolatin.def tex/latex/misc/mac.def tex/latex/misc/pc850.def tex/latex/misc/roman8.def tex/latex/misc/umlaute.sty tex/latex/psfrag/psfrag.sty tex/latex/psnfss/README tex/latex/seminar/fancybox.sty
rmdir tex/latex/psfrag fonts/type1/adobe/courier fonts/afm/adobe/mathptm doc/german/styles doc/german/allgemein doc/german doc/dutch/latex doc/dutch 2>/dev/null

for i in $tars; do
  test -f $here/$i || continue
  echo
  echo "Extracting $here/$i..."
  echo
  gzip -dc $here/$i | (umask 0; tar -xvf -)
done

cd $TETEXDIR
rm -f CREDITS FAQ-teTeX FEATURES LICENSE TETEXDOC.dvi TETEXDOC.tex TETEXDOC.toc 

$backup && { test -f texmf.cnf && cp texmf.cnf texmf.cnf-BAK; }
rm -f info/localdir
echo
echo "Extracting $here/base.tar.gz..."
echo
gzip -dc $here/base.tar.gz | (umask 0; tar -xvf -)

# restore TETEXDIR and TEXMF in texmf.cnf. Use $SELFAUTODIR, $SELFAUTOPARENT, $TETEXDIR
# where possible.
test -z "$SELFAUTODIR" && SELFAUTODIR=_do_not_use
test -z "$SELFAUTOPARENT" && SELFAUTOPARENT=_do_not_use

if test x"`kpsetool -v '$TETEXDIR'`" != x"$TETEXDIR"; then
  case $TETEXDIR in
    "$SELFAUTODIR") ttd='$SELFAUTODIR';;
    $SELFAUTODIR/*) ttd=`echo $TETEXDIR | sed "s@$SELFAUTODIR/@\\\$SELFAUTODIR/@"`;;
    "$SELFAUTOPARENT") ttd='$SELFAUTOPARENT';;
    $SELFAUTOPARENT/*) ttd=`echo $TETEXDIR | sed "s@$SELFAUTOPARENT/@\\\$SELFAUTOPARENT/@"`;;
  esac
  cd $TETEXDIR
  ed texmf.cnf >/dev/null 2>&1 <<eof
/^TETEXDIR/c
TETEXDIR        = $ttd
.
w
q
eof
fi

if test x"`kpsetool -v '$TEXMF'`" != x"$TEXMF"; then
  case $TEXMF in
    "$TETEXDIR") ttd='$TETEXDIR';;
    $TETEXDIR/*) ttd=`echo $TEXMF | sed "s@$TETEXDIR/@\\\$TETEXDIR/@"`;;
    "$SELFAUTODIR") ttd='$SELFAUTODIR';;
    $SELFAUTODIR/*) ttd=`echo $TEXMF | sed "s@$SELFAUTODIR/@\\\$SELFAUTODIR/@"`;;
    "$SELFAUTOPARENT") ttd='$SELFAUTOPARENT';;
    $SELFAUTOPARENT/*) ttd=`echo $TEXMF | sed "s@$SELFAUTOPARENT/@\\\$SELFAUTOPARENT/@"`;;
  esac
  cd $TETEXDIR
  ed texmf.cnf >/dev/null 2>&1 <<eof
/^TEXMF/c
TEXMF   = $ttd
.
w
q
eof
fi

texhash
texconfig init

cat <<eof

We are done... Please check your environment with 
  texconfig confall
again and fix any problems before proceeding.

Then, call texconfig again to set up hyphenation, paper size for
printing, printer mode (implies resolution), font generation, etc.
just like after your first installation.

Welcome to teTeX-0.4!
eof
