#!/bin/sh
# $Id: tl-hyph-test 74203 2025-02-22 17:51:24Z karl $
# Public domain. Karl Berry, 2021; Arthur Rosendahl, 2021--2022.
# 
# Method for testing hyph-utf8 before committing to TL.
# The idea is to make a new TL tree, unzip the hyph-utf8.tds.zip there,
# build TL from the modified (not committed) tree,
# install from that TL into another temp dir.
# Thus checking all the steps that will happen after commit.
# It's complicated.

# Usage:
## Initial setup for first time:
##tmpdir=/home/texlive/hyphtest # used in TLhyphtest.pro (used below).
##                              # but otherwise could be anything
##mkdir $tmpdir # e.g., , could be anywhere
##cd $tmpdir
## 
## Get svn checkout of Master tree; need real checkout for the build below.
##svn -q co svn://tug.org/texlive/trunk/Master # takes an hour
## If on tug, slightly faster:
##time nice -19 svn -q co file:///home/svn/texlive/trunk/Master
#
# 
# After the first time, when the checkout already exists:
if false; then # better to run these one at a time
  cd $tmpdir
  rm -rf inst catdump.pl     # clean up from last time
  cd Master
  ~karl/bin/svri
  time svn update -q  # get updates made in the meantime (could take an hour)
  svn status          # make sure it's completely clean
  # 
  # Unzip the new hyph-utf tree in our temp checkout.
  cd texmf-dist
  ls -l $CTAN/install/language/hyph-utf8.tds.zip # ensure new file on ctan
  unzip !$                                       # overwrite All
  # In that unzip command, overwrite All symlinks with the regular files.
  # (Can't use -q since we have to do this overwrite.)
  #   On tug.org, CTAN=/home/ftp/tex-archive.
  #   This unzip won't remove any no-longer-distributed files;
  #   deal with that below.
  # 
  # Unzip new tlpsrc files (without leading directory):
  cd ../tlpkg/tlpsrc
  unzip -j ../../texmf-dist/source/generic/hyph-utf8/tlpsrc.zip # overwrite All
  #
  # No tlpsrc should have any non-ASCII characters.
  # If so, fix them & report:
  ~karl/bin/nonascii *.tlpsrc
  #
  # Back to top level of $tmpdir (the one with the ./Master subdir).
  cd $tmpdir
  #
  # Remove the tlpsrc.zip.
  rm Master/texmf-dist/source/generic/hyph-utf8/tlpsrc.zip
  #
  # See what we've got.
  svn status Master # if new package/file has been added, set newpkg/file below
  svn diff Master >/tmp/sdh # review changes, especially in tlpsrc files
  #
  # When ready, run this script:
  #./Master/tlpkg/bin/tl-hyph-test >/tmp/tht.out
  echo $?
  # Make sure output is good, no errors building formats, etc.
  # 
  # If any files are "present but not covered" (in the "file
  # consistency" check), look to see if they are in the original
  # hypf-utf8.tds.zip; if so, then it's a bug in the tlpsrc generation,
  # report. If not, then rm them from our working directory, and svn rm
  # them from the TL dev directory where we'll eventually commit. (The
  # usual method for determining when files have been removed won't work
  # here because hyph-utf8 is split into numerous TL packages.)
  #
  # Once it all works, can install hyph-utf8 with ctan2tl [p] as usual;
  #   copy any changed .tlpsrc (or other) files to dev, and
  #   don't commit in hyphtest, so additions and removals will be correct.
  # Also, good to run a test build right after committing since there is a
  #   good chance of failure, despite all the testing:
  #force_rebuild=true cron.trunk # or cron.pretest, if during pretest
  # 
  # To clean up here:
  #rm -rf inst tmp catdump.pl
  #svn revert --depth infinity Master >/tmp/svri
  #rm `svn status Master | sed -n 's/\?//p'` # remove "?" files (new ones)
  #svn status Master  # ensure clean
fi

# 
: ${do_mktexlsr=true}
: ${do_tlpdb=true}
: ${do_lang_files=true}
: ${do_install=true}
: ${do_pkgs=true}
: ${newfile=hyph-he.tex}  # some new file in the new release
: ${newpkg=hyphen-hebrew} # and the package it's in

real0=`realpath $0`
mydir=`cd \`dirname "$real0"\` && pwd`  # tlpkg/bin

Master=`cd $mydir/../.. && pwd`
test -d "$Master" || { echo "$0: no Master dir: $Master" >&2; exit 1; }

top=`cd $Master/.. && pwd`

# /usr/local/bin for updated perl on tug.
PATH=$Master/bin/x86_64-linux:/usr/local/bin:/usr/bin
TMPDIR=$top/tmp; export TMPDIR
test -d $TMPDIR || mkdir $TMPDIR
test -d $TMPDIR || { echo "$0: no tmpdir?: $TMPDIR" >&2; exit 1; }

unset TEXMFSYSVAR; unset TEXMFSYSCONFIG
unset TEXMFVAR; unset TEXMFCONFIG
MKTEXFMT=0; export MKTEXFMT
MKTEXTFM=0; export MKTEXTFM

if $do_mktexlsr; then
  printf "\n\f `date`\n"
  set -x
  mktexlsr --verbose || exit 1; set +x
fi
#
if kpsewhich $newfile >/dev/null; then :; else
  echo "$0: kpsewhich could not find new file, ls-R problem?: $newfile" >&2
  exit 1
fi

if $do_tlpdb; then
  printf "\n\f `date`\n"
  # It takes a long time to read the Catalogue info; it can be turned
  # off as shown, but then the resulting texlive.tlpdb will be hugely
  # different from the canonical one, which makes it hard to check.
  nocat="--catalogue /nonesuch"; nocat=
  
  # If running this multiple times, can use --catalogue-dump
  # to avoid rereading the XML every time; just don't forget
  # to remove the dump file when done, or it will never get updated.
  # If you want to sit around waiting for the Catalogue reading every time,
  # can just disable as shown.
  catdump="--catalogue-dump $top/catdump.pl" #; catdump=
  
  # -v for progress; -vv for extremely voluminous debugging.
  # --save-anyway because there will probably be revision decreases.
  # --from-files because we don't want to commit the new files,
  #   so the script has to read the filesystem, not svn.
  opts="-v --save-anyway --from-files $nocat $catdump"
  set -x
  $Master/tlpkg/bin/tl-update-tlpdb $opts || exit 1; set +x
fi
#
# we intentionally use tlmgr instead of grepping tlpdb directly
# to make sure it's working normally.

if tlmgr info --list $newpkg | grep /$newfile'$' >/dev/null; then :; else
  echo "$0: tlmgr could not find new file, tlpdb problem?: $newfile" >&2
  exit 1
fi

# 
# Remake the language.* files. Irritatingly different for different
# formats and engines.

# return 0 if files $1 and $2 are the same except for the first lines.
# (we need to ignore the timestamps in the generation lines.)
same_except_for_line1 ()
{
  rm -f $TMPDIR/sef1 $TMPDIR/sef2
  sed 1d <"$1" >$TMPDIR/sef1
  sed 1d <"$2" >$TMPDIR/sef2
  cmp -s $TMPDIR/sef1 $TMPDIR/sef2
}

if $do_lang_files; then
  printf "\n\f `date`\n"
  echo "$0: updating language.* files ..."

  cd $Master/texmf-dist/tex/generic/config || exit 1
  for utype in language.dat language.def language.dat.lua; do
    ufile=$utype
    cmd="tlmgr -q generate --dest=$ufile.new $utype"
    # -q to avoid the "helpful" message about running fmtutil.
    printf "$ufile update with: $cmd\n"
    $cmd || { echo "$0: $ufile generation failed: $cmd" >&2; exit 1; }
    if same_except_for_line1 $ufile $ufile.new; then
      printf "  same: "; ls -l `pwd`/$ufile
      rm $ufile.new 
    else
      mv -v $ufile.new $ufile || exit 1
      printf "  updated: "; ls -l `pwd`/$ufile
      svn diff $ufile >$TMPDIR/$ufile.diff
      echo "  (diff in $TMPDIR/$ufile.diff)"
    fi
  done
fi

# 
printf "\n\f `date`\n"
cmd="tlmgr check files runfiles"
echo "$0: checking file consistency: $cmd"
$cmd || exit $?

# 
# Install minimal, then install the packages with AddHyphen directives
# (in practice: hyphen-* and dehyph-exptl), to avoid installing all the
# non-language stuff, which is a ton.

instdir=$top/inst
if $do_install; then
  printf "\n\f `date`\n"
  echo "$0: installing minimal TL into: $instdir"

  rm -rf $instdir
  date
  set -x
  $Master/tlpkg/bin/tl-try-install \
    -o $instdir \
    --profile $Master/tlpkg/dev/profiles/TLhyphtest.pro \
    --quiet \
    --repo $Master \
  || { echo "$0: tl-try-install failed." >&2; exit 1; }
  set +x
fi

if $do_pkgs; then
  printf "\n\f `date`\n"
  echo "$0: installing hyph packages with tlmgr into: $instdir"

  tlpsrc=$Master/tlpkg/tlpsrc
  hyphen_pkgs=`cd $tlpsrc && grep -l AddHyphen * | sed s/.tlpsrc//`
  if test -z "$hyphen_pkgs"; then
    echo "$0: no packages with AddHyphen found in: $tlpsrc" >&2
    exit 1
  fi

  engine_pkgs="pdftex etex xetex luatex uptex "

  echo "$0: installing hyphen packages: " $hyphen_pkgs
  echo "$0:        and engine packages: " $engine_pkgs
  
  # reset PATH to run using our just-installed tree.
  PATH=$instdir/bin/x86_64-linux:/usr/local/bin:/usr/bin
  date
  set -x
  tlmgr install $hyphen_pkgs $engine_pkgs || exit $?
fi

# also written into inst/ since we just changed PATH.
printf "\n\f `date`\n"
echo "$0: running fmtutil-sys separately from install for reproducibility:"
set -x
fmtutil-sys --all
# and that's our exit status.
