#!/bin/sh
# Sources:
# ftp://ftp.pal.xgw.fi/pub/gnu/alpha/gnu/p2c-1.21alpha2.tar.gz
# ftp://ftp.clara.net/pub/mirrors/gnu/hurd/p2c-1.21alpha2.tar.gz
# ftp://ftp.obspm.fr/pub1/computing/gnu/hurd/p2c-1.21alpha2.tar.gz

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


VERSION=${VERSION:-1.21alpha2}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-3}


if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=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

# Explode the package framework:
cd $PKG
explodepkg $CWD/_p2c.tar.gz

cd $TMP
rm -rf p2c-1.21alpha-07.Dec.93
tar xzvf $CWD/p2c-1.21alpha2.tar.gz || exit 1
cd p2c-1.21alpha-07.Dec.93

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 {} \;

zcat $CWD/p2c_1.21alpha2-2.1.diff.gz | patch -p1 -E --verbose --backup --suffix=.orig || exit 1
find . -name "*.orig" | xargs rm --verbose
cd src
make OPT="$SLKCFLAGS" || exit 1
make p2cc
cat p2c > $PKG/usr/bin/p2c
cat p2cc > $PKG/usr/bin/p2cc
cat libp2c.a > $PKG/usr/lib/libp2c.a
cat p2c.man | gzip -9c > $PKG/usr/man/man1/p2c.1.gz
cat p2cc.man | gzip -9c > $PKG/usr/man/man1/p2cc.1.gz
cat sys.p2crc > $PKG/usr/lib/p2c/p2crc
cat loc.p2crc > $PKG/usr/lib/p2c/loc.p2crc
cat system.imp > $PKG/usr/lib/p2c/system.imp
cat system.m2 > $PKG/usr/lib/p2c/system.m2
cat turbo.imp > $PKG/usr/lib/p2c/turbo.imp
cat string.pas > $PKG/usr/lib/p2c/string.pas
cat p2c.h > $PKG/usr/include/p2c/p2c.h
cd ..

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

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

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

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

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/p2c-1.21alpha-07.Dec.93
  rm -rf $PKG
fi