#!/bin/bash

# p2c.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 15-Jul-2004

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

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

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

# Extract source:
tar zxvf $CWD/$PACKAGE-$VERSION.tar.gz
cd $PACKAGE-*
slackhousekeeping

# Create package framework:
mkdir -p $PKG/usr/{include/p2c,bin,lib/p2c,man/man1}

# Apply patches:
zcat $CWD/p2c-1.21alpha2.diff.gz | patch -Ep1 
zcat $CWD/p2c-1.21alpha2.time.diff.gz | patch -Ep1

# Build:
( cd src
  make || failmake 
  make p2cc || failmake

  # Install binaries:
  install -m755 p2c p2cc $PKG/usr/bin

  # Install man pages:
  install -m644 p2c.man  $PKG/usr/man/man1/p2c.1
  install -m644 p2cc.man $PKG/usr/man/man1/p2cc.1

  # Install libaries:
  install -m644 libp2c.a $PKG/usr/lib
  install -m644 sys.p2crc loc.p2crc system.imp system.m2 turbo.imp string.pas \
              $PKG/usr/lib/p2c

  # Headers:
  install -m644 p2c.h $PKG/usr/include/p2c )

# Docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a README ftp.README examples \
      $PKG/usr/doc/$PACKAGE-$VERSION
rm -rf $PKG/usr/doc/$PACKAGE-$VERSION/examples/c

# Slackware policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown (root:bin), slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = armv3 ]; 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
