# Build/install Tcl the way Slackware's binary package is made:
CWD=`pwd`

VERSION=8.4.6
# See also version number 8.4 in the symlinks below...)
ARCH=${ARCH:-i486}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

cd /tmp
tar xzvf $CWD/tcl$VERSION-src.tar.gz
cd tcl$VERSION
chown -R root.root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
mkdir -p /usr/doc/tcl$VERSION
cp -a README license.terms /usr/doc/tcl$VERSION
cd unix
CFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr --disable-shared
make
make install
make clean
CFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr --enable-shared
make

# Fix unexpanded ${TCL_DBGX} variables strewn throughout tclConfig.sh:
cat tclConfig.sh | sed -e s/\@LDFLAGS_DEBUG\@//g | sed -e s/\@LDFLAGS_OPTIMIZE\@//g | sed -e s/\$\{TCL_DBGX\}//g > tmpfile
mv tmpfile tclConfig.sh
# Make a script to produce symlinks to gzipped man pages (used when packing later):
cat mkLinks | perl -pi -e 's|\.3|foo3foo3|' | perl -pi -e 's|\.3|foo3foo3|' | \
  perl -pi -e 's|foo3foo3|.3.gz|' | perl -pi -e 's|foo3foo3|.3.gz|' | \
  perl -pi -e 's|\.n|foonfoon|' | perl -pi -e 's|\.n|foonfoon|' | \
  perl -pi -e 's|foonfoon|.n.gz|' | perl -pi -e 's|foonfoon|.n.gz|' | \
  perl -pi -e 's|ln |ln -sf |' > $CWD/mkgzLinks.sh

# Briefly, to use the above generated script, you go into say /usr/man/man3/
# of the extracted package and do this:
# mkdir tmp
# cp *.* tmp
# mv tmp/* .
# rm -r tmp
# gzip -9 *
# sh /path/to/mkgzLinks.sh .

make install

( cd /usr/bin
  rm -f tclsh
  ln -sf tclsh8.4 tclsh )
( cd /usr/lib
  rm -f libtcl.a libtcl.so libtclstub.a
  ln -sf libtcl8.4.a libtcl.a
  ln -sf libtcl8.4.so libtcl.so
  ln -sf libtclstub8.4.a libtclstub.a )

mkdir -p /install
cat $CWD/slack-desc > /install/slack-desc
