#!/bin/bash
#
# slackbuild script for python
# got it from ftp://ftp.slackware.com/pub/slackware/slackware-11.0/source/d/python/
# small changes by rhatto at riseup.net
#

if [ -s "slack-required" ]; then
  echo Recomended and required packages for building python are:
  cat slack-required | sed -e 's/^/\t/'
  if [ "$INTERACT" != "no" ]; then
    echo If you dont have those installed, press Ctrl-C. Otherwise, hit ENTER.
    read crap
#  else
#    echo Sleeping 3 seconds...
#    sleep 3
  fi
fi

CWD="`pwd`"

if [ -f ~/.slackbuildrc ]; then
  source ~/.slackbuildrc
elif [ -f /etc/slackbuildrc ]; then
  source /etc/slackbuildrc
fi

PACKAGE="python"
VERSION=${VERSION:=2.4.4}
ARCH=${ARCH:=i486}
BUILD=${BUILD:=1rha}
SRC_DIR=${SRC:=$CWD}
TMP=${TMP:=/tmp}
REPOS=${REPOS:=$TMP}

# -------  error codes for createpkg  --------------
ERROR_WGET=31;      ERROR_MAKE=32;      ERROR_INSTALL=33
ERROR_MD5=34;       ERROR_CONF=35;      ERROR_HELP=36
ERROR_TAR=37;       ERROR_MKPKG=38;     ERROR_GPG=39
ERROR_PATCH=40

RTOOL="wget"
PACKAGE_EXT="bz2"
SRC="Python-$VERSION.tar.$PACKAGE_EXT"
URL="http://www.python.org/ftp/python/$VERSION/$SRC"
HTML="html-2.4.4.tar.bz2"
HTML_URL="http://www.python.org/ftp/python/doc/$VERSION/$HTML"

SRC_DIR="$SRC_DIR/$PACKAGE"
mkdir -p $SRC_DIR

if [ "$RTOOL" == "wget" ]; then
  if [ ! -f "$SRC_DIR/$SRC" ]; then
    wget "$URL" -O "$SRC_DIR/$SRC" || exit $ERROR_WGET
  fi
  if [ ! -f "$SRC_DIR/$HTML" ]; then
    wget "$HTML_URL" -O "$SRC_DIR/$HTML" || exit $ERROR_WGET
  fi
fi

TMP=$TMP/$PACKAGE
PKG=$TMP/package-$PACKAGE
rm -rf $TMP
mkdir -p $PKG

# Location for Python site-packages:
SITEPK=$PKG/usr/lib/python2.4/site-packages
# same as above without $PKG
TOOLSDIR=/usr/lib/python2.4/site-packages

LIBDIR=/usr/lib

# Normally I don't trust -O3, but it is the Python default so
# I'll assume that in this case it has been well tested.
if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O3 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O3 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O3"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O3"
  LIBDIR="/usr/lib64"
fi

cd $TMP
rm -rf Python-$VERSION
tar xjvf $SRC_DIR/Python-$VERSION.tar.bz2 || exit $ERROR_TAR
cd Python-$VERSION
chown -R root:root .
find . -type d -exec chmod 755 {} \;
find . -type f -perm 775 -exec chmod 755 {} \;
find . -type f -perm 664 -exec chmod 644 {} \;
find . -type d -name CVS -exec rm -r {} \;

if [ "$ARCH" == "x86_64" ]; then
  zcat $CWD/Python-2.3.3-dirs.patch.gz | patch -p0 --verbose || exit $ERROR_PATCH # thanks suse
fi

if [ -f "$CWD/$PACKAGE-$VERSION.diff" ]; then
  patch -p1 < $CWD/$PACKAGE-$VERSION.diff || exit $ERROR_PATCH
fi

OPT="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=$LIBDIR \
  --with-ncurses \
  --with-threads \
  --enable-ipv6 \
  --enable-shared || exit $ERROR_CONF
make -j3 || exit $ERROR_MAKE
make install DESTDIR=$PKG || exit $ERROR_INSTALL

# 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
( cd Tools ; cp -a * $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 /usr/lib/python2.4/site-packages/modulator/modulator.py modulator
  ln -sf /usr/lib/python2.4/site-packages/pynche/pynche pynche
  ln -sf /usr/lib/python2.4/site-packages/i18n/msgfmt.py .
  ln -sf /usr/lib/python2.4/site-packages/i18n/pygettext.py .
)

# Install docs:
mkdir -p $PKG/usr/doc/python-$VERSION
cp -a README $PKG/usr/doc/python-$VERSION
cp -a Misc $PKG/usr/doc/python-$VERSION
( cd $PKG/usr/doc/python-$VERSION ; mkdir html )
( cd $PKG/usr/doc/python-$VERSION/html
  tar xjvf $SRC_DIR/html-$VERSION.tar.bz2
  chown -R root:root .
)
( cd $PKG/usr/bin
  rm -f python
  ln -sf python2.4 python
)
( 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
)
gzip -9 $PKG/usr/man/man1/*.?
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n $REPOS/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz || exit $ERROR_MKPKG

if [ "$CLEANUP" == "yes" ]; then
  rm -rf $TMP
fi