#!/bin/sh
#
#  vanilla{tclsh,wish} - Build script for standard Tcl/Tk
#
#############################################################################
#
# Build script to generate a single-file "vanillawish"
# based on an AndroWish source tree. This is an experiment
# and requires a recent (2016-02-11) AndroWish check-out.
#
# This script must be run in directory outside of the
# AndroWish source tree which gets populated with copies of
# the AndroWish sources and further filled with the compiled
# (Linux 32 bit i586) products.
#
# It must be invoked with a single argument 'action':
#
# 'init'       initialize current directory from AndroWish
# 'sync'       sync with AndroWish source tree
# 'clean'      run 'make clean' in subdirectories
# 'distclean'  run 'make distclean' in subdirectories
# 'build'      (default action) build everything
#              If all goes well two files "vanillawish" and
#              "vanillatclsh" are produced which can be copied
#              and run on a 32 bit i586 Linux.
#
#############################################################################

set -e

SCRIPTDIR=$(dirname $0)
SCRIPTDIR=$(cd ${SCRIPTDIR} ; pwd)
AWDIR=$(dirname $SCRIPTDIR)
if ! test -d "${AWDIR}/jni" -o ! -d "${AWDIR}/assets" ; then
  echo >&2 "ERROR: installation problem"
  exit 1
fi

if test "$(pwd)" = "${AWDIR}" ; then
  echo >&2 "ERROR: refusing to run in AndroWish source tree"
  exit 1
fi

# prefix where things would be installed on Linux (= install root)
PFX=/opt/tcltk8.6

# working (build) directory
HERE=$(pwd)

# some checks on directory names
if test "$(echo ${HERE} | tr -d [:blank:])" != "${HERE}" ; then
  echo >&2 "ERROR: use another path which doesn't contain blanks"
  exit 1
fi
if test "$(echo ${PFX} | tr -d [:blank:])" != "${PFX}" ; then
  echo >&2 "ERROR: use another prefix which doesn't contain blanks"
  exit 1
fi

case ${PFX} in
  *${HERE}*)
    echo >&2 "ERROR: use another path which doesn't contain '${PFX}'"
    exit 1
    ;;
esac

# prefix relative to working (build) directory
PFX_HERE=${HERE}${PFX}

echo
echo "vanilla{tclsh,wish} - Build script for standard Tcl/Tk"
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -m32 -march=i586 -mtune=generic -DTCL_UTF_MAX=6"
CXX="g++ -m32 -march=i586 -mtune=generic -DTCL_UTF_MAX=6"
NM=nm
export STRIP AR RANLIB CC CXX RC NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl sdl2tk blt jpeg-turbo 3dcanvas"
SUBDIRS="${SUBDIRS} tkimg trf tktable tktreectrl tkpath itk v4l2 tkhtml"
SUBDIRS="${SUBDIRS} dbus-tcl dbus-intf tclx libdmtx ZBar zint"
SUBDIRS="${SUBDIRS} tcl-augeas tktray snack tkvnc tksvg VecTcl tclral"
SUBDIRS="${SUBDIRS} tclepeg tcluvc xotcl nsf libsocketcan tclcan vu"
SUBDIRS="${SUBDIRS} rl_json tkdnd mpexpr"
SUBDIRS="${SUBDIRS} tclcsv tkzinc libffi ffidl tcl-lmdb DiffUtilTcl"
SUBDIRS="${SUBDIRS} tclparser tclcompiler snap7 libmodbus fswatch"
SUBDIRS="${SUBDIRS} tserialport topcua parse_args tkvlc"

ACTION="$1"
if test -z "$ACTION" ; then
  ACTION=build
fi

clean_build_stamps() {
  for i in $@ ; do
    case "$i" in
      tcl)
        rm -f tcl/unix/build-stamp
        ;;
      sdl2tk)
        test -d sdl2tk.static && rm -f sdl2tk.static/unix/build-stamp
        rm -f sdl2tk/unix/build-stamp
        ;;
      tclkit)
        rm -f tclkit/vqtcl/build-stamp
        ;;
      libdmtx)
        rm -f libdmtx/tcl/build-stamp
        ;;
      ZBar)
        rm -f ZBar/tcl/build-stamp
        ;;
      zint)
        rm -f zint/backend_tcl/build-stamp
        ;;
      snack)
        rm -f snack/unix/build-stamp
        ;;
      VecTcl)
        rm -f VecTcl/WavReader/build-stamp
        rm -f VecTcl/TkBridge/build-stamp
        rm -f VecTcl/build-stamp
        ;;
      xotcl|nsf)
        rm -f ${i}/*.o ${i}/generic/stubs*/*.o
        rm -f ${i}/build-stamp
        ;;
      mpexpr)
        rm -f ${i}/unix/build-stamp
        ;;
      snap7)
        rm -f ${i}/build/unix/build-stamp
        ;;
      *)
        rm -f ${i}/build-stamp
        ;;
     esac
  done
}

if test "$ACTION" = "init" ; then
  echo "copying sources"
  for i in ${SUBDIRS} ; do
    echo "  $i"
    if test "$i" = "zlib" ; then
      rsync -a "tcl/compat/zlib" .
    elif test -d "${AWDIR}/undroid/dbus/$i" ; then
      rsync -a "${AWDIR}/undroid/dbus/$i" .
    elif test -d "${AWDIR}/undroid/$i" ; then
      rsync -a "${AWDIR}/undroid/$i" .
    else
      rsync -a "${AWDIR}/jni/$i" .
    fi
  done
  echo
  clean_build_stamps ${SUBDIRS}
  exit 0
fi

if test "$ACTION" = "sync" ; then
  echo "syncing sources"
  for i in ${SUBDIRS} ; do
    echo "  $i"
    if test "$i" = "zlib" ; then
      rsync -utlpr "tcl/compat/zlib" .
    elif test -d "${AWDIR}/undroid/dbus/$i" ; then
      rsync -utlpr "${AWDIR}/undroid/dbus/$i" .
    elif test -d "${AWDIR}/undroid/$i" ; then
      rsync -utlpr "${AWDIR}/undroid/$i" .
    else
      rsync -utlpr "${AWDIR}/jni/$i" .
    fi
  done
  echo
  clean_build_stamps ${SUBDIRS}
  exit 0
fi

if test "$ACTION" = "clean" -o "$ACTION" = "distclean" ; then
  echo "cleaning sources"
  for i in ${SUBDIRS} ; do
    echo "  $i"
    case "$i" in
      tcl)
        (
          exec >> build.log 2>&1
          make -C tcl/unix ${ACTION}
        ) || true
	;;
      sdl2tk)
        (
          exec >> build.log 2>&1
          if test -d sdl2tk.static ; then
            make -C sdl2tk.static/unix ${ACTION} || true
          fi
          make -C sdl2tk/unix ${ACTION} || true
        ) || true
	;;
      tclkit)
        (
          exec >> build.log 2>&1
          make -C tclkit/vqtcl ${ACTION} || true
        ) || true
	;;
      libdmtx)
        (
          exec >> build.log 2>&1
          make -C libdmtx/tcl ${ACTION} || true
        ) || true
	;;
      ZBar)
        (
          exec >> build.log 2>&1
          make -C ZBar/tcl ${ACTION} || true
        ) || true
	;;
      zint)
        (
          exec >> build.log 2>&1
          make -C zint/backend_tcl ${ACTION} || true
        ) || true
	;;
      snack)
        (
          exec >> build.log 2>&1
          make -C snack/unix ${ACTION} || true
        ) || true
	;;
      VecTcl)
        (
          exec >> build.log 2>&1
          make -C VecTcl/WavReader ${ACTION} || true
          make -C VecTcl/TkBridge ${ACTION} || true
          make -C VecTcl ${ACTION} || true
        ) || true
	;;
      mpexpr)
        (
          exec >> build.log 2>&1
          make -C mpexpr/unix ${ACTION} || true
        ) || true
        ;;
      tdom)
        (
          exec >> build.log 2>&1
          make -C tdom ${ACTION} || true
          make -C tdom/extensions/tnc ${ACTION} || true
        ) || true
        ;;
      snap7)
        (
          exec >> build.log 2>&1
          make -C snap7/build/unix -f unix.mk clean || true
        ) || true
        ;;
      *)
        (
          exec >> build.log 2>&1
          make -C $i ${ACTION} || true
        ) || true
        ;;
     esac
  done
  echo
  clean_build_stamps ${SUBDIRS}
  rm -f build.log assets.zip tassets.zip vanillawish vanillatclsh
  rm -rf assets tassets ${PFX_HERE}
  exit 0
fi

if test "$ACTION" != "build" ; then
  echo "Don't know what to do for '${ACTION}'. Specify action as"
  echo "one of 'init', 'sync', 'clean', 'distclean', and 'build'."
  echo "If omitted, the default action is 'build'."
  exit 1
fi

for i in ${SUBDIRS} ; do
  if ! test -d ${i} ; then
    echo >&2 "ERROR: missing directory '$i', please do 'sync' or 'init'."
    exit 1
  fi
done

echo
echo "writing log output to build.log"
echo

echo -e >> build.log \
  "\n***** build started on $(date '+%Y-%m-%d %H:%M:%S') *****\n"

fail() {
  echo -e >> build.log \
    "\n***** build failed on $(date '+%Y-%m-%d %H:%M:%S') *****\n"
  echo "failed, see build.log for details."
  exit 1
}

if test ! -d ${PFX_HERE} ; then
  if test -e .fixup-stamp ; then
    rm -f .fixup-stamp
  fi
fi

test ! -e .fixup-stamp || (
  exec >> build.log 2>&1
  # unfix paths in installation tree
  set -x
  perl -pi -e "s@${PFX}/lib@${HERE}/tcl/unix@g" ${PFX_HERE}/bin/*-config || true
  perl -pi -e "s@${PFX}@${PFX_HERE}@g" ${PFX_HERE}/bin/*-config || true
  perl -pi -e "s@${PFX}@${PFX_HERE}@g" ${PFX_HERE}/lib/*.sh || true
  set +x
  rm -f .fixup-stamp
) || fail

echo -n "build Tcl (static) ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tcl/unix
  test -e build-stamp && echo >&3 "already done" && exit 0
  ./configure --prefix=${PFX} --build=i586-linux-gnu --disable-shared \
    --with-zipfs || exit 1
  make binaries || exit 1
  make install-binaries install-libraries install-headers \
    install-private-headers install-packages INSTALL_ROOT=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build libressl (static, pic) ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd libressl
  ASMOPT=""
  GCCVER=$($CC --version | awk '/(GCC|gcc)/ {print ($3+0.0)*10;}')
  if test $GCCVER -lt 44 ; then
    ASMOPT="--disable-asm"
  fi
  test -e build-stamp && echo >&3 "already done" && exit 0
  ./configure --prefix=${PFX} --build=i586-linux-gnu --with-pic $ASMOPT \
    --disable-shared --with-openssldir=/etc/pki/tls || exit 1
  # shrink SUBDIRS
  perl -pi -e 's/^SUBDIRS.*/SUBDIRS = crypto ssl include/g' Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  # no libtool madness, please
  rm -f ${PFX_HERE}/lib/libcrypto.la ${PFX_HERE}/lib/libssl.la
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build zlib (static, pic) ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd zlib
  test -e build-stamp && echo >&3 "already done" && exit 0
  CFLAGS="-fPIC" ./configure --prefix=${PFX} --static
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
)

echo -n "build curl (static, pic) ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd curl
  test -e build-stamp && echo >&3 "already done" && exit 0
  # cheat libressl for build
  mkdir -p ssl/lib/pkgconfig
  cp ${PFX_HERE}/lib/pkgconfig/openssl.pc ssl/lib/pkgconfig
  cp ${PFX_HERE}/lib/pkgconfig/libssl.pc ssl/lib/pkgconfig
  cp ${PFX_HERE}/lib/pkgconfig/libcrypto.pc ssl/lib/pkgconfig
  perl -pi -e "s@${PFX}@${PFX_HERE}@g" ssl/lib/pkgconfig/*.pc
  ./configure --prefix=${PFX} --build=i586-linux-gnu --with-pic \
    --disable-shared --disable-ldap \
    --without-libidn2 --without-nghttp2 --without-zsh-functions-dir \
    --without-librtmp --without-libssh2 --without-libpsl --without-axtls \
    --without-nss --without-cyassl --without-mbedtls --without-polarssl \
    --without-gnutls --without-brotli \
    --with-ssl=$(pwd)/ssl --with-zlib=${HERE}/zlib
  # shrink SUBDIRS
  perl -pi -e 's/^SUBDIRS.*/SUBDIRS = lib include/g' Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  # cheat curl binary to make TclCurl configure happy
  echo '#!/bin/false' > ${PFX_HERE}/bin/curl && chmod a+x ${PFX_HERE}/bin/curl
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tcludp ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tcludp
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tdom ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tdom
  test -e build-stamp && echo >&3 "already done" && exit 0
  CC="${CC} -include $(pwd)/renxpat.h" \
    DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
      --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Tdom_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  cd extensions/tnc
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tdom=../.. --enable-threads || exit 1
  echo > exports '{ global: Tnc_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  cd ../..
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tclvfs ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tclvfs
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tclkit/vqtcl ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tclkit/vqtcl
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Vlerq_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build trofs ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd trofs
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Trofs_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries libraries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tbcload ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tbcload
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Tbcload_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tls ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tls
  test -e build-stamp && echo >&3 "already done" && exit 0
  CFLAGS="-DNO_SSL2=1 -DNO_SSL3=1" LDFLAGS="-lrt" DESTDIR=${HERE} ./configure \
    --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads \
    --with-ssl-dir=${PFX_HERE} || exit 1
  echo > exports '{ global: Tls_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build Memchan ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd Memchan
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure \
    --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Memchan_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries libraries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build TclCurl ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd TclCurl
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} LDFLAGS="-L${PFX_HERE}/lib" ./configure \
    --build=i586-linux-gnu --prefix=${PFX} \
    --with-curlprefix=${PFX_HERE} \
    --with-curlinclude=${HERE}/curl/include \
    --with-libcurl=${PFX_HERE}/lib \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Tclcurl_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries libraries || exit 1
  make install-binaries install-libraries install-scripts \
    DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

# prepare two sdl2tk subdirs
test -d sdl2tk.static || cp -rp sdl2tk sdl2tk.static

echo -n "build Tk (X11 backend, static) ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd sdl2tk.static/unix
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  ./configure --prefix=${PFX} --build=i586-linux-gnu --disable-shared \
    --disable-rpath --with-tcl=${HERE}/tcl/unix || exit 1
  rm -f ${PFX_HERE}/bin/wish.static
  make binaries || exit 1
  make install-binaries install-libraries install-headers \
    install-private-headers install-demos INSTALL_ROOT=${HERE} || exit 1
  mv ${PFX_HERE}/bin/wish8.6 ${PFX_HERE}/bin/wish.static
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build Tk (X11 backend, dynamic) ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd sdl2tk/unix
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  ./configure --prefix=${PFX} --build=i586-linux-gnu --enable-shared \
    --disable-rpath --with-tcl=${HERE}/tcl/unix || exit 1
  make binaries || exit 1
  make install-binaries install-libraries INSTALL_ROOT=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build blt ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd blt
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  # same BLT subset as AndroWish, please
  CFLAGS="-DNO_BGEXEC=1 -DNO_CRC32=1 -DNO_DDE=1"
  CFLAGS="${CFLAGS} -DNO_BGPATTERN=1 -DNO_DATATABLE=1 -DNO_DEBUG=1"
  CFLAGS="${CFLAGS} -DNO_TREE=1 -DNO_WATCH=1 -DNO_PICTURE=1"
  CFLAGS="${CFLAGS} -DNO_TABSET=1 -DNO_TABNOTEBOOK=1 -DNO_HIERBOX=1"
  CFLAGS="${CFLAGS} -DNO_BUSY=1 -DNO_DRAGDROP=1 -DNO_CUTBUFFER=1"
  CFLAGS="${CFLAGS} -DNO_DND=1 -DNO_CONTAINER=1 -DNO_BEEP=1"
  CFLAGS="${CFLAGS} -DNO_PRINTER=1 -DNO_TILE=1 -DNO_TILEFRAME=1"
  CFLAGS="${CFLAGS} -DNO_TILEBUTTON=1 -DNO_TILESCROLLBAR=1 -DNO_TREEVIEW=1"
  CFLAGS="${CFLAGS} -DNO_MOUNTAIN=1 -DNO_TED=1 -DNO_EPSCANV=1"
  CFLAGS="${CFLAGS} -DNO_SENDEVENT=1 -DNO_COMBOTREE=1 -DNO_HTEXT=1"
  CFLAGS="${CFLAGS} -DUSE_TCL_STUBS=1 -DUSE_TK_STUBS=1 -DTK_DRAWANGLEDCHARS=1"
  export CFLAGS
  ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --with-tclincls=${PFX_HERE}/include --with-tkincls=${PFX_HERE}/include \
    --disable-jpeg --enable-threads || exit 1
  # fixup shared lib deps etc.
  perl -pi -e 's/-ltk8.6/-ltkstub8.6/g' src/shared/Makefile
  perl -pi -e 's/-ltcl8.6/-ltclstub8.6/g' src/shared/Makefile
  perl -pi -e "s@-L${PFX}@-L${PFX_HERE}@g" src/shared/Makefile
  echo > exports '{ global: Blt_Init; Blt_SafeInit; local: *; };'
  perl -pi -e "s@-Wl,-E@-Wl,--version-script=$(pwd)/exports@g" \
    src/shared/Makefile
  make || exit 1
  make install INSTALL_ROOT=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build jpeg-turbo (static, pic) ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd jpeg-turbo
  test -e build-stamp && echo >&3 "already done" && exit 0
  chmod a+x ./configure
  ./configure --build=i586-linux-gnu --prefix=${PFX} --with-jpeg8 \
    --enable-static --disable-shared --with-pic --with-mem-srcdst
  make -C simd libsimd.la || exit 1
  make libjpeg.la || exit 1
  cp -p .libs/libjpeg.a .
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build 3dcanvas ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd 3dcanvas
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  echo > exports '{ global: Canvas*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries libraries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tkimg ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tkimg
  test -e build-stamp && echo >&3 "already done" && exit 0
  chmod a+x compat/libtiff/configure
  PATH=${PFX_HERE}/bin:$PATH
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --with-libjpeg=${HERE}/jpeg-turbo --enable-threads || exit 1
  make || exit 1
  make install-libraries INSTALL_ROOT=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

# trf wants the stub enabled zlib from tkimg

echo -n "build trf ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd trf
  # move compat/stdlib.h out of way
  test -f compat/stdlib.h && mv compat/stdlib.h compat/stdlib.h-DONTUSE
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-zlib-include-dir=$(pwd)/compat \
    --with-zlibtcl=${PFX_HERE}/lib --enable-builtin-bz2 \
    --with-ssl=${PFX_HERE} --enable-static-md5 \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Trf_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  perl -pi -e "s@-L${PFX}@-L${PFX_HERE}@g" Makefile
  perl -pi -e 's/libz\.so/libz.so.1/g' Makefile
  perl -pi -e 's/-DOPENSSL_SUB=1/-DOPENSSL_SUB=1 -DSSL_LIB_NAME=\\"libcrypto.so.10\\" -DCRYPT_LIB_NAME=\\"libcrypt.so.1\\"/g' Makefile
  # force use of compat/md2.h
  perl -pi -e 's/-DHAVE_MD2_H=1//g' Makefile
  make binaries libraries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  rm -f ${PFX_HERE}/lib/Trf*/lib*.a
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tktable ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tktable
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  echo > exports '{ global: Tktable_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  install -d ${PFX_HERE}/lib/Tktable2.11/demos
  install -m644 demos/*.tcl demos/*.gif ${PFX_HERE}/lib/Tktable2.11/demos
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tktreectrl ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tktreectrl
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  cp -rp demos ${PFX_HERE}/lib/treectrl2.4.2
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tkpath ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tkpath
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  cp -rp demos ${PFX_HERE}/lib/tkpath0.3.3
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build itk ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd itk
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --with-itcl=$(echo ${PFX_HERE}/lib/itcl*) || exit 1
  echo > exports '{ global: Itk_Init; Itk_SafeInit; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make LIBS="$(echo ${PFX_HERE}/lib/itcl*/libitclstub*.a)" || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build v4l2 ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd v4l2
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  # CentOS 5 lacks libudev so need to cheat here
  if ! test -r /usr/include/libudev.h ; then
    CFLAGS="-I${SCRIPTDIR}/compat -DHAVE_LIBUDEV=1" \
    DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
      --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix || exit 1
  else
    DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
      --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix || exit 1
  fi
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tkhtml ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tkhtml
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  echo > exports '{ global: Tkhtml_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build dbus-tcl ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd dbus-tcl
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix || exit 1
  echo > exports '{ global: Dbus_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build dbus-intf ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd dbus-intf
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix || exit 1
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tclx ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tclx
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix || exit 1
  echo > exports '{ global: Tclx_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build libdmtx ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd libdmtx/tcl
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  echo > exports '{ global: Dmtx_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build ZBar ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd ZBar/tcl
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i568-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  echo > exports '{ global: Zbar_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build zint ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd zint/backend_tcl
  test -e build-stamp && echo >&3 "already done" && exit 0
  chmod +x ./configure
  DESTDIR=${HERE} ./configure --build=i568-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  echo > exports '{ global: Zint_*Init; Zint_*Unload; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tcl-augeas ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tcl-augeas
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tktray ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tktray
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  echo > exports '{ global: Tktray_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build snack ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd snack/unix
  test -e build-stamp && echo >&3 "already done" && exit 0
  chmod +x ./configure
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads --enable-alsa || exit 1
  echo > exports '{ global: Snack_*Init; Sound_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  perl -pi -e "s@${PFX}/@${PFX_HERE}/@g" Makefile
  make all || exit 1
  make install exec_prefix=${PFX_HERE} prefix=${PFX_HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tkvnc ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tkvnc
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --with-zlib=${HERE}/zlib --enable-threads || exit 1
  echo > exports '{ global: Vnc_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tksvg ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tksvg
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build VecTcl ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd VecTcl
  test -e build-stamp && echo >&3 "already done" && exit 0
  # already preprocessed, no need to expand
  touch generic/vectcl.[ch] generic/compathack.[ch]
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Vectcl_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build VecTclTk ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd VecTcl/TkBridge
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} CFLAGS=-I${HERE}/VecTcl/generic LDFLAGS=-L${HERE}/VecTcl \
    ./configure --build=i586-linux-gnu --prefix=${PFX} \
      --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
      --with-vectcl=${PFX_HERE}/lib/vectcl0.3 --enable-threads || exit 1
  echo > exports '{ global: Vectcltk_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'"-L${HERE}/VecTcl -Wl,--version-script=$(pwd)/exports@g" \
    Makefile
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build WavReader ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd VecTcl/WavReader
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} CFLAGS=-I${HERE}/VecTcl/generic LDFLAGS=-L${HERE}/VecTcl \
    ./configure --build=i586-linux-gnu --prefix=${PFX} \
      --with-tcl=${HERE}/tcl/unix --with-vectcl=${PFX_HERE}/lib/vectcl0.3 \
      --enable-threads || exit 1
  echo > exports '{ global: Wavreader_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'"-L${HERE}/VecTcl -Wl,--version-script=$(pwd)/exports@g" \
    Makefile
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tclral ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tclral
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Ral_*Init; Ral_*Unload; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tclepeg ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tclepeg
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} CFLAGS="-I${HERE}/jpeg-turbo" \
    LDFLAGS="-L${HERE}/jpeg-turbo" \
      ./configure --build=i586-linux-gnu --prefix=${PFX} \
        --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Tclepeg_*Init; local: *; };'
  perl -pi -e 's@-Wl,--export-dynamic@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tcluvc ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tcluvc
  test -e build-stamp && echo >&3 "already done" && exit 0
  PATH=${PFX_HERE}/bin:$PATH
  # CentOS 5 lacks libudev so need to cheat here
  if ! test -r /usr/include/libudev.h ; then
    CFLAGS="-I${SCRIPTDIR}/compat -DHAVE_LIBUDEV=1 -DLIBUVC_HAVE_JPEG=1 -I${HERE}/jpeg-turbo" \
      LDFLAGS="-L${HERE}/jpeg-turbo" LIBS="-ljpeg" DESTDIR=${HERE} \
        ./configure --build=i586-linux-gnu --prefix=${PFX} \
          --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix || exit 1
  else
    CFLAGS="-DLIBUVC_HAVE_JPEG=1 -I${HERE}/jpeg-turbo" \
      LDFLAGS="-L${HERE}/jpeg-turbo" LIBS="-ljpeg" DESTDIR=${HERE} \
        ./configure --build=i586-linux-gnu --prefix=${PFX} \
          --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix || exit 1
  fi
  echo > exports '{ global: Tcluvc_*Init; local: *; };'
  perl -pi -e 's@-Wl,--export-dynamic@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build xotcl ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd xotcl
  test -e build-stamp && echo >&3 "already done" && exit 0
  CFLAGS="-I${HERE}/tdom/expat" ./configure --build=i586-linux-gnu \
    --prefix=${PFX} --with-tcl=${HERE}/tcl/unix --enable-threads \
    --disable-rpath --without-gdbm --without-aolserver \
    --with-actiweb=yes --with-xotclsh=no --with-xowish=no || exit 1
  echo > exports '{ global: Xotcl_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries libraries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  rm -f ${PFX_HERE}/lib/xotcl*/*.a
  rm -f ${PFX_HERE}/lib/xotcl*.sh
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build nsf ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd nsf
  test -e build-stamp && echo >&3 "already done" && exit 0
  ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads \
    --disable-rpath || exit 1
  echo > exports '{ global: Nsf_Init; Nsf_SafeInit; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries libraries || exit 1
  make install-binaries install-libraries install-xotcl-libraries \
    DESTDIR=${HERE} || exit 1
  rm -f ${PFX_HERE}/lib/nsf*/*.a
  rm -f ${PFX_HERE}/lib/nsf*.sh
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build libsocketcan ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd libsocketcan
  test -e build-stamp && echo >&3 "already done" && exit 0
  # CentOS 5 lacks some stuff in rtnetlink.h, so need to cheat here
  if ! grep -q IFLA_INFO_MAX /usr/include/linux/rtnetlink.h ; then
    CFLAGS="-I${SCRIPTDIR}/compat" \
      ./configure --build=i586-linux-gnu --prefix=${PFX} \
        --disable-static || exit 1
  else
    ./configure --build=i586-linux-gnu --prefix=${PFX} \
      --disable-static || exit 1
  fi
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tclcan ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tclcan
  test -e build-stamp && echo >&3 "already done" && exit 0
  ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build vu ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd vu
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  echo > exports '{ global: Vu_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build rl_json ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd rl_json
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Rl_json_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tkdnd ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tkdnd
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  echo > exports '{ global: Tkdnd_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make binaries || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build mpexpr ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd mpexpr/unix
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix || exit 1
  echo > exports '{ global: Mpexpr_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install prefix=${PFX_HERE} exec_prefix=${PFX_HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tclcsv ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tclcsv
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Tclcsv_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tkzinc ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tkzinc
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads --disable-shape --enable-gl || exit 1
  echo > exports '{ global: Tkzinc_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make package || exit 1
  make install-package install-demos DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build ffidl ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd ffidl
  test -e build-stamp && echo >&3 "already done" && exit 0
  mkdir -p lib-src && rm -rf lib-src/libffi && cp -rp ../libffi lib-src
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads \
    --enable-libffi || exit 1
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tcl-lmdb ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tcl-lmdb
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Lmdb_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build DiffUtilTcl ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd DiffUtilTcl
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Diffutil_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tclparser ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tclparser
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tclcompiler ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tclcompiler
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Tclcompiler_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build snap7 ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd snap7/build/unix
  test -e build-stamp && echo >&3 "already done" && exit 0
  make -f unix.mk || exit 1
  $STRIP ../bin/*.so || true
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build libmodbus ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd libmodbus
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --disable-tests --disable-static --enable-shared \
    --without-documentation || exit 1
  make || exit 1
  make install DESTDIR=${HERE} || exit 1
  $STRIP ${PFX_HERE}/lib/libmodbus.so || true
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build fswatch ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd fswatch
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tserialport ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tserialport
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Tserialport_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make prerequisite || exit 1
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build topcua ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd topcua
  test -e build-stamp && echo >&3 "already done" && exit 0
  CFLAGS="-std=c99" DESTDIR=${HERE} \
    ./configure --build=i586-linux-gnu --prefix=${PFX} \
      --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  echo > exports '{ global: Topcua_*Init; local: *; };'
  perl -pi -e 's@\$\{LDFLAGS_DEFAULT\}@'-Wl,--version-script=$(pwd)/exports'@g' \
    Makefile
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build parse_args ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd parse_args
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads || exit 1
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "build tkvlc ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd tkvlc
  test -e build-stamp && echo >&3 "already done" && exit 0
  DESTDIR=${HERE} ./configure --build=i586-linux-gnu --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --with-tk=${HERE}/sdl2tk/unix \
    --enable-threads || exit 1
  make || exit 1
  make install-binaries install-libraries DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

echo -n "strip binaries ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  set -x
  $STRIP ${PFX_HERE}/bin/tclsh* ${PFX_HERE}/bin/wish* ${PFX_HERE}/lib/*.so \
    ${PFX_HERE}/lib/*/*.so
  # fixup paths in installation tree
  perl -pi -e "s@${PFX_HERE}@${PFX}@g" ${PFX_HERE}/bin/*-config
  perl -pi -e "s@${HERE}/tcl/unix@${PFX}/lib@g" ${PFX_HERE}/bin/*-config
  perl -pi -e "s@${PFX_HERE}@${PFX}@g" ${PFX_HERE}/lib/*.sh
  set +x
  touch .fixup-stamp
  echo >&3 "done"
) || fail

echo -n "wrap single-file wish and tclsh binaries ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  set -x
  rm -rf assets
  mkdir -p assets
  cp -rp ${PFX_HERE}/lib/tcl8* assets
  cp -rp ${PFX_HERE}/lib/tk8* assets
  cp -rp ${PFX_HERE}/lib/itcl* assets
  cp -rp ${PFX_HERE}/lib/sqlite3* assets
  cp -rp ${PFX_HERE}/lib/tdbc* assets
  cp -rp ${PFX_HERE}/lib/thread* assets
  cp -rp ${PFX_HERE}/lib/udp* assets
  cp -rp ${PFX_HERE}/lib/tdom[0-9]* assets
  cp -rp ${PFX_HERE}/lib/tnc[0-9]* assets
  cp -rp ${PFX_HERE}/lib/vfs* assets
  cp -rp ${PFX_HERE}/lib/vqtcl* assets
  cp -rp ${PFX_HERE}/lib/tls* assets
  cp -rp ${PFX_HERE}/lib/trofs* assets
  cp -rp ${PFX_HERE}/lib/tbcload* assets
  cp -rp ${PFX_HERE}/lib/Memchan* assets
  cp -rp ${PFX_HERE}/lib/Trf* assets
  cp -rp ${PFX_HERE}/lib/TclCurl* assets
  # BLT demos from AndroWish assets
  cp -rp ${AWDIR}/assets/blt* assets
  cp -rp ${PFX_HERE}/lib/Canvas3d* assets
  cp -rp ${PFX_HERE}/lib/Img* assets
  cp -rp ${PFX_HERE}/lib/Tktable* assets
  cp -rp ${PFX_HERE}/lib/treectrl* assets
  cp -rp ${PFX_HERE}/lib/tkpath* assets
  cp -rp ${PFX_HERE}/lib/itk* assets
  if test -d ${PFX_HERE}/lib/v4l2* ; then
    cp -rp ${PFX_HERE}/lib/v4l2* assets
  fi
  cp -rp ${PFX_HERE}/lib/Tkhtml* assets
  if test -d ${PFX_HERE}/lib/tcluvc* ; then
    cp -rp ${PFX_HERE}/lib/tcluvc* assets
  fi
  cp -rp ${PFX_HERE}/lib/dbus* assets
  cp -rp ${PFX_HERE}/lib/tclx* assets
  cp -rp ${PFX_HERE}/lib/dmtx* assets
  cp -rp ${PFX_HERE}/lib/zbar* assets
  cp -rp ${PFX_HERE}/lib/zint* assets
  cp -rp ${PFX_HERE}/lib/augeas* assets
  cp -rp ${PFX_HERE}/lib/tktray* assets
  cp -rp ${PFX_HERE}/lib/snack* assets
  cp -rp ${PFX_HERE}/lib/vnc* assets
  cp -rp ${PFX_HERE}/lib/tksvg* assets
  # Tcl-only directories
  cp -rp ${AWDIR}/assets/bwidget* assets
  cp -rp ${AWDIR}/assets/classview* assets
  cp -rp ${AWDIR}/assets/fsdialog* assets
  cp -rp ${AWDIR}/assets/gridplus* assets
  cp -rp ${AWDIR}/assets/helpviewer* assets
  cp -rp ${AWDIR}/assets/icons* assets
  cp -rp ${AWDIR}/assets/iwidgets* assets
  cp -rp ${AWDIR}/assets/materialicons* assets
  cp -rp ${AWDIR}/assets/mqtt* assets
  cp -rp ${AWDIR}/assets/pdf4tcl* assets
  cp -rp ${AWDIR}/assets/tcllib* assets
  cp -rp ${AWDIR}/assets/tclsoap* assets
  cp -rp ${AWDIR}/assets/tclws* assets
  cp -rp ${AWDIR}/assets/tfirmata* assets
  cp -rp ${AWDIR}/assets/tile-extras* assets
  cp -rp ${AWDIR}/assets/tkbugz assets
  cp -rp ${AWDIR}/assets/tkcon2* assets
  cp -rp ${AWDIR}/assets/tkconclient* assets
  cp -rp ${AWDIR}/assets/tkinspect* assets
  cp -rp ${AWDIR}/assets/tklib* assets
  cp -rp ${AWDIR}/assets/TkMC* assets
  cp -rp ${AWDIR}/assets/tksqlite* assets
  cp -rp ${AWDIR}/assets/twdbgi* assets
  cp -rp ${AWDIR}/assets/upnp* assets
  cp -rp ${AWDIR}/assets/ukaz* assets
  cp -rp ${AWDIR}/assets/wibble* assets
  cp -rp ${AWDIR}/assets/stardom* assets
  cp -rp ${AWDIR}/assets/modbus* assets
  cp -p ${PFX_HERE}/lib/libmodbus.so assets/modbus*
  cp -rp ${AWDIR}/assets/snap7* assets
  cp -p snap7/build/bin/libsnap7* assets/snap7*
  # BLT fixups
  cp -p ${PFX_HERE}/lib/blt*/libBLT* assets/blt*
  cp -p ${PFX_HERE}/lib/blt*/pkgIndex* assets/blt*
  # some demos
  cp -p ${AWDIR}/assets/tkimg*/demo.tcl assets/Img*
  cp -rp ${AWDIR}/assets/Canvas3d*/demo assets/Canvas3d*
  if test -d assets/v4l2* ; then
    cp -rp ${HERE}/v4l2*/demos assets/v4l2*
  fi
  if test -d assets/tcluvc* ; then
    cp -rp ${HERE}/tcluvc*/demos assets/tcluvc*
  fi
  cp -rp ${PFX_HERE}/lib/vectcl0* assets
  cp -rp ${PFX_HERE}/lib/vectcltk0* assets
  cp -rp ${AWDIR}/assets/vectcl*/demo assets/vectcl0*
  cp -rp ${PFX_HERE}/lib/WavReader* assets
  cp -rp ${AWDIR}/assets/VecTcLab* assets
  cp -rp ${PFX_HERE}/lib/ral* assets
  cp -rp ${AWDIR}/assets/ralutil* assets
  cp -rp ${PFX_HERE}/lib/tclepeg* assets
  cp -rp ${PFX_HERE}/lib/xotcl* assets
  cp -rp ${PFX_HERE}/lib/nsf* assets
  cp -rp ${PFX_HERE}/lib/tclcan* assets
  cp -p ${PFX_HERE}/lib/libsocketcan.so.2 assets/tclcan*
  cp -rp ${PFX_HERE}/lib/vu* assets
  cp -rp ${PFX_HERE}/lib/rl_json* assets
  cp -rp ${PFX_HERE}/lib/tkdnd* assets
  cp -rp ${PFX_HERE}/lib/Mpexpr* assets
  cp -rp ${PFX_HERE}/lib/tclcsv* assets
  cp -rp ${PFX_HERE}/lib/Tkzinc* assets
  cp -rp ${PFX_HERE}/lib/Ffidl* assets
  cp -rp ${PFX_HERE}/lib/lmdb* assets
  cp -rp ${PFX_HERE}/lib/DiffUtil* assets
  cp -rp ${PFX_HERE}/lib/parser* assets
  cp -rp ${PFX_HERE}/lib/tclcompiler* assets
  cp -rp ${PFX_HERE}/lib/fswatch* assets
  cp -rp ${PFX_HERE}/lib/tserialport* assets
  cp -rp ${PFX_HERE}/lib/topcua* assets
  cp -rp ${PFX_HERE}/lib/parse_args* assets
  cp -rp ${PFX_HERE}/lib/tkvlc* assets
  # add stripped down TDK
  cp -rp ${AWDIR}/undroid/TDK assets
  cp -rp ${AWDIR}/undroid/mkappimg assets
  # add shortcuts providing builtin:widget, builtin:tksqlite, etc.
  (
    cd assets
    echo > widget \
      'source [file dirname [info script]]/'$(echo tk8*)'/demos/widget'
    echo > tksqlite \
      'source [file dirname [info script]]/'$(echo tksqlite*)'/tksqlite.tcl'
    echo > imgdemo \
      'source [file dirname [info script]]/'$(echo Img*)'/demo.tcl'
    echo > tkpdemo \
      'source [file dirname [info script]]/'$(echo tkpath*)'/demos/all.tcl'
    echo > 3ddemo \
      'source [file dirname [info script]]/'$(echo Canvas3d*)'/demo/shapes.tcl'
    echo > tkcon \
      'source [file dirname [info script]]/'$(echo tkcon2*)'/tkcon.tcl'
    echo >> tkcon 'eval ::tkcon::Init $argv'
    echo > tkinspect \
      'source [file dirname [info script]]/'$(echo tkinspect*)'/tkinspect.tcl'
    echo > treectrl \
      'source [file dirname [info script]]/'$(echo treectrl*)'/demos/demo.tcl'
    echo > tktable \
      'source [file dirname [info script]]/'$(echo Tktable*)'/demos/spreadsheet.tcl'
    echo > bugz 'source [file dirname [info script]]/tkbugz/tk_bugz.tcl'
    echo > zint \
      'source [file dirname [info script]]/'$(echo zint*)'/demo.tcl'
    echo > dungfork \
      'source [file dirname [info script]]/'$(echo augeas*)'/dungfork.tcl'
    echo > vncviewer \
      'source [file dirname [info script]]/'$(echo vnc*)'/vncviewer.tcl'
    echo > vectclab \
      'source [file dirname [info script]]/'$(echo VecTcLab*)'/vectclab.tcl'
    echo > tkmc \
      'source [file dirname [info script]]/'$(echo TkMC*)'/tkmc.tcl'
    echo > helpviewer \
      'source [file dirname [info script]]/'$(echo helpviewer*/main.tcl)
    echo > zinc-widget \
      'source [file dirname [info script]]/'$(echo Tkzinc*/demos/zinc-widget)
    echo > stardom \
      'source [file dirname [info script]]/'$(echo stardom*/stardom.tcl)
  )
  # add tkchat
  if test -d ${AWDIR}/assets/tkchat* ; then
    cp -rp ${AWDIR}/assets/tkchat* assets
    (
      cd assets
      echo > tkchat \
        'source [file dirname [info script]]/'$(echo tkchat*/tkchat.tcl)
    )
  fi
  # add notebook
  if test -d ${AWDIR}/assets/notebook2.2 ; then
    cp -rp ${AWDIR}/assets/notebook2.2 assets
    echo > assets/notebook \
      'source [file dirname [info script]]/notebook2.2/notebook.tcl'
  fi
  # add sdx
  cp -rp ${HERE}/tclkit/sdx/lib/app-sdx assets/app-sdx2.0
  cp -rp ${HERE}/tclkit/sdx/lib/sdx assets/sdx1.0
  cp -rp ${HERE}/tclkit/sdx/lib/starsync assets/starsync1.0
  cp -rp ${HERE}/tclkit/sdx/lib/stringfileinfo assets/stringfileinfo0.2
  echo > assets/sdx 'package require starkit ; package require app-sdx'
  if test -d ${AWDIR}/undroid/ttk-themes ; then
    cp -rp ${AWDIR}/undroid/ttk-themes assets
  fi
  # add mpksc
  cp -p ${PFX_HERE}/bin/mpksc assets
  # now use same assets for tclsh
  rm -rf tassets
  cp -rp assets tassets
  # add libtk*.so
  cp -p ${PFX_HERE}/lib/libtk8.6.so tassets/tk8.6
  # fixup .../lib/tk8.6/pkgIndex.tcl for libtk*.so
  cat > tassets/tk8.6/pkgIndex.tcl <<'EOD'
if {[catch {package present Tcl 8.6}]} {return}
package ifneeded Tk 8.6.9 [list load [file join $dir \
    libtk8.6[info sharedlibextension]] Tk]
EOD
  # build ZIP to be embedded for vanillatclsh
  rm -f tassets.zip
  ( cd tassets ; rm -f */*.a ; zip -rq ../tassets.zip * )
  # now wrap to single-file binary vanillatclsh
  cp -p ${PFX_HERE}/bin/tclsh8.6 vanillatclsh || exit 1
  cat tassets.zip >> vanillatclsh || exit 1
  # fixup .../lib/tk8.6/pkgIndex.tcl since we have static wish
  cat > assets/tk8.6/pkgIndex.tcl <<'EOD'
if {[catch {package present Tcl 8.6}]} {return}
package ifneeded Tk 8.6.9 {load {} Tk}
EOD
  # build ZIP to be embedded for vanillawish
  rm -f assets.zip
  ( cd assets ; rm -f */*.a ; zip -rq ../assets.zip * )
  # now wrap to single-file binary vanillawish
  cp -p ${PFX_HERE}/bin/wish.static vanillawish || exit 1
  cat assets.zip >> vanillawish || exit 1
  set +x
  echo >&3 "done"
) || fail

echo -e >> build.log \
  "\n***** build ended on $(date '+%Y-%m-%d %H:%M:%S') *****\n"

echo
echo "You should now have a new vanilla{tclsh,wish}, happy tcl'ing."
echo
ls -l vanillatclsh vanillawish

#############################################################################
# EOF
