#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-f2c
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
explodepkg $CWD/_f2c.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 "| libf2c-0.11 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/libf2c-0.11.tar.gz
cd libf2c
cat f2c.h > /usr/include/f2c.h
make.clean
#make.shared
make.elf
make.clean
make.static
cat libf2c.a > $PKG/usr/lib/libf2c.a
#cat libf2c.sa > $PKG/usr/lib/libf2c.sa
rm -f $PKG/usr/lib/libf2c.sa
cat libf2c.so.0.11 > $PKG/usr/lib/libf2c.so.0.11
echo "+=====+"
echo "| f2c |"
echo "+=====+"
cd $TMP
tar xzvf $CWD/f2c.tar.gz
cd f2c
zcat $CWD/f2c.diff.gz | patch
zcat $CWD/f2c.malloc.diff.gz | patch
cat f2c.1t | gzip -9c > $PKG/usr/man/man1/f2c.1.gz
cat f2c.h > $PKG/usr/include/f2c.h
cd src
make
strip f2c
cat f2c > $PKG/usr/bin/f2c
( cd $PKG/usr/bin ; zcat $CWD/f77.diff.gz | patch )
( cd $PKG/usr/bin ; rm f77.orig )

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

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