#!/bin/bash
#
# Heavily based on the Slackware 13.0 SlackBuild
#
# wxPython (Python GUI development)
# http://www.wxpython.org/
#


# Config variables:
PKGNAME="wxpython"
VERSION="2.8.10.1"
ARCH="$(arch)"
TAG="pv"
BUILD="4"


# Download urls:
BASE_URL="http://downloads.sourceforge.net/${PKGNAME}/"
SRC_FILENAME="wxPython-src-${VERSION}.tar.bz2"
DN_URL="${BASE_URL}${SRC_FILENAME}"


# Standard destinations folders:
BLD_PREFIX="/usr"
BLD_CONFIG="/etc"
BLD_LIBRARIES="${BLD_PREFIX}/lib"
BLD_MANDIR="${BLD_PREFIX}/man"
BLD_INFODIR="${BLD_PREFIX}/info"
BLD_DOCDIR="${BLD_PREFIX}/doc"
BLD_DESKTOP="${PKG}${BLD_PREFIX}/share/applications"
BLD_ICONS="${PKG}${BLD_PREFIX}/share/pixmaps"


# Dirs variables:
CWD="$(pwd)"
TMP="/tmp/txz_build-${PKGNAME}"
PKG="${TMP}/package"


# Build GCC flags change destination dirs for your arch:
if [ "${ARCH}" = "i386" ]; then
	SLKCHOST="i386-pc-linux-gnu"
	SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "${ARCH}" = "i486" ]; then
	SLKCHOST="i486-pc-linux-gnu"
	SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "${ARCH}" = "i686" ]; then
	SLKCHOST="i686-pc-linux-gnu"
	SLKCFLAGS="-O2"
elif [ "${ARCH}" = "x86_64" ]; then
	SLKCHOST="x86_64-pc-linux-gnu"
	SLKCFLAGS="-O2 -pipe"
	BLD_LIBRARIES="${BLD_PREFIX}/lib64"
fi


# Other vars:
SLKLDFLAGS="-L{$BLD_LIBRARIES}"


# Cleaning and/or creating build folders:
rm -rf ${PKG} >> /dev/null 2>> /dev/null
rm -rf ${TMP} >> /dev/null 2>> /dev/null
mkdir -p ${PKG}
mkdir -p ${TMP}


# Untar the source archive;
if [ ! -e "${CWD}/${SRC_FILENAME}" ]; then
	(cd ${CWD}; wget ${DN_URL})
fi
cd ${TMP}
tar xjvf "${CWD}/${SRC_FILENAME}" || exit 1
cd wxPython-src-${VERSION} || exit 1


# Chown and Chmod its files:
cd ${TMP}/wxPython-src-${VERSION} || exit 1
chown -Rv root:root .
chmod -R u+w,go+r-w,a-s .


# Starting the configurator:
cd ${TMP}/wxPython-src-${VERSION} || exit 1
CHOST="${SLKCHOST}" \
CXXFLAGS="${SLKCFLAGS}" \
CFLAGS="${SLKCFLAGS}" \
LDFLAGS="${SLKLDFLAGS}" \
./configure \
--enable-debug \
--enable-debug_gdb \
--enable-geometry \
--enable-graphics_ctx \
--enable-sound \
--enable-mediactrl \
--enable-display \
--enable-gui \
--enable-monolithic \
--enable-unicode \
--with-sdl \
--with-opengl \
--with-gtk \
--with-gnomeprint \
--prefix=${BLD_PREFIX} \
--sysconfdir=${BLD_CONFIG} \
--libdir=${BLD_LIBRARIES} \
--mandir=${BLD_MANDIR} \
--infodir=${BLD_INFODIR} \
--build=${ARCH}-slackware-linux || exit 1


# Standard build:
make || exit
make -C contrib/src/gizmos || exit 1
make -C contrib/src/stc || exit 1


# Install wxGTK and contribs:
make install DESTDIR=${PKG} || exit 1
make -C contrib/src/gizmos install DESTDIR=${PKG} || exit 1
make -C contrib/src/stc install DESTDIR=${PKG} || exit 1


# Fix a broken symlink:
(
	cd ${PKG}${BLD_PREFIX}/bin
	rm -rfv wx-config
	ln -sf ../lib/wx/config/gtk2-unicode-debug-2.8 wx-config
)


# Build wxPython:
cd ${TMP}/wxPython-src-${VERSION}/wxPython || exit 1
PATH="${PATH}:${PKG}${BLD_PREFIX}/bin" \
LD_LIBRARY_PATH="${PKG}${BLD_PREFIX}/lib" \
CHOST="${SLKCHOST}" \
CXXFLAGS="${SLKCFLAGS} -I${PKG}${BLD_PREFIX}/include -I${PKG}${BLD_PREFIX}/include/wx-2.8 -I${PKG}${BLD_PREFIX}/lib/wx/include/gtk2-unicode-debug-2.8" \
CFLAGS="${SLKCFLAGS} -I${PKG}${BLD_PREFIX}/include -I${PKG}${BLD_PREFIX}/include/wx-2.8 -I${PKG}${BLD_PREFIX}/lib/wx/include/gtk2-unicode-debug-2.8" \
LDFLAGS="${SLKLDFLAGS} -L${PKG}${BLD_PREFIX}/lib" \
python setup.py \
	BUILD_GLCANVAS="0" \
	WXPORT="gtk2" \
	UNICODE="1" \
	EP_ADD_OPTS="1" \
	BUILD_GLCANVAS="0" \
	WX_CONFIG="${PKG}${BLD_PREFIX}/bin/wx-config" \
	build || exit 1


# Install wxPython for wxGTK:
cd ${TMP}/wxPython-src-${VERSION}/wxPython || exit 1
python setup.py install --root=${PKG} || exit 1


# Menu items:
cd ${TMP}/wxPython-src-${VERSION}/wxPython || exit 1
for FILE in distrib/*.desktop; do
	install -m 644 ${FILE} ${PKG}${BLD_DESKTOP}
done


# Icon files:
mkdir -p ${PKG}${BLD_ICONS}
chmod 755 ${PKG}${BLD_ICONS}
install -m 644 ${TMP}/wxPython-src-${VERSION}/wxPython/wx/py/PyCrust_32.png ${PKG}${BLD_ICONS}/PyCrust.png
install -m 644 ${TMP}/wxPython-src-${VERSION}/wxPython/wx/tools/XRCed/XRCed_32.png ${PKG}${BLD_ICONS}/XRCed.png


# Restore CWD:
cd ${TMP}/wxPython-src-${VERSION} || exit 1


# Strip binaries:
(
	cd ${PKG}
	find  | xargs file | sed -n "/ELF.*executable/b PRINT;/ELF.*shared object/b PRINT;d;:PRINT s/\(.*\):.*/\1/;p;" | xargs strip --strip-unneeded 2> /dev/null
)


# Compress and link manpages, if any:
if [ -d ${PKG}${BLD_MANDIR} ]; then
	(
		cd ${PKG}${BLD_MANDIR}
		for MANPAGE_DIR in $(find . -type d -name "man*") ; do
			(
				cd ${MANPAGE_DIR}
				for MANPAGE_PAGE in $(find . -type l -maxdepth 1) ; do
					ln -s $(readlink ${MANPAGE_PAGE}).gz ${MANPAGE_PAGE}.gz
					rm ${MANPAGE_PAGE}
				done
				gzip -9 *.*
			)
		done
	)
fi


# Compress info files, if any:
if [ -d ${PKG}${BLD_INFODIR} ]; then
	(
		cd ${PKG}${BLD_INFODIR}
		rm -f dir
		gzip -9 *
	)
fi


# Add a documentation directory:
mkdir -p ${PKG}${BLD_DOCDIR}/${PKGNAME}-${VERSION}
cp -a AUTHORS COPYING INSTALL* LICENSE NEWS README VERSION ${PKG}${BLD_DOCDIR}/${PKGNAME}-${VERSION} 2>/dev/null
cp -a ${TMP}/wxPython-src-${VERSION}/docs/* $PKG/usr/doc/wxpython-$VERSION 2>/dev/null
cat ${CWD}/slack-desc > ${PKG}${BLD_DOCDIR}/${PKGNAME}-${VERSION}/slack-desc
cat ${CWD}/${PKGNAME}.SlackBuild > ${PKG}${BLD_DOCDIR}/${PKGNAME}-${VERSION}/${PKGNAME}.SlackBuild


# Move doc from /usr/share/doc to ${BLD_DOCDIR}:
if [ -d ${PKG}/usr/share/doc ]; then
	(
		mv ${PKG}/usr/share/doc/* ${PKG}${BLD_DOCDIR}/${PKGNAME}-${VERSION}
		rmdir -p ${PKG}/usr/share/doc/*
	)
fi


# Make Slackware package info:
mkdir -p ${PKG}/install
cat ${CWD}/slack-desc > ${PKG}/install/slack-desc


# If exist, it creates doinst.sh:
if [ -f "${CWD}/doinst.sh" ]; then
	cat ${CWD}/doinst.sh > ${PKG}/install/doinst.sh
fi


# Creating slack-required:
requiredbuilder -v -y -s ${CWD} ${PKG} || exit 1
cat ${PKG}/install/slack-required > ${PKG}${BLD_DOCDIR}/slack-required


# Make package:
cd ${PKG}
chown root:root * -Rv
makepkg -l y -c n ${CWD}/${PKGNAME}-${VERSION}-${ARCH}-${BUILD}${TAG}.txz


# Creating some infos:
(
	cd ${CWD}
	md5sum ${PKGNAME}-${VERSION}-${ARCH}-${BUILD}${TAG}.txz > ${PKGNAME}-${VERSION}-${ARCH}-${BUILD}${TAG}.txz.md5
	cat slack-desc > ${PKGNAME}-${VERSION}.txt
)


# Removing files, if asked:
case "$1" in
	"--cleanup")
		rm -rf ${PKG} >> /dev/null 2>> /dev/null
		rm -rf ${TMP} >> /dev/null 2>> /dev/null
	;;
esac
