#!/bin/sh
# $Id: pyvnc2swf.SlackBuild,v 1.1 2007/01/17 15:07:57 root Exp root $
# Copyright (c) 2007 Eric Hameleers <alien@slackware.com>
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild script 
# ===========================
# By:        Eric Hameleers <alien@slackware.com>
# For:       pyvnc2swf
# Descr:     a cross-platform screen recording tool
# URL:       http://www.unixuser.org/~euske/vnc2swf/pyvnc2swf.html
# Needs:     pygame
# Changelog:
# 0.8.2.1-1:   17/Jan/2007 by Eric Hameleers <alien@slackware.com>
#            * Initial build.
# 
# Run 'sh pyvnc2swf.SlackBuild --cleanup' to build a Slackware package.
# The package (.tgz) plus descriptive .txt file are created in /tmp .
# Install using 'installpkg'. 
#
# -----------------------------------------------------------------------------

# --- INIT ---
# Set initial variables:

PRGNAM=pyvnc2swf
VERSION=${VERSION:-0.8.2.1}
ARCH=noarch
BUILD=${BUILD:-1}

DOCS="LICENSE.txt PKG-INFO README.txt"

PYTHON_VERSION=`python -c 'import sys; print sys.version[:3]'`

# Where do we look for sources?
CWD=`pwd`
SRCDIR=`dirname $0`
[ "${SRCDIR:0:1}" == "." ] && SRCDIR=${CWD}/${SRCDIR}

# Place to build (TMP) package (PKG) and output (OUTPUT) the program:
TMP=${TMP:-/tmp/build}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# Input URL: http://davidf.sjsoft.com/files/pyvnc2swf/pyvnc2swf-0.8.2.1.tar.gz
SOURCE[0]="$SRCDIR/${PRGNAM}-${VERSION}.tar.gz"
SRCURL[0]="http://davidf.sjsoft.com/files/${PRGNAM}/${PRGNAM}-${VERSION}.tar.gz"

SOURCE[1]="$SRCDIR/ez_setup.py"
SRCURL[1]="http://peak.telecommunity.com/dist/ez_setup.py"

##
## --- with a little luck, you won't have to edit below this point --- ##
##

# Exit the script on errors:
set -e
trap 'echo "$0 FAILED at line $LINENO!" | tee $OUTPUT/error-${PRGNAM}.log' ERR
# Catch unitialized variables:
set -u
P1=${1:-1}

if [ ! -d $TMP/tmp-$PRGNAM ]; then
  mkdir -p $TMP/tmp-$PRGNAM # location to build the source
elif [ "$P1" != "--oldbuild" ]; then
  # If the "--oldbuild" parameter is present, we keep
  #  the old build files and continue;
  # By default we remove the remnants of previous build and continue:
  rm -rf $TMP/tmp-$PRGNAM/*
fi

if [ ! -d $PKG ]; then
  mkdir -p $PKG  # place for the package to be built
else
  rm -rf $PKG/*  # We always erase old package's contents:
fi

if [ ! -d $OUTPUT ]; then
  mkdir -p $OUTPUT  # place for the package to be saved
fi


# --- SOURCE FILE AVAILABILITY ---

for (( i = 0; i < ${#SOURCE[*]}; i++ )) ; do
  if ! [ -f ${SOURCE[$i]} ]; then
    if ! [ "x${SRCURL[$i]}" == "x" ]; then
      # Check if the $SRCDIR is writable at all - if not, download to $OUTPUT
      [ -w "$SRCDIR" ] || SOURCE[$i]="$OUTPUT/`basename ${SOURCE[$i]}`"
      echo "Source '`basename ${SOURCE[$i]}`' not available yet..."
      echo "Will download file to `dirname $SOURCE[$i]`"
      wget -nv -O "${SOURCE[$i]}" "${SRCURL[$i]}" || true
      if [ $? -ne 0 ]; then
        echo "Downloading '`basename ${SOURCE[$i]}`' failed... aborting the buil
d."
        mv -f "${SOURCE[$i]}" "${SOURCE[$i]}".FAIL
        exit 1
      fi
    else
      echo "File '`basename ${SOURCE[$i]}`' not available... aborting the build.
"
      exit 1
    fi
  fi
done

if [ "$P1" == "--download" ]; then
  echo "Download complete."
  exit 0
fi

# --- PACKAGE BUILDING ---

echo "++"
echo "|| $PRGNAM-$VERSION"
echo "++"

cd $PKG

# Explode the package framework:
if [ -f $SRCDIR/_$PRGNAM.tar.gz ]; then
  explodepkg $SRCDIR/_$PRGNAM.tar.gz
fi

cd $TMP/tmp-$PRGNAM


# --- TARBALL EXTRACTION,PATCH,MODIFY ---

echo "Extracting the source archive(s) for $PRGNAM..."
for (( i = 0; i < ${#SOURCE[*]}; i++ )) ; do
  if `file ${SOURCE[$i]} | grep -q ": bzip2"`; then
    tar -xjvf ${SOURCE[$i]}
  elif `file ${SOURCE[$i]} | grep -q ": gzip"`; then
    tar -xvf ${SOURCE[$i]}
  else
    cp ${SOURCE[$i]} .
  fi
done
cd ${PRGNAM}-${VERSION}
# We will do an auto-install of setuptools if it is missing from this box:
set +e
python <<-EOT
	"""Test for availability of setuptools
	"""
	import sys
	try:
	    import setuptools
	except ImportError:
	    sys.exit(2)
	sys.exit(0)
	EOT
RET=$?
set -e
if [ $RET -eq 2 ]; then
  echo "** Please ignore the previous 'FAILED' message **"
  mv ../ez_setup.py .
  sed -i -e '/from setuptools import/{N;s/^/from ez_setup import use_setuptools\nuse_setuptools()\n/}' setup.py
fi
chown -R root:root .
chmod -R u+w,go+r-w,a-s .


# --- BUILDING ---

echo Building ...

python setup.py build 2>&1 | tee $OUTPUT/make-${PRGNAM}.log

python setup.py install \
  --single-version-externally-managed \
  --root=$PKG \
  --record=$OUTPUT/install-${PRGNAM}.log

# Create a doinst.sh that checks the python version
mkdir -p $PKG/install
cat <<-EOT > $PKG/install/doinst.sh
	PYTHON=\`python -c 'import sys; print sys.version[:3]'\`
	if [ "\$PYTHON" != "$PYTHON_VERSION" ]; then
	  echo "**"
	  echo "** You need to install python version $PYTHON_VERSION before starting with ${PRGNAM}!"
	  echo "**"
	fi
	EOT


# --- DOCUMENTATION ---

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION || true
chmod -R a-w $PKG/usr/doc/$PRGNAM-$VERSION/*

chmod -R o-w $PKG


# --- PACKAGE DESCRIPTION ---

mkdir -p $PKG/install
cat $SRCDIR/slack-desc > $PKG/install/slack-desc
if [ -f $SRCDIR/doinst.sh ]; then
  cat $SRCDIR/doinst.sh >> $PKG/install/doinst.sh
fi
if [ -f $SRCDIR/slack-required ]; then
  cat $SRCDIR/slack-required > $PKG/install/slack-required
fi


# --- BUILDING ---

# Build the package:
cd $PKG
makepkg --linkadd y --chown n $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}.tgz \
  2>&1 | tee $OUTPUT/makepkg-${PRGNAM}.log
(cd $OUTPUT && md5sum ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}.tgz > ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}.tgz.md5)
cat $PKG/install/slack-desc | grep "^${PRGNAM}" > $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}.txt
if [ -f $PKG/install/slack-required ]; then
  cat $PKG/install/slack-required > $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}.dep
fi


# --- CLEANUP ---

# Clean up the extra stuff:
if [ "$P1" = "--cleanup" ]; then
  rm -rf $TMP/tmp-$PRGNAM
  rm -rf $PKG
fi
