#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-ncftp

VERSION=3.1.9
ARCH=${ARCH:-i486}
BUILD=1

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf ncftp-$VERSION
tar xjvf $CWD/ncftp-$VERSION-src.tar.bz2
cd ncftp-$VERSION
chown -R root.root .
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  $ARCH-slackware-linux
make
mkdir -p $PKG/usr/bin
( cd bin
  for file in ncftp ncftpbatch ncftpbookmarks ncftpget ncftpls ncftpput ; do
    strip --strip-unneeded $file
    cat $file > $PKG/usr/bin/$file
  done
)
( cd $PKG/usr/bin
  rm -f ncftpspooler
  ln -sf ncftpbatch ncftpspooler
  chmod 755 *
  chown -R root.bin .
)
mkdir -p $PKG/usr/man/man1
( cd doc/man
  for file in *.1 ; do
    cat $file | gzip -9c > $PKG/usr/man/man1/$file.gz
  done )
mkdir -p $PKG/usr/doc/ncftp-$VERSION
cp -a doc/* $PKG/usr/doc/ncftp-$VERSION
# Useless redundancy:
rm -r $PKG/usr/doc/ncftp-$VERSION/man \
      $PKG/usr/doc/ncftp-$VERSION/html

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/ncftp-$VERSION-$ARCH-$BUILD.tgz

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