#!/bin/bash

# Copyright (C) 2005 Lucien Nardini (lucien.nardini gmail.com)
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# 
# * 20060302 : Romain Acciari
#   - New SlackBuild for XdTV 2.3.0
# * 20051225 : Romain Acciari (racciari laposte.net)
#   - Based on Lucien Nardini build script.
#     Please do not email him if you got problems with it.

set -e -x

source $PWD/build.conf

if [ ! -d $TMP ]; then
  mkdir -m 755 -p $TMP
fi
rm -rf $TMP/$NAME0-$VERSION0 $PKG0

ARCHIVE0=$(basename "$SOURCE0")
rm -f $ARCHIVE0.part
if [ ! -e $ARCHIVE0 ]; then
  wget "$SOURCE0" -O $ARCHIVE0.part
  mv $ARCHIVE0.part $ARCHIVE0
fi
#md5sum -c $ARCHIVE0.md5
#sha1sum -c $ARCHIVE0.sha1


tar xzf $NAME0-$VERSION0.tar.gz -C $TMP
cd $TMP/$NAME0-$VERSION0

if [ "$ARCH" != "noarch" ]; then
  case $ARCH in
    i386) CFLAGS="-O3 -march=i386 -mcpu=i686" ;;
    i486) CFLAGS="-O3 -march=i486 -mcpu=i686" ;;
    i586) CFLAGS="-O3 -march=i586 -mcpu=i686" ;;
    i686) CFLAGS="-O3 -march=i686" ;;
    *)    CFLAGS="-O3" ;;
  esac
  export CFLAGS
fi

./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --enable-shared \
  --disable-static

make

mkdir -m 755 -p $PKG0
make DESTDIR=$PKG0 install


mkdir -m 755 -p $PKG0/usr/share/pixmaps
mkdir -m 755 -p $PKG0/opt/kde/share/icons/hicolor/16x16/apps
mkdir -m 755 -p $PKG0/opt/kde/share/icons/hicolor/32x32/apps
mkdir -m 755 -p $PKG0/opt/kde/share/icons/hicolor/48x48/apps
cp -a $PKG0/usr/share/xdtv/icons/xdtv.xpm $PKG0/usr/share/pixmaps/
cp -a $PKG0/usr/share/xdtv/icons/xdtv-16.png $PKG0/opt/kde/share/icons/hicolor/16x16/apps/xdtv.png
cp -a $PKG0/usr/share/xdtv/icons/xdtv-32.png $PKG0/opt/kde/share/icons/hicolor/32x32/apps/xdtv.png
cp -a $PKG0/usr/share/xdtv/icons/xdtv-48.png $PKG0/opt/kde/share/icons/hicolor/48x48/apps/xdtv.png
mkdir -m 755 -p $PKG0/usr/share/applications
install -m 644 $CWD/xdtv.desktop $PKG0/usr/share/applications/
install -m 644 $CWD/xdtv_wizard.desktop $PKG0/usr/share/applications/
gzip -9 $PKG0/usr/man/man?/*.?

#not needed with version greater than 2.3.1
#gzip -9 $PKG0/usr/X11R6/lib/X11/fonts/misc/*.pcf


find $PKG0 -type d | xargs chmod 755
find $PKG0 -type f | xargs chmod 644
chmod 755 $PKG0/usr/bin/*

find $PKG0 -type f | file -f - | grep "ELF.*executable" | cut -f 1 -d : \
| while read FILE ; do
  strip --strip-all $FILE
  chmod 755 $FILE
done

find $PKG0 -type f | file -f - | grep "ELF.*shared object" | cut -f 1 -d : \
| while read FILE ; do
  strip --strip-unneeded $FILE
  chmod 755 $FILE
done


mkdir -m 755 -p $PKG0/usr/doc/$PKGNAME0-$VERSION0
install -m 644 \
  AUTHORS ChangeLog COPYING FAQfr-xdtv NEWS README* TODO  \
  $PKG0/usr/doc/$PKGNAME0-$VERSION0
mkdir -m 755 -p $PKG0/usr/doc/$PKGNAME0-$VERSION0/lirc
install -m 644 lircrc*.sample \
  $PKG0/usr/doc/$PKGNAME0-$VERSION0/lirc/

mkdir -m 755 $PKG0/install/
sed "s,^,$PKGNAME0: ,g" $CWD/slack-desc >$PKG0/install/slack-desc
chmod 644 $PKG0/install/slack-desc
install -m 755 $CWD/doinst.sh $PKG0/install/


PACK="set -e -x ; cd $PKG0
chown root.root -R . ; chown root.bin -R usr/bin
rm -f $CWD/$PKGNAME0-$VERSION0-$ARCH-$BUILD.tgz
/sbin/makepkg -l y -c n -p $CWD/$PKGNAME0-$VERSION0-$ARCH-$BUILD.tgz
cd $CWD ; rm -rf $PKG0"

if [ $UID -eq 0 ]; then
  eval "$PACK"
elif type -p fakeroot ; then
  echo "$PACK" | fakeroot
else
  su -c "$PACK"
fi


if [ "$1" != "--no-cleanup" ]; then
  cd $TMP
  rm -rf $NAME0-$VERSION0
fi
exit 0
