#!/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 "| ncurses-1.8.5 |"
echo "+===============+"
cd $TMP
#tar xzvf $CWD/ncurses-1.8.6.tar.gz
tar xzvf $CWD/ncurses-1.8.5.tar.gz
#cd ncurses-1.8.6
cd ncurses-1.8.5
zcat $CWD/ncurses-1.8.6.diff.gz | patch -p1
zcat $CWD/ncurses-1.8.5-patch.gz | patch -p1
cd src
Configure
make
cat libdcurses.a > $PKG/usr/lib/libdcurses.a
cat libncurses.a > $PKG/usr/lib/libncurses.a
strip tic untic
cat tic > $PKG/usr/bin/tic
cat untic > $PKG/usr/bin/untic
cd ../man
for file in tic.1m untic.1m ; do
  cat $file | gzip -9c > $PKG/usr/man/man1/$file.gz
done
for file in *.3x ; do
  cat $file | gzip -9c > $PKG/usr/man/man3/$file.gz
done
for file in term.5 terminfo.5 ; do
  cat $file | gzip -9c > $PKG/usr/man/man5/$file.gz
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 $TMP/ncurses-1.8.5
  rm -rf $PKG
fi
