#!/bin/bash

# python.SlackBuild
# Heavily based on the original Slackware build script,
# Modified by Stuart Winter <mozes@slackware.com> for ARMedslack.
# 24-Jun-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

# Determine the CFLAGS for the known architectures:
# Don't set any SLKCFLAGS here, or OPT="$SLKCFLAGS" before the ./configure.
# Python gets the compile options right without any help.
case $ARCH in
   arm)
            export EXTRA_CFLAGS="$SLKCFLAGS" ## Take them from the slackdev config - could just be armv7-a
            export LIBDIRSUFFIX="" ;;
esac

# Capitalised source archive name:
SRCNAM=Python

# 2.6, 2.7 etc:
BRANCH_VERSION=$(echo $VERSION | cut -f 1,2 -d . )

# Location for Python site-packages:
SITEPK=$PKG/usr/lib${LIBDIRSUFFIX}/${PKGNAM}${BRANCH_VERSION}/site-packages
# same as above without $PKG
TOOLSDIR=/usr/lib${LIBDIRSUFFIX}/${PKGNAM}${BRANCH_VERSION}/site-packages

# Extract source:
tar xvvf $CWD/$SRCNAM-$VERSION.tar.xz
cd $SRCNAM-* || exit 1
slackhousekeeping

# Clean up after the patches:
find . -name "*.orig" -exec rm "{}" \;

zcat $CWD/python.readline.set_pre_input_hook.diff.gz | patch -p1 --verbose || exit 1
# We don't want a large libpython*.a:
zcat $CWD/python.no-static-library.diff.gz | patch -p1 --verbose || exit 1

# Configure:
#OPT="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --docdir=/usr/doc/python-$VERSION \
   --with-threads \
   --enable-ipv6 \
   --enable-shared \
   --build=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall

# Install some python-demo files:
mkdir -p $PKG/usr/doc/python-$VERSION
cp -a Demo $PKG/usr/doc/python-$VERSION

# We'll install the python-tools under site-packages:
mkdir -p $SITEPK
cp -a Tools/* $SITEPK

mkdir -p $PKG/usr/doc/python-$VERSION
mv $SITEPK/README $PKG/usr/doc/python-$VERSION/README.python-tools
( cd $PKG/usr/doc/python-$VERSION
  ln -sf $TOOLSDIR Tools
)
# Make a few useful symlinks:
mkdir -p $PKG/usr/bin
( cd $PKG/usr/bin
  ln -sf $TOOLSDIR/pynche/pynche pynche
  ln -sf $TOOLSDIR/i18n/msgfmt.py .
  ln -sf $TOOLSDIR/i18n/pygettext.py .
)

# Install docs:
mkdir -p $PKG/usr/doc/python-$VERSION/Documentation
cp -a README LICENSE $PKG/usr/doc/python-$VERSION
cp -a Misc $PKG/usr/doc/python-$VERSION
tar xf $CWD/python-$VERSION-docs-text.tar.?z*
mv python-${VERSION}-docs-text/* $PKG/usr/doc/python-$VERSION/Documentation
chown -R root:root $PKG/usr/doc/python-$VERSION

# Fix possible incorrect permissions:
( cd $PKG
  find . -type d -exec chmod 755 "{}" \;
  find . -perm 640 -exec chmod 644 "{}" \;
  find . -perm 750 -exec chmod 755 "{}" \;
)

( cd $PKG/usr/bin
  rm -f python
  ln -vfs python${BRANCH_VERSION} python
)

( cd $PKG/usr/man
  if [ -r python${BRANCH_VERSION}.1.gz ] ; then
         ln -vfs python${BRANCH_VERSION}.1.gz python.1.gz
  fi
)

# 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	
