#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-zoneinfo
SRC=/devel/manpagesrc
INFO=$PKG/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
explodepkg $CWD/_zoneinfo.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 "| timezone utils |"
echo "+================+"
cd $TMP
mkdir build
cd build
tar xzvf $CWD/tzcode95g.tar.gz
tar xzvf $CWD/tzdata95k.tar.gz
zcat $CWD/tzcode95g.diff.gz | patch
make
strip zic zdump
cat zic > $PKG/usr/bin/zic
cat zdump > $PKG/usr/bin/zdump
cat yearistype > $PKG/usr/bin/yearistype
for file in *.1 ; do
  gzip -9c $file > $PKG/usr/man/man1/$file.gz  
done
for file in *.3 ; do
  gzip -9c $file > $PKG/usr/man/man3/$file.gz  
done
for file in *.5 ; do
  gzip -9c $file > $PKG/usr/man/man5/$file.gz  
done
for file in *.8 ; do
  gzip -9c $file > $PKG/usr/man/man8/$file.gz  
done
echo "+===================+"
echo "| timezone database |"
echo "+===================+"
# This patch makes all the links under /usr/lib/zoneinfo symlinks.  They'll
# be all screwed up, but it won't matter since the correct links are in the
# install script.  Basically, creating these as symlinks just makes them a 
# lot easier to find and delete. :^)
zcat $CWD/zic-symlink.diff.gz | patch
make zic
for file in africa antarctica asia australasia etcetera europe \
factory northamerica pacificnew solar87 solar88 solar89 southamerica \
systemv leapseconds backward ; do
  ./zic -d $PKG/usr/lib/zoneinfo $file
done
# We already have these in the installation script, so buh-bye:
find $PKG/usr/lib/zoneinfo -type l -print -exec rm {} \;

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

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