usage () {
  echo '$1 = location of your personal copy of TeXLive (please no space in path)'
  echo '$2 = basever = date of TeXLive checkout as format yyyy.mm.dd'
  exit 0
}

if [ -z $2 ]; then usage; fi

#tlnet=${1:-/home/norbert/public_html/tlnet}
#tlnet=${1:-/home/hille/devel/TeXLive/ctan_tl_texmf_tl_bin/ctan.mirror.norbert-ruehl.de}
#tlnet=${1:-/home/hille/devel/TeXLive/zzz_upstream_raw/ctan.mirror.norbert-ruehl.de}
tlnet="$1"

#basever=2023.04.09
basever="$2"

verstr=`date +%Y%m%d`
pkg="context-${basever}.${verstr}"
orig="context_${basever}.${verstr}+dfsg.orig.tar.gz"

if [ -r $pkg ] ; then
  echo "already here: $pkg"
  exit 1
fi

unpackone() {
  what=$1
  for i in "" .doc .i386-linux ; do
    fn="$tlnet/archive/$what${i}.tar.xz"
    if [ -r "$fn" ] ; then
      echo "unpacking $fn"
      tar -xf "$fn"
    fi
  done
}

echo "installing from $tlnet"

mkdir $pkg
cd $pkg
unpackone context
# remove win32 stubs
rm -r texmf-dist/scripts/context/stubs/mswin/
rm -r texmf-dist/scripts/context/stubs/win64/
#
# remove non-free files for the moment
# replaced in TL already
rm -rf texmf-dist/tex/context/colors/profiles/

grep -l -r licence=cc-by-nc-sa *|xargs rm

# Remove luametatex binary
rm -f bin/i386-linux/luametatex

# Remove (non)broken links (after luametatex removal)
rm -f bin/i386-linux/context bin/i386-linux/mtxrun
rm -f bin/*/context.lua rm bin/*/mtxrun.lua
# Kill directory, if empty
find bin/ -type d -empty -delete

# Remove pdf formatted manual pages
rm -f texmf-dist/doc/man/man1/*pdf

# Remove EPS files stolen/generated from/by CorelDraw.
rm -f texmf-dist/doc/context/sources/general/manuals/start/graphics/ma-cb-00.eps
rm -f texmf-dist/tex/context/sample/common/spider.eps

Remove TeX Live package information
rm -rf tlpkg

cd ..

GZIP=-9 tar -czf $orig $pkg
