#!/bin/bash

# ncurses.SlackBuild
# Almost entirely the original build script for Slackware Linux by Patrick Volkerding
# Modified by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# 1-Mar-2004

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/l/ncurses
export PORTCWD=$PWD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     SLKCFLAGS="-march=armv3 -mtune=xscale" ;;
   powerpc) SLKCFLAGS="" ;;
   sparc)   SLKCFLAGS="" ;;
   *)       SLKCFLAGS="" ;;
esac

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete and re-create temporary directories

# Untar source:
tar zxvvf $CWD/$PACKAGE-$VERSION.tar.gz
cd $PACKAGE-$VERSION
slackhousekeeping

ncurses_configure() {
CPPFLAGS="$SLKCFLAGS" \
CFLAGS="$SLKCFLAGS" \
./configure $WIDEC \
  --prefix=/usr \
  --with-gpm \
  --disable-termcap \
  --with-normal \
  --with-shared \
  --enable-symlinks \
  --without-debug \
  --without-profile \
  --without-ada \
  --program-prefix="" \
  --program-suffix="" \
  $ARCH-slackware-linux || failconfig
}

# Configure & compile normally:
ncurses_configure
make || failmake
make install
make install DESTDIR=$PKG

# Build/install again with wide character support:
make clean
WIDEC=--enable-widec
ncurses_configure
make || failmake
make install
make install DESTDIR=$PKG

# Move the include files from /usr/include into
# /usr/include/ncurses, then make symlinks back
# into /usr/include.
( cd $PKG/usr/include
  rm -rf ncurses
  mkdir ncurses
  mv *.h ncurses
  for file in ncurses/* ; do
    ln -sf $file .
  done
  # This shouldn't clobber the real one:
  mv termcap.h termcap-ncurses.h )

# Move the ncurses libraries into /lib, since they're important:
mkdir -p $PKG/lib
( cd $PKG/usr/lib
  chmod 755 *.so
  chmod 644 *.a
  mv libncurses.so.5* $PKG/lib
  mv libncursesw.so.5* $PKG/lib
  rm -f libncurses.so
  ln -sf /lib/libncurses.so.5 libncurses.so
  rm -f libncursesw.so
  ln -sf /lib/libncursesw.so.5 libncursesw.so
  # Olde obsolete names, just in case
  rm -f libcurses.so
  ln -sf libncurses.so libcurses.so
  rm -f libcursesw.so
  ln -sf libncursesw.so libcursesw.so
)

# Set TERMINFO to point to the package:
export TERMINFO=$PKG/usr/share/terminfo

# Fix the xterm entries:
if [ -r /usr/X11R6/lib/X11/etc/xterm.terminfo ]; then
  progs/tic -v /usr/X11R6/lib/X11/etc/xterm.terminfo
else
  echo "WARNING: /usr/X11R6/lib/X11/etc/xterm.terminfo not found"
  sleep 10
fi

# Update screen entry:
progs/tic -v $CWD/screeninfo.src

# Update rxvt entry:
progs/tic -v $CWD/rxvt.terminfo

# Update Eterm entry:
progs/tic -v $CWD/Eterm.ti

unset TERMINFO

# Then, add /usr/doc/ncurses-xxxxx with appropriate files.
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a ANNOUNCE INSTALL MANIFEST NEWS README* TO-DO \
      $PKG/usr/doc/$PACKAGE-$VERSION
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION/c++
cp -a  c++/NEWS c++/PROBLEMS c++/README-first \
       $PKG/usr/doc/$PACKAGE-$VERSION/c++

# Enter the package's root:
cd $PKG

# Slackware policies:
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # set all files to root.root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   . $PORTCWD/arm/pkger
 else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
