#!/bin/bash
#
# Maintainer       : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# Former           : George Vlahavas (vlahavas~AT~gmail~DOT~com, gapan@zenwalk forums)
# project web page : http://sdl.perl.org
# source           : http://zarb.org/~gc/t/SDL_perl-1.20.0.tar.gz

# Package name
NAME="SDL_perl"
# Package version
VERSION="1.20.0"
# Source code URL
SOURCE="http://zarb.org/~gc/t/SDL_perl-1.20.0.tar.gz"
# Files that should be placed in /usr/doc/package-version/
DOCS="BUGS CHANGELOG COPYING MANIFEST README TODO"

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

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: SDL_Perl - Simple DirectMedia Library Bindings for Perl
$NAME:
$NAME: SDL_Perl is a multimedia binding for Perl, using the Simple
$NAME: DirectMedia Layer and OpenGL
$NAME:
$NAME: 
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

# Check to see if source is present. If it's not, download it
rm -rf $NAME-$VERSION
if [ -f $NAME-$VERSION.tar.gz ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c $SOURCE
fi

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

# Configure and make
cd $NAME-$VERSION
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="-O2 -march=$ARCH -mtune=$CPU"
./configure
# Patch for proper mandir
sed -i -e 's,share/,,' Makefile
make

# Install on temporary directory
make install DESTDIR=$PKG

# Take care of the perllocal.pod file which also exists in other packages
mv $PKG/usr/lib/perl5/5.10.0/i486-linux-thread-multi/perllocal.pod $PKG/usr/lib/perl5/5.10.0/i486-linux-thread-multi/perllocal.pod.$NAME

# Create the doinst.sh script for the perllocal.pod file
cat > $PKG/install/doinst.sh << EOF
configcat() {
  NEW="usr/lib/perl5/5.10.0/i486-linux-thread-multi/perllocal.pod.\$1"
  OLD="usr/lib/perl5/5.10.0/i486-linux-thread-multi/perllocal.pod"
  # If there's no file by that name, mv it over:
  if [ ! -r "\$OLD" ]; then
    mv "\$NEW" "\$OLD"
  elif grep "SDL_perl" "\$OLD" &> /dev/null ; then # don't append it twice
    rm "\$NEW" &>/dev/null
  else
    cat "\$NEW" >> "\$OLD"
    rm "\$NEW"
  fi
}
configcat $NAME
EOF

# 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 {} \;

# Add the build-file in the package
mkdir -p $PKG/usr/src/$NAME-$VERSION
cp "$CWD/build-$NAME.sh" "$PKG/usr/src/$NAME-$VERSION/build-$NAME.sh"
chmod a+x "$PKG/usr/src/$NAME-$VERSION/build-$NAME.sh"

# 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
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

# Calculate md5sum
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

cat > $NAME-$VERSION-$ARCH-$BUILD.src << EOF
http://zenwalk.pinguix.com/user-accounts/bip/EXTRA/l/$NAME/build-$NAME.sh
http://zarb.org/~gc/t/SDL_perl-$VERSION.tar.gz
EOF
