#!/bin/sh
# $Id: tl-update-asy 70552 2024-03-10 17:59:37Z karl $
# Public domain.  Originally written 2008, Karl Berry.
# Info about updating asymptote.
# 
# Do not just run this script. It won't work. It is necessary to run the
# commands one at a time and think about what is being done.
# 
# Also, the script uses various paths and convenience variables that I
# define for myself; feel free to peruse tug.org:~karl/.bash_login, etc.
# --karl

test "x$1" = x--build || exit 1
renice +19 -p $$ >/dev/null 2>&1

# 
if false && $update_sources_from_ctan; then
  # this part is about copying the sources from CTAN to TL.
  cd $B/source/utils/
  # be sure no dregs:
  (cd asymptote && make distclean; rm -f Makefile; sup && sat) 
  \cp -arf /home/ftp/tex-archive/graphics/asymptote/ .

  cd asymptote
  ver=`awk -F\" '{print $2}' revision.cc | sed 1q` # the newly-released version
  echo $ver
  edit ../README for $ver

  # show list of new files to add:
  svn status | sed -n 's/^\?//p'
  svn add `!!`
  
  # show list of files to remove:
  diff -qr . /home/ftp/tex-archive/graphics/asymptote/ \
  | egrep -v ' differ' | egrep -v '\.svn' | tee /tmp/asyrm
  sed -e 's,: ,/,' -e 's/^Only in //' /tmp/asyrm
  svn rm `!!`

  # final check and commit:
  svn status | fgrep \? #  # don't worry about binaries.
  svn commit -m"asy $ver sources" . ../README
  exit $?

# 
elif test "x$1" = x--build; then
  # this part is about building the x86 linux asymptote on tug.org.
  # Despite all the dire warnings above, this part actually can be run
  # as a script, because it's so often necessary to redo.
  unset GREP_OPTIONS CDPATH
  PATH=/usr/bin:/usr/sbin; export PATH # system gcc for sake of shared libs
  PATH=$lb:$PATH		       # get our kpsewhich
  svn=/usr/local/bin/svn                # so we need our svn explicitly
  set -x
  rhtoolset=/opt/rh/devtoolset-10/enable
  test -s $rhtoolset && . $rhtoolset
  
  set -e
  cd /home/texlive/karl/Build/source/utils/asymptote
  $svn update
  $svn revert asy-keywords.el
  $svn status
  if test -f Makefile; then
    make clean
    $svn revert doc/asy-latex.pdf
    rm -f Makefile
  fi
  rm -rf config.cache config.h config.log config.status errors.temp
  rm -rf /tmp/ainst autom4te.cache
  ./configure --prefix=/tmp/ainst --enable-static \
    --with-latex=/tmp/ainst/latex --with-context=/tmp/ainst/context \
    --disable-fftw --disable-gl --disable-gsl --disable-lsp \
    --enable-texlive-build CFLAGS=-g CXXFLAGS=-std=c++11
  make
  make check
  make install-prebuilt
  exit 0


# 
elif false && $update_runtime_from_ainst; then
  # this part is about copying the results of a successful build 
  # and install (from the previous section) into the Master/texmf runtime.
  # This should be done only when new binaries are being commited,
  # because support files and binaries are required to match versions.
  cp="cp -afv"

  xu=$B/source/utils
  xy=$xu/asymptote
  cd $xy
  ver=`awk -F\" '{print $2}' revision.cc | sed 1q`; echo $ver
  #
  $cp binaries/x86_64-windows/texlive/asy.exe $pg/asymptote64/
  # also check dlls:
  ls -lR binaries/x86_64-windows/texlive/ $tm/tlpkg/asymptote64/

  cd /tmp/ainst
  $cp share/info/{*/,}*.info $xist/doc/info
  $cp share/man/man1/asy.1 $xist/doc/man/man1/asy.1
  $cp share/man/man1/xasy.1x $xist/doc/man/man1/xasy.1
  $cp -r share/doc/asymptote $xist/doc
  #
  $cp -r share/asymptote $xist
  #
  $cp latex/asymptote/* $xist/tex/latex/asymptote/
  $cp context*/asymptote/* $xist/tex/context/third/asymptote/

  make -C $xist/doc/man
  
  ci="$xu/README \
    $xy \
    $pb/tl-update-asy \
    $pg/asymptote* \
    $xist/doc/info/  $xist/doc/man $xist/doc/asymptote \
    $xist/asymptote/ $xist/tex/*/asymptote"

  svn status $ci | sed -n 's/^\?//p'
  svn add `!!`

  cd /tmp/ainst
  xmf=/home/texlive/karl/Master/texmf-dist
  diff -qr share/asymptote/ $xmf/asymptote/ | fgrep -v .svn
  #svn rm ...

  diff -qr share/doc/asymptote/ $xmf/doc/asymptote/ | fgrep -v .svn
  #svn rm ...

  svn status $ci  # just to see
  scom -m"asymptote $ver support files, windows binary" $ci
fi

# the production build on x86_64-linux and i386-linux:
# 
# cd .../Build/source/utils
# docker run -it -v "$(pwd)/asymptote:/asymptote" --entrypoint bash debian:10
# 
# In the docker container you are dropped in:
# apt update
# apt upgrade
# # note we do NOT install libtirpc-dev because it cannot disabled by configure
# apt install build-essential pkg-config libeigen3-dev libcurl4-openssl-dev libreadline-dev libboost-filesystem-dev flex libglu1-mesa-dev freeglut3-dev libosmesa6-dev libreadline6-dev zlib1g-dev bison libglm-dev libncurses-dev
# cd /asymptote
# ./configure --prefix=/tmp/asyinst --enable-static --enable-texlive-build CXXFLAGS=-std=c++11 --disable-gsl --disable-fftw --disable-lsp --disable-curl
# sed -i -e 's/^LIBS = /LIBS = -static-libgcc -static-libstdc++ /' Makefile
# make -j6
# exit
# Strip and copy the resulting asy.
# For i386 binaries:
#   docker run -it -v "$(pwd)/asymptote:/asymptote" --entrypoint bash i386/debian:10
