#!/bin/bash
#
# Heavily based on the Slackware 12.2 SlackBuild
#
# Xaw TV
# http://bytesex.org/xawtv/
#
# Packager:	Luca Cireddu
#		luca@lucacireddu.it
#


# Config variables:
PKGNAME="xawtv"
VERSION="3.95"
ARCH="$(arch)"
BUILD="1srd"
SRC_FILENAME="$PKGNAME-$VERSION.tar.gz"
DN_URL="http://dl.bytesex.org/releases/$PKGNAME/$SRC_FILENAME"


# Standard destinations folders:
BLD_PREFIX="/usr"
BLD_CONFIG="/etc"
BLD_LIBRARIES="$BLD_PREFIX/lib"
BLD_MANDIR="$BLD_PREFIX/man"
BLD_DOCDIR="$BLD_PREFIX/doc"
BLD_INFODIR="$BLD_PREFIX/info"
BLD_INCLUDE="$BLD_PREFIX/include"


# Dirs variables:
CWD="$(pwd)"
TMP="/tmp/tgz_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 and chown and chmod its files:
if [ ! -e "$CWD/$SRC_FILENAME" ]; then
	(cd $CWD; wget $DN_URL)
fi
cd $TMP
tar xzvf "$CWD/$SRC_FILENAME"
cd $PKGNAME-$VERSION || exit 1
chown -Rv root:root .
chmod -R u+w,go+r-w,a-s .


# Applying the patches:
(
	cd $TMP/$PKGNAME-$VERSION
	patch -p1 < $CWD/patches/xawtv-fbtools_c.patch || exit 1
	patch -p1 < $CWD/patches/xawtv-fbtools_h.patch || exit 1
	patch -p1 < $CWD/patches/xawtv-matrox_c.patch || exit 1
)


# Starting the configurator:
CHOST="$SLKCHOST" \
CXXFLAGS="$SLKCFLAGS" \
CFLAGS="$SLKCFLAGS" \
LDFLAGS="$SLKLDFLAGS" \
./configure \
--enable-mmx \
--disable-xvideo \
--disable-motif \
--without-x \
--prefix=$BLD_PREFIX \
--sysconfdir=$BLD_CONFIG \
--libdir=$BLD_LIBRARIES \
--mandir=$BLD_MANDIR \
--infodir=$BLD_INFODIR \
--includedir=$BLD_INCLUDE \
--build=$ARCH-slackware-linux || exit 1


# Build and install:
make || exit 1
make install DESTDIR=$PKG || 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 MAN_DIR in $(find . -type d -name "man*") ; do
			( cd $MAN_DIR
				for MAN_PAGE in $( find . -type l -maxdepth 1) ; do
					ln -s $( readlink $MAN_PAGE ).gz $MAN_PAGE.gz
					rm $MAN_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


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


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


# Creating slack-required:
requiredbuilder -v -y -s $CWD $PKG || exit 1


# Creating doinst.sh, if any:
if [ -f "$CWD/doinst.sh" ]; then
	cat $CWD/doinst.sh > $PKG/install/doinst.sh
fi


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


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


# Remove temp files:
case "$1" in
	"cleanup")
		rm -rf $PKG >> /dev/null 2>> /dev/null
		rm -rf $TMP >> /dev/null 2>> /dev/null
	;;
esac
