#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-tcl
SRC=/devel/manpagesrc
INFO=/devel/info-pages/usr/info
TEX=/devel/texinfo-docs

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
tar xzvf $CWD/_tcl.tar.gz

# Function to handle manpage source:
man2gz () { # $1 is source page name, $2 is target name for preformatted
            # output (full path && name) and $3 is the same, but for the
            # source.
  mkdir -p `dirname $2`
  groff -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+==============+"
echo "| tcl7.3l1-src |"
echo "+==============+"
cd $TMP
tar xzvf $CWD/tcl7.3l1-src.tar.gz
cd tcl7.3
./configure --prefix=/usr
make
cat tclsh > $PKG/usr/bin/tclsh
cat libtcl.a > $PKG/usr/lib/libtcl.a
cat libtcl.sa > $PKG/usr/lib/libtcl.sa
cat libtcl.so.3.1.0 > $PKG/usr/lib/libtcl.so.3.1.0
cd doc
man2gz tclsh.1 $PKG/usr/man/preformat/cat1/tclsh.1.gz $SRC/usr/man/man1/tclsh.1
for file in *.3 ; do
  SHORT=`basename $file .3`
  man2gz $file $PKG/usr/man/preformat/$SHORT.3tcl.gz $SRC/usr/man/man3/$SHORT.3tcl
done
for file in *.n ; do
  SHORT=`basename $file .n`
  man2gz $file $PKG/usr/man/preformat/$SHORT.3tcl.gz $SRC/usr/man/man3/$SHORT.3tcl
done

# Build the package:
cd $PKG
tar czvf $TMP/tcl.tgz .

# Now let's build the tk package...

PKG=$TMP/package-tk

if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
tar xzvf $CWD/_tk.tar.gz

echo "+===============+"
echo "| tk3.6p1l1-src |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/tk3.6p1l1-src.tar.gz
cd tk3.6
./configure --prefix=/usr
make
cat wish > $PKG/usr/bin/wish
cat libtk.a > $PKG/usr/lib/libtk.a
cat libtk.sa > $PKG/usr/lib/libtk.sa
cat libtk.so.3.1.2 > $PKG/usr/lib/libtk.so.3.1.2
cd doc
man2gz wish.1 $PKG/usr/man/preformat/cat1/wish.1.gz $SRC/usr/man/man1/wish.1.gz
for file in *.3 ; do
  SHORT=`basename $file .3`
  man2gz $file $PKG/usr/man/preformat/$SHORT.3tk.gz $SRC/usr/man/man3/$SHORT.3tk
done
for file in *.n ; do
  SHORT=`basename $file .n`
  man2gz $file $PKG/usr/man/preformat/$SHORT.3tk.gz $SRC/usr/man/man3/$SHORT.3tk
done

# Build the package:
cd $PKG
tar czvf $TMP/tk.tgz .

# Now we need to build the tclX package... the source for these is sort of
# related...

PKG=$TMP/package-tclx

if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
tar xzvf $CWD/_tclx.tar.gz

echo "+==========+"
echo "| tclX7.3b |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/tclX7.3bl1-src.tar.gz
cd tclX7.3b
./configure --prefix=/usr
make
( cd src ; make shared )
( cd tksrc ; make shared )
strip tclmaster/bin/tcl
cat tclmaster/bin/tcl > $PKG/usr/bin/tcl
strip tkmaster/bin/wishx
cat tkmaster/bin/wishx > $PKG/usr/bin/wishx
cat tclmaster/lib/libtcl.a > $PKG/usr/lib/libtcl.a
cat tclmaster/lib/libtclx.a > $PKG/usr/lib/libtclx.a
cat src/libtclx.sa > $PKG/usr/lib/libtclx.sa
cat src/libtclx.so.3.2.0 > $PKG/usr/lib/libtclx.so.3.2.0
cat tksrc/libtkx.sa > $PKG/usr/lib/libtkx.sa
cat tksrc/libtkx.so.3.2.0 > $PKG/usr/lib/libtkx.so.3.2.0

#man2gz tclsh.1 $PKG/usr/man/preformat/cat1/tclsh.1.gz $SRC/usr/man/man1/tclsh.1
#for file in *.3 ; do
#  SHORT=`basename $file .3`
#  man2gz $file $PKG/usr/man/preformat/$SHORT.3tcl.gz $SRC/usr/man/man3/$SHORT.3tcl
#done
#for file in *.n ; do
#  SHORT=`basename $file .n`
#  man2gz $file $PKG/usr/man/preformat/$SHORT.3tcl.gz $SRC/usr/man/man3/$SHORT.3tcl
#done

# Build the package:
cd $PKG
tar czvf $TMP/tclx.tgz .

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/tclX7.3b
  rm -rf $TMP/tcl7.3
  rm -rf $TMP/tk3.6
  rm -rf $PKG
fi
