#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project web page : http://www.geocities.com/matsp888/unix/

# Package name
NAME="v2strip"
# Package version
VERSION="0.2.10"
# Files that should be placed in /usr/doc/package-version/
DOCS="AUTHORS COPYING ChangeLog INSTALL NEWS README"

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="i486"
CPU="i686"
BUILD="1gv"

rm -rf $PKG
mkdir -p $PKG

# Create package install directory
mkdir $PKG/install

# Create the slack-desc file
cat > $PKG/install/slack-desc << END
$NAME: v2strip (a utility that removes ID3v2 tags from MP3 files)
$NAME:
$NAME: v2strip is a little utility that removes ID3v2 tags from MP3 files
$NAME: to provide backward compatibility for applications not yet
$NAME: supporting these tags.
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

# Unpack source
tar xf $NAME-$VERSION.tar.gz

if [ $ARCH = "x86_64" ];then
        export CFLAGS="-O2 -fPIC"
        export CXXFLAGS=$CFLAGS
        export LIBDIRSUFFIX="64"
else
        export CFLAGS="-O2 -march=$ARCH -mtune=i686"
        export CXXFLAGS=$CFLAGS
        export LIBDIRSUFFIX=""
fi

# Configure and make
cd $NAME-$VERSION
./configure --prefix=/usr --libdir=/usr/lib${LIBDIRSUFFIX}
make

# Install on temporary directory
make install DESTDIR=$PKG

# Strip binaries
cd $PKG
find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# Copy Docs
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cd $CWD/$NAME-$VERSION
cp -a $DOCS $PKG/usr/doc/$NAME-$VERSION

# Compress the man page(s)
find $PKG/usr/man -type f -exec gzip -9 {} \;

# Make sure ownerships and permissions are sane
cd $PKG
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 {} \;

# Make the package
/sbin/makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

# Calculate md5sum and dependencies files
cd $CWD/
md5sum $NAME-$VERSION-$ARCH-$BUILD.txz > $NAME-$VERSION-$ARCH-$BUILD.md5

# Remove source-code and temporary install directories
# since they are not needed anymore
rm -rf $CWD/$NAME-$VERSION
rm -rf $PKG