#!/bin/bash
# packager                  : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# source                    : http://sourceforge.net/projects/mjpeg
# dernières révisions       : 29/4/2010

softname='mjpegtools'
softversion='1.9.0'
packageversion='64.4'

arch='i486'
cpu='i686'

prefix=/usr

# log
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softname-$softversion.tar.gz"
src="$buildir/$softname-$softversion"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://downloads.sourceforge.net/mjpeg/$softname-$softversion.tar.gz

# prepare the build result directory
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/$prefix/bin
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc
          |-----handy-ruler------------------------------------------------------|
mjpegtools: Mjpeg - tools to edit, playback, compress MPEG
mjpegtools:
mjpegtools: 
mjpegtools: Mjpeg tools is a suite of programs which support video capture, 
mjpegtools: editing, playback and compression to MPEG of MJPEG video..
mjpegtools: 
mjpegtools: 
mjpegtools:
mjpegtools: 
mjpegtools:
mjpegtools: 
EOF

# check if source is present
if [ -f $softname-$softversion.tar.gz ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c --no-check-certificate $source
fi

# extract the source code
cd $buildir
tar xvzf $srcpkg
cd $src

# building

    patch -p1 < ../lpcmstrm_in.cpp.patch
	patch -p1 < ../cinelerra.patch

    export CFLAGS="-O2 -march=$arch -mtune=$cpu"
    echo "echo $CFLAGS" > cpuinfo.sh
    ./configure \
      --prefix=$prefix \
      --sysconfdir="/etc" \
      --localstatedir="/var/lib" \
	  --mandir="/usr/man" \
      --infodir="/usr/info" \
	  --enable-static="no" \
	  --enable-shared="yes" \
	  --build=$arch-slackware-linux || exit

make || exit 1
make install DESTDIR=$dest

# add 'default' files
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      AUTHORS PLANS INSTALL HINTS COPYING NEWS README TODO CHANGES ChangeLog\
      $dest/usr/doc/$softname-$softversion

# Strip Executables
( cd $# Strip Executables# Strip ExecutablesIdest
  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
)

# compress man pages
( cd $dest/$prefix/man
  find . -name "*.?" | xargs gzip -9
)


# compress info pages
( cd $dest/$prefix/info
  find . -name "*.info" | xargs gzip -9
)

# put the ZenWalk build in the right directory
cd $buildir
cp build-$softname.sh $dest/$prefix/src/$softname-$softversion
cp lpcmstrm_in.cpp.patch $dest/$prefix/src/$softname-$softversion
cp cinelerra.patch $dest/$prefix/src/$softname-$softversion

# set target permissions
chown -R root.root $dest

cd $dest
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 {} \;

# real packaging work
cd $dest
makepkg -l y -c n $dest.txz

# computes the md5sum signature for the package
cd $buildir
md5sum $package.txz > $package.md5

# .src
echo http://zenwalk.pinguix.com/user-accounts/bip/ISO/ap/mjpegtools/source/build-mjpegtools.sh > $package.src
echo http://zenwalk.pinguix.com/user-accounts/bip/ISO/ap/mjpegtools/source/lpcmstrm_in.cpp.patch >> $package.src
echo http://zenwalk.pinguix.com/user-accounts/bip/ISO/ap/mjpegtools/source/cinelerra.patch >> $package.src
echo $source >> $package.src

# .dep
cd $buildir
echo -e "atk,cairo,expat,fontconfig,freetype,gcc,gcc-g++,glib2,gtk+2,\
libX11,libXau,libXcomposite,libXcursor,libXdamage,libXdmcp,libXext,\
libXfixes,libXi,libXinerama,libXrandr,libXrender,libdv,libjpeg,libpng,libquicktime,libxcb,pango,pixman,sdl,zlib" > $package.dep

# erase source
rm -rf $src
rm -rf $dest

# log
) 2>&1 | tee "$softname-$softversion-build.log"
