#!/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 -f base.tar.gz ||
  {
     echo "Cannot install this update without file base.tar.gz."
     exit 1
  }

here=`pwd`

{ 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
  }

cat <<eof
Welcome to the teTeX 0.3.3 -> 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, modes.mf, 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
  bibtex.tar.gz
  dc-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
  texdraw.tar.gz
  xypic.tar.gz
"

umask 022

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

$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 metafont/misc/modes.mf && cp metafont/misc/modes.mf metafont/misc/modes.mf-BAK
$backup && test -f fontname/special.map && cp fontname/special.map fontname/special.map-BAK
$backup && test -f maketex/maketex.site && cp maketex/maketex.site maketex/maketex.site-BAK
test -d dvips/config || mkdir dvips/config
mv dvips/config.* dvips/config 2>/dev/null

test -d fonts/source/ams/extracm && mv fonts/source/ams/extracm fonts/source/ams/cmextra
test -d fonts/tfm/ams/extracm && mv fonts/tfm/ams/extracm fonts/tfm/ams/cmextra
test -f fonts/source/ams/cmextra/cmex9.mf && rm -f fonts/source/ams/cmextra/cmex9.mf
test -f fonts/tfm/ams/cmextra/cmex9.tfm && rm -f fonts/tfm/ams/cmextra/cmex9.tfm

rm -rf bibtex/bst/abbrv.bst bibtex/bst/acm.bst bibtex/bst/alpha.bst bibtex/bst/amsalpha.bst bibtex/bst/amsplain.bst bibtex/bst/apalike.bst bibtex/bst/ieeetr.bst bibtex/bst/plain.bst bibtex/bst/siam.bst bibtex/bst/unsrt.bst

rm -f tex/latex/config/latex.ini

rm -rf fonts/*/public/dc fonts/pk/*/public/dc
rm -rf fonts/*/*knappen/dc fonts/pk/*/*knappen/dc
rm -rf fonts/*/public/tc fonts/pk/*/public/tc
rm -rf fonts/*/*knappen/tc fonts/pk/*/*knappen/tc

rm -f doc/babel/tb1202.dvi doc/babel/tb1401.dvi doc/babel/user.dvi doc/dutch/latex/ntgclass doc/fontname/ChangeLog doc/fontname/README doc/fontname/fontname.dvi doc/fonts/dc/dok.dvi doc/fonts/dc/help.eng doc/fonts/dc/help.fre
rm -f doc/fonts/dc/help.ger doc/german/allgemein/l2kurz.dvi doc/german/dinbrief/README doc/german/dinbrief/announce doc/german/dinbrief/brfbody.tex doc/german/dinbrief/brfkopf.tex doc/german/dinbrief/dbold.tex doc/german/dinbrief/dinbrief.dvi doc/german/dinbrief/dintab.dvi doc/german/dinbrief/example.tex
rm -f doc/german/dinbrief/test10.tex doc/german/dinbrief/test11.tex doc/german/dinbrief/test12.tex doc/german/dinbrief/testnorm.tex doc/german/g-brief/beispiel.tex doc/german/g-brief/g-brief.dvi doc/german/koma-script/liesmich.txt doc/german/koma-script/scr_new1.dvi doc/german/koma-script/scr_new2.dvi
rm -f doc/german/koma-script/scr_new3.dvi doc/german/koma-script/scr_new4.dvi doc/german/koma-script/scr_new5.dvi doc/german/koma-script/scr_new6.dvi doc/german/koma-script/scr_new7.dvi doc/german/koma-script/scr_new8.dvi doc/german/koma-script/scr_new9.dvi doc/german/koma-script/scrguide.dvi doc/german/misc/germdoc.dvi doc/german/misc/lkurz.dvi
rm -f doc/latex/koma-script 2>/dev/null
rm -f doc/latex/g-brief 2>/dev/null
rm -f doc/latex/dinbrief 2>/dev/null
rm -f doc/german/misc/picinpar.dvi doc/german/misc/script.dvi doc/help/CTAN.sites doc/help/TeX-FAQ doc/help/TeX-index doc/help/ctan.dat doc/latex/floatflt/00readme.flt doc/latex/floatflt/floatfig.txt
rm -f doc/latex/misc/labels.dvi doc/latex/ntgclass/00readme.txt doc/latex/ntgclass/catalog.txt doc/latex/ntgclass/changes.txt doc/latex/ntgclass/manifest.txt doc/latex/rotating/catalog doc/latex/styles/germdoc.dvi doc/latex/styles/here.doc doc/latex/styles/myletter.dvi
rm -f doc/latex/styles/ulem.doc doc/latex/umlaute/anleitung.dvi doc/latex/umlaute/umlaute.dvi doc/programs/info-stnd.dvi doc/programs/info.dvi doc/texdraw/Makefile doc/texdraw/texdraw.dvi doc/texdraw/texdraw.p10 doc/texdraw/texdraw.p11 doc/texdraw/texdraw.p12
rm -f doc/texdraw/texdraw.p13 doc/texdraw/texdraw.ps1 doc/texdraw/texdraw.ps2 doc/texdraw/texdraw.ps3 doc/texdraw/texdraw.ps4 doc/texdraw/texdraw.ps5 doc/texdraw/texdraw.ps6 doc/texdraw/texdraw.ps7 doc/texdraw/texdraw.ps8 doc/texdraw/texdraw.ps9
rm -f doc/texdraw/txdexamp.dvi doc/texdraw/txdexamp.p10 doc/texdraw/txdexamp.p11 doc/texdraw/txdexamp.p12 doc/texdraw/txdexamp.p13 doc/texdraw/txdexamp.ps1 doc/texdraw/txdexamp.ps2 doc/texdraw/txdexamp.ps3 doc/texdraw/txdexamp.ps4 doc/texdraw/txdexamp.ps5
rm -f doc/texdraw/txdexamp.ps6 doc/texdraw/txdexamp.ps7 doc/texdraw/txdexamp.ps8 doc/texdraw/txdexamp.ps9 doc/texdraw/txdexamp.tex dvips/8a.enc dvips/8r.enc dvips/DC.enc dvips/EC.enc dvips/asex.enc
rm -f 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/extex.enc
rm -f dvips/finclude.pro dvips/funky.enc dvips/odvips.enc dvips/ps2frag.ps dvips/psfonts.map dvips/render.ps dvips/resolution400.ps dvips/special.pro dvips/tex.pro dvips/texc.pro
rm -f dvips/texmext.enc dvips/texmital.enc dvips/texmsym.enc dvips/texps.pro dvips/xl2.enc dvips/xt2.enc fontname/softkey.map fontname/source.map fonts/source/ams/euler/ebigop.mf fonts/source/public/olddc/dxsixtst.mf
rm -f tex/generic/babel/russian.sty tex/generic/hyphen/dkhyph.tex tex/latex/base/cfgguide.tex tex/latex/base/clsguide.tex tex/latex/base/fntguide.tex tex/latex/base/gglo.ist tex/latex/base/gind.ilg tex/latex/base/gind.ind tex/latex/base/gind.ist tex/latex/base/ltpatch.ltx
rm -f tex/latex/base/modguide.tex tex/latex/base/source2e.tex tex/latex/base/usrguide.tex tex/latex/fancyheadings/extramarks.sty tex/latex/fancyheadings/fixmarks.sty tex/latex/graphics/dviwindo.def tex/latex/mfnfss/TS1cmr.fd tex/latex/mfnfss/TS1cmss.fd tex/latex/mfnfss/TS1cmtt.fd tex/latex/mfnfss/initials.sty
rm -f tex/latex/misc/a4.sty tex/latex/misc/endfloat.sty tex/latex/misc/german.sty tex/latex/misc/lplain.tex tex/latex/psfrag/epsf.sty tex/latex/psfrag/psfrag.sty tex/latex/seminar/fancybox.sty tex/plain/misc/dcstdedt.tex tex/plain/misc/epsf.tex
rmdir doc/german/* 2>/dev/null
rmdir doc/babel doc/fontname doc/german doc/latex/misc doc/latex/umlaute doc/texdraw fonts/pk fonts/tfm/tmp tex/latex/pictex tex/latex/psfrag 2>/dev/null
rmdir fonts/source/knappen 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
$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

test -n "$VARFONTS" && test -d "$VARFONTS" &&
  {
    echo "Please remove old dc/tc font bitmaps (pk) and metrics (tfm) from the"
    echo "directory $VARFONTS (if they exist)."
  }

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

