#!/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.4 |"
echo "+========+"
cd $TMP
tar xzvf $CWD/tcl7.4.tar.gz
cd tcl7.4
zcat $CWD/tcl7.4.diff.gz | patch -p1
./configure --prefix=/usr
make
cat tclsh > $PKG/usr/bin/tclsh
cat libtcl.a > $PKG/usr/lib/libtcl.a
cat libtcl.so.1.7.4 > $PKG/usr/lib/libtcl.so.1.7.4
cd doc
mkdir -p $PKG/usr/man/man{1,3}
cp tclsh.1 $PKG/usr/man/man1/tclsh.1
cp tclsh.1 $SRC/usr/man/man1/tclsh.1
for file in *.3 ; do
  cp $file $PKG/usr/man/man3/$file
  cp $file $SRC/usr/man/man3/$file
done
for file in *.n ; do
  SHORT=`basename $file .n`
  cp $file $PKG/usr/man/man3/$SHORT.3tcl
  cp $file $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 "| tk4.0 |"
echo "+=======+"
cd $TMP
tar xzvf $CWD/tk4.0.tar.gz
cd tk4.0
zcat $CWD/tk4.0.diff.gz | patch -p1
./configure --prefix=/usr
make
cat libtk.a > $PKG/usr/lib/libtk.a
cat libtk.so.1.4.0 > $PKG/usr/lib/libtk.so.1.4.0
rm libtk.a
cc -O2 -I. -I./../tcl7.4 -I/usr/X11R6/include -DHAVE_UNISTD_H=1 \
-DHAVE_LIMITS_H=1 -DSTDC_HEADERS=1 -DTK_FILE_READ_PTR=1 \
-DTK_LIBRARY=\"/usr/lib/tk4.0\" -fPIC tkAppInit.o -L. \
-ltk -L../tcl7.4 -ltcl -L/usr/X11R6/lib -lX11  -lieee -lm -o wish
cat wish > $PKG/usr/bin/wish
cp -a library $PKG/usr/lib/tk4.0
( cd $PKG/usr/lib/tk4.0 ; chown -R root.root * )
( cd $PKG/usr/lib/tk4.0 ; chmod -R 644 * )
( cd $PKG/usr/lib/tk4.0/demos ; chmod 755 browse hello ixset rmt \
  rolodex square tcolor timer widget )
cd doc
mkdir -p $PKG/usr/man/man{1,3}
cp wish.1 $PKG/usr/man/man1/wish.1
cp wish.1 $SRC/usr/man/man1/wish.1
for file in *.3 ; do
  SHORT=`basename $file .3`
  cp $file $PKG/usr/man/man3/$SHORT.3tk
  cp $file $SRC/usr/man/man3/$SHORT.3tk
done
for file in *.n ; do
  SHORT=`basename $file .n`
  cp $file $PKG/usr/man/man3/$SHORT.3tk
  cp $file $SRC/usr/man/man3/$SHORT.3tk
done

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

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/tcl7.4
  rm -rf $TMP/tk4.0
  rm -rf $PKG
fi
