#!/bin/sh
# Heavily based on the Slackware 12. SlackBuild
# http://www.songbirdnest.com
# Packager Andrea Sciucca  ( gohanz at infinito.it)
# http://www.slacky.it

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/Songbird
NAME=songbird
VERSION=1.2.0
SVERSION=_1.2.0-1146_linux
ARCH=${ARCH:-i686}
BUILD=1as
SOURCE=http://download.songbirdnest.com/installer/linux/i686/Songbird$SVERSION-i686.tar.gz

if [ ! -e Songbird$SVERSION-i686.tar.gz ]; then
 wget -c $SOURCE
fi

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi

mkdir -p $PKG/usr/lib

( cd $PKG/usr/lib
  tar xvzf $CWD/Songbird$SVERSION-i686.tar.gz
  cd Songbird
  zcat $CWD/songbird.diff.gz | patch -p1 --verbose
  chown -R root:root .
  find . \( -perm 777 -o -perm 775 -o -perm 711 \) -exec chmod 755 {} \;
  find . \( -perm 700 -o -perm 555 -o -perm 511 \) -exec chmod 755 {} \;
  find . \( -perm 666 -o -perm 664 -o -perm 600 \) -exec chmod 644 {} \;
  find . \( -perm 444 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;
)

mkdir -p $PKG/usr/bin

mkdir -p $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/usr/share/applications

zcat $CWD/$NAME.gz > $PKG/usr/bin/$NAME
chmod 755 $PKG/usr/bin/$NAME

cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Encoding=UTF-8
Exec=/usr/lib/Songbird/songbird
Icon=/usr/lib/Songbird/chrome/icons/default/default.xpm
Type=Application
Categories=Application;AudioVideo;
Name=Songbird
GenericName=Web Player
Comment=Songbird Media Player Mozilla based!
MimeType=text/html
X-KDE-StartupNotify=true
EOF

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP
fi
