#!/bin/bash

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

# Record toolchain & other info for the build log:
slackbuildinfo

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

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

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.gz
cd $PKGNAM-*
slackhousekeeping

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

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

# Apply patches:
zcat $CWD/p2c_1.21alpha2-2.1.diff.gz | patch -p1 -E --verbose --backup --suffix=.orig || failpatch
zcat $CWD/p2c_time.diff.gz | patch -p0 --verbose --backup --suffix=.orig || exit 1

# Build:
( cd src
  OPT="$SLKCFLAGS" 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 )

mkdir -p $PKG/usr/doc/p2c-1.21alpha2
cp -a \
  README ftp.README \
  $PKG/usr/doc/p2c-1.21alpha2
( cd src
  for file in HISTORY NOTES COPYING INSTALL ; do
    cp -a $file $PKG/usr/doc/p2c-1.21alpha2
  done
)
cp -a src/README $PKG/usr/doc/p2c-1.21alpha2/README.src
cp -a examples $PKG/usr/doc/p2c-1.21alpha2
( cd $PKG/usr/doc/p2c-1.21alpha2
  zcat $CWD/p2c.examples.diff.gz | patch -p1 --verbose || exit 1
) || exit 1
rm -f $PKG/usr/doc/p2c-1.21alpha2/examples/Makefile~
rm -rf $PKG/usr/doc/p2c-1.21alpha2/examples/c

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging 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, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

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