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 .source .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/icc/profiles/*

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

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

# Remove broken links (after luametatex removal)
rm -f bin/i386-linux/context bin/i386-linux/mtxrun

cd ..

GZIP=-9 tar -czf $orig $pkg
