#!/bin/bash
# This is a script to build a Zenwalk package. Check the Zenwalk homepage: www.zenwalk.org
#
# SOFTWARE			: camorama
# MAINTAINER       	: skywatcher (s.skywatcher|AT|gmail|DOT|org, simone antoniucci)
# LATEST REVISION   : 2009-12-12 
# BUILD-ONLY DEPS	: -
# NOTES				: -

### Checking for unbound variables and exit on errors
set -u -e

### Repo
URL=http://zenwalk.pinguix.com/user-accounts/skywatcher

### Pkg variables
NAME="camorama"
VERSION="0.19"
ARCH="i686" 
CPU="i686"
BUILD="64.1"
EXT="txz"
HOMEPAGE="http://camorama.fixedgear.org"
SRCLNK="http://ftp.gnome.org/pub/GNOME/sources/camorama/$VERSION/camorama-$VERSION.tar.bz2"
DESCRIPTION="webcam tool"
DOCS="AUTHORS ChangeLog COPYING INSTALL NEWS README THANKS TODO"
ADDSRC=""
INCSRC=""
ADDDEP="libv4l"
XCLDEP=""

### Script variables
CWD=`pwd`
PKG=$NAME-$VERSION-$ARCH-$BUILD
URL=$URL/$NAME/$VERSION
PKGDIR=$CWD/$NAME-install
SRCDIR=$CWD/$NAME-$VERSION
SRCPKG=`echo $SRCLNK | cut -d\  -f1 | rev | cut -d/ -f1 | rev`

### Flags and jobs
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="-O2 -march=$ARCH -mtune=$CPU"
NJOBS="-j2"

### Cleaning
rm -rf $PKGDIR
rm -rf $SRCDIR

### Making default package dirs
install -dv $SRCDIR $PKGDIR/{install,usr/src/$NAME-$VERSION}

### Getting sources
for src in $SRCLNK; do \
	source=`echo $src | rev | cut -d/ -f1 | rev` 
	if [ -f $source ]; then
		echo "Source file $source is already present, no need to download it";
	else
		echo "Downloading $source..."
		wget -c -nv $SRCLNK
	fi
done

### Extracting main sources
tar xfv $SRCPKG -C $SRCDIR --strip-components=1

### Building ===========================================================
cd $SRCDIR

# Compiling and installing
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --mandir=/usr/man --docdir=/usr/doc/$NAME-$VERSION \
			--bindir=/usr/libexec \
			--disable-schemas-install \
			--with-gconf-source="xml::/etc/gconf/gconf.xml.defaults" \
|| exit 1
make $NJOBS || exit 1
GCONF_DISABLE_MAKEFILE_SCHEMA_INSTALL=1 make install DESTDIR=$PKGDIR || exit 1

# gconf stuff
export GCONF_CONFIG_SOURCE="xml::$PKGDIR/etc/gconf/gconf.xml.defaults"
if [ -d $PKGDIR/etc/gconf/schemas ]; then
	install -dv -m755 $PKGDIR/etc/gconf/gconf.xml.defaults
	SCHEMAS=$PKGDIR/etc/gconf/schemas
	for schema in $SCHEMAS/*.schemas; do
		gconftool-2 --makefile-install-rule $schema
	done
	( cd $PKGDIR/etc/gconf ; find . -type d -exec chmod 755 {} \; )
	( cd $PKGDIR/etc/gconf ; find . -type f -exec chmod 644 {} \; )
fi

# exec
install -dv $PKGDIR/usr/bin
cat >> $PKGDIR/usr/bin/camorama << EOF
#!/bin/bash
LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so /usr/libexec/camorama "\$@"
EOF
chmod 755 $PKGDIR/usr/bin/camorama

# Icon
install -Dv $SRCDIR/pixmaps/$NAME.png $PKGDIR/usr/share/icons/hicolor/48x48/apps/$NAME.png

# Desktop entry
sed -i s:"=Application;Graphics":"=Video;AudioVideo;Application;": $PKGDIR/usr/share/applications/$NAME.desktop

# Docs, man, info
install -dv $PKGDIR/usr/doc/$NAME-$VERSION
install -v $DOCS $PKGDIR/usr/doc/$NAME-$VERSION

# Unnedeed stuff

### Slack-desc 
#    |-----handy-ruler------------------------------------------------------|
cat > $PKGDIR/install/slack-desc << EOF
$NAME: $NAME - $DESCRIPTION
$NAME:  
$NAME: Camorama is a small utility to view, alter and save images from a
$NAME: webcam or a Video4Linux device. It can apply a number of image
$NAME: filters and make remote captures.
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: $HOMEPAGE
EOF

### Doinstall

#~ ===================================================================== 

### Managing and zipping man and info files
if [ -d $PKGDIR/usr/man ]; then
	find $PKGDIR/usr/man -type f -exec gzip -9 -v {} \;
fi
if [ -d $PKGDIR/usr/info ]; then
	find $PKGDIR/usr/info -type f -exec gzip -9 -v {} \;
fi

### Adding build-file and included stuff to the package
for file in $0 $INCSRC
	do cp -av $CWD/$file $PKGDIR/usr/src/$NAME-$VERSION
done
chmod a+x $PKGDIR/usr/src/$NAME-$VERSION/build-$NAME.sh

### Stripping binaries
cd $PKGDIR
set +e
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

### Ownerships and permissions
cd $PKGDIR
chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
set -e

### Making the package
makepkg -l y -c n $CWD/$PKG.$EXT
cd $CWD

### md5
md5sum $PKG.$EXT > $PKG.md5

### dep
ADD=$ADDDEP EXCLUDE=$XCLDEP requiredbuilder -z $PKG.$EXT > $PKG.dep

### slackdesc
cat $PKGDIR/install/slack-desc > $PKG.txt

### src
echo $SRCLNK > $PKG.src
for link in $0 $ADDSRC 
	do echo $URL/$link >> $PKG.src
done

### Cleaning
rm -rf $SRCDIR
rm -rf $PKGDIR

### Done. Ciao!
