#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://picasa.google.com/linux/
# Packager - haraekei : harakei84 AT gmail DOT com

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-picasa
NAME=picasa
VERSION=2.7.3736-15
SVERSION=2.7.3736_15
CHOST=i486
ARCH=${ARCH:-i486}
BUILD=3hrk
SOURCE=http://dl.google.com/linux/rpm/stable/i386/${NAME}-${VERSION}.i386.rpm

[ ! -f $CWD/${NAME}-${VERSION}.i386.rpm ] && wget $SOURCE

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

cd $TMP
[ ! -f ${NAME}-${VERSION}.i386.tgz ] && rpm2tgz $CWD/${NAME}-${VERSION}.i386.rpm
[ ! -d $NAME ] && mkdir -p $NAME
tar xzvf $TMP/${NAME}-${VERSION}.i386.tgz -C $NAME

cd $NAME
chown -R root:root .
mkdir -p $PKG/opt/picasa
cp -a opt/picasa/{bin,desktop,lib,wine} $PKG/opt/picasa

sed 's/ICON/\/opt\/picasa\/desktop\/picasa-fontcfg.xpm/' \
$PKG/opt/picasa/desktop/google-picasa-fontcfg.desktop.template > \
$PKG/opt/picasa/desktop/google-picasa-fontcfg.desktop

sed -i 's/EXEC/\/opt\/picasa\/bin\/picasafontcfg/' \
$PKG/opt/picasa/desktop/google-picasa-fontcfg.desktop

sed 's/ICON/\/opt\/picasa\/desktop\/picasa.xpm/' \
$PKG/opt/picasa/desktop/google-picasa-mediadetector.desktop.template > \
$PKG/opt/picasa/desktop/google-picasa-mediadetector.desktop

sed -i 's/EXEC/\/opt\/picasa\/bin\/mediadetector/' \
$PKG/opt/picasa/desktop/google-picasa-mediadetector.desktop

sed 's/ICON/\/opt\/picasa\/desktop\/picasa.xpm/' \
$PKG/opt/picasa/desktop/google-picasa.desktop.template > \
$PKG/opt/picasa/desktop/google-picasa.desktop

sed -i 's/EXEC/\/usr\/bin\/picasa/' \
$PKG/opt/picasa/desktop/google-picasa.desktop

sed 's/ICON/\/opt\/picasa\/desktop\/picasa.xpm/' \
$PKG/opt/picasa/desktop/google-picasa.directory.template > \
$PKG/opt/picasa/desktop/google-picasa.directory

mkdir -p $PKG/usr/bin
cat << EOF > $PKG/usr/bin/picasa
#!/bin/bash
cd /opt/picasa/bin
./picasa
EOF
chmod 755 $PKG/usr/bin/picasa

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp opt/picasa/{*GPLV2,LICENSE.FOSS,README} $PKG/usr/doc/$NAME-$VERSION

cd $PKG
find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
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
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$SVERSION-$ARCH-$BUILD.tgz

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