#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-flex
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/_flex.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 "| flex-2.4.7 |"
echo "+============+"
cd $TMP
tar xzvf $CWD/flex-2.4.7.tar.gz
cd flex-2.4.7
configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cat flex > $PKG/usr/bin/flex
cat FlexLexer.h > $PKG/usr/include/FlexLexer.h
cat libfl.a > $PKG/usr/lib/libfl.a
man2gz flex.1 $PKG/usr/man/preformat/cat1/flex.1.gz $SRC/usr/man/man1/flex.1
man2gz flexdoc.1 $PKG/usr/man/preformat/cat1/flexdoc.1.gz $SRC/usr/man/man1/flexdoc.1
cd MISC
makeinfo flex.texinfo
cat flex.info | gzip -9c > $INFO/flex.info.gz
cat flex.info-1 | gzip -9c > $INFO/flex.info-1.gz
cat flex.info-2 | gzip -9c > $INFO/flex.info-2.gz
cp flex.texinfo $TEX/flex.texi

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

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