#!/bin/bash
#  Copyright (C) 2010  Thomas Bourdon, <tb@halpanet.org>
#  Copyright (C) 2011  Matias A. Fonzo, <selk@dragora.org>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

P=portmap

# Get the latest version of the tarball:
if [[ -z $V ]]; then
  printf -v V "%s" ""*.tar.lz""
  V=${V##*-}
  V=${V%.tar.*}
fi

B=${B:-1}

CWD=$(pwd)
TMP=${TMP:-/tmp/sources}
OUT=${OUT:-/tmp/packages}

# The architecture (ARCH) can be exported:
ARCH=${ARCH:-i486}

# Flags for the compiler (according to the ARCH):
case "$ARCH" in
  i486)
    DCFLAGS="-O2 -march=i486 -mtune=i686";
    ;;
  x86_64)
    DCFLAGS="-O2 -mtune=generic"
    libSuffix=64
    ;;
  *)
    DCFLAGS="$DCFLAGS"
    libSuffix="$libSuffix"
    ;;
esac

PKG=${TMP}/package-${P}

rm -rf $PKG
mkdir -p $PKG $OUT

echo "Uncompressing the tarball..."
rm -rf ${TMP}/${P}_${V}
tar -xvf ${CWD}/${P}-${V}.tar.lz -C "$TMP"

cd ${TMP}/${P}_${V}

# Set sane ownerships and permissions:
chown -R 0:0 .
find . \
  \( -perm 2777 -o -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \
  \) -exec chmod 755 {} + \
  -o \
  \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \
  \) -exec chmod 644 {} + 

export CFLAGS="$DCFLAGS"

make;

strip --strip-unneeded portmap pmap_dump pmap_set
install -m 0750 portmap -D ${PKG}/sbin/rpc.portmap
install -m 0750 pmap_dump -D ${PKG}/usr/sbin/pmap_dump
install -m 0750 pmap_set -D ${PKG}/usr/sbin/pmap_set

# Strip binaries, libraries and archives
find $PKG -type f | xargs file | awk '/ELF/ && /executable/ || /shared object/' | \
 cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || :

find $PKG -type f | xargs file | grep "current ar archive" | \
 cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null || :

# Add init script:
install -m 0755 ${CWD}/runit/run -D ${PKG}/etc/sv/portmap/run.new
install -d ${PKG}/etc/runit/runsvdir/default
( cd ${PKG}/etc/runit/runsvdir/default
  ln -sf /etc/sv/portmap .
)

# Include manpages:
mkdir -p ${PKG}/usr/man/man8
for file in *.8 ; do
  gzip -9Nc < $file > ${PKG}/usr/man/man8/${file}.gz
done
( cd ${PKG}/usr/man/man8
  ln -sf portmap.8.gz rpc.portmap.8.gz
)

# Add documentation:
mkdir -p ${PKG}/usr/doc/${P}-${V}
install -m 0644 \
 BLURB* CHANGES README* \
 ${PKG}/usr/doc/${P}-${V}

# Add the post-install script:
mkdir -p ${PKG}/install
zcat ${CWD}/post-install.gz > ${PKG}/install/post-install

# Copy the description files:
mkdir -p ${PKG}/description
cp ${CWD}/description/* ${PKG}/description

cd $PKG
makepkg -l ${OUT}/${P}-${V}-${ARCH}-${B}.tlz

