#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-ncurses
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/_ncurses.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 "| ncurses-1.8.6 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/ncurses-1.8.6.tar.gz
cd ncurses-1.8.6
zcat $CWD/ncurses-1.8.6.diff.gz | patch -p1
zcat $CWD/ncurses-1.8.5-patch.gz | patch -p1
cd src
make
cat libdcurses.a > $PKG/usr/lib/libdcurses.a
cat libncurses.a > $PKG/usr/lib/libncurses.a
strip tic infocmp
cat tic > $PKG/usr/bin/tic
cat infocmp > $PKG/usr/bin/infocmp
cd ../man
man2gz captoinfo.1m $PKG/usr/man/preformat/cat1/captoinfo.1m.gz $SRC/usr/man/man1/captoinfo.1m
man2gz infocmp.1m $PKG/usr/man/preformat/cat1/infocmp.1m.gz $SRC/usr/man/man1/infocmp.1m
man2gz tic.1m $PKG/usr/man/preformat/cat1/tic.1m.gz $SRC/usr/man/man1/tic.1m
#man2gz tput.1 $PKG/usr/man/preformat/cat1/tput.1.gz $SRC/usr/man/man1/tput.1
man2gz term.5 $PKG/usr/man/preformat/cat5/term.5.gz $SRC/usr/man/man5/term.5
man2gz terminfo.5 $PKG/usr/man/preformat/cat5/terminfo.5.gz $SRC/usr/man/man5/terminfo.5
for file in *.3x ; do
  man2gz $file $PKG/usr/man/preformat/cat3/$file.gz $SRC/usr/man/man3/$file
done

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

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