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

VERSION=${PKGVERSION:-4.60}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-3}

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

cd $TMP
rm -rf nmap-$VERSION
tar xjvf $CWD/nmap-$VERSION.tar.bz2 || exit 1
cd nmap-$VERSION

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

CFLAGS=-O2 \
./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --build=$ARCH-slackware-linux

make -j4 || exit 1

make install DESTDIR=$PKG || exit 1

# --mandir seems to be working great
if [ -d $PKG/usr/share/man ]; then
  mv $PKG/usr/share/man $PKG/usr
fi

# This thing also has its head up its ass with regards to DESTDIR.
# We will simply kill it.
rm -f $PKG/usr/bin/uninstall_zenmap

mkdir -p $PKG/usr/share/applications
zcat $CWD/zenmap.desktop.gz > $PKG/usr/share/applications/zenmap.desktop
mkdir -p $PKG/usr/share/pixmaps
cat $CWD/nmap.png > $PKG/usr/share/pixmaps/nmap.png
mkdir -p $PKG/usr/doc/nmap-$VERSION
cp -a \
  COPYING COPYING.OpenSSL HACKING INSTALL \
  $PKG/usr/doc/nmap-$VERSION
( cd docs
  # It looks like all of these translations are gone from the 4.00+ tarball, and
  # most of them are not available on the web site.  Sorry -- I didn't do it.
  if [ -f nmap_german.1 ] ; then
    mkdir -p $PKG/usr/man/de/man1
    cat nmap_german.1 | gzip -9c > $PKG/usr/man/de/man1/nmap.1.gz
  fi
  if [ -f nmap_spanish.1 ] ; then
    mkdir -p $PKG/usr/man/es/man1
    cat nmap_spanish.1 | gzip -9c > $PKG/usr/man/es/man1/nmap.1.gz
  fi
  if [ -f nmap_french.1 ] ; then
    mkdir -p $PKG/usr/man/fr/man1
    cat nmap_french.1 | gzip -9c > $PKG/usr/man/fr/man1/nmap.1.gz
  fi
  if [ -f nmap_italian.1 ] ; then
    mkdir -p $PKG/usr/man/it/man1
    cat nmap_italian.1 | gzip -9c > $PKG/usr/man/it/man1/nmap.1.gz
  fi
  if [ -f nmap_lithuanian.1 ] ; then
    mkdir -p $PKG/usr/man/lt/man1
    cat nmap_lithuanian.1 | gzip -9c > $PKG/usr/man/lt/man1/nmap.1.gz
  fi
  if [ -f nmap_portuguese.1 ] ; then
    mkdir -p $PKG/usr/man/pt/man1
    cat nmap_portuguese.1 | gzip -9c > $PKG/usr/man/pt/man1/nmap.1.gz
  fi
  if [ -f nmap_russian.1 ] ; then
    mkdir -p $PKG/usr/man/ru/man1
    cat nmap_russian.1 | gzip -9c > $PKG/usr/man/ru/man1/nmap.1.gz
  fi
)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Strip binaries:
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null
)

# Compress and link manpages, if any:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

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