#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://search.cpan.org/dist/YAML
# Packagers Gohanz ( gohanz@infinito.it)
# http://www.slacky.eu
#
#
# Exit on most errors
set -e 


CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/YAML
NAME=YAML
VERSION=0.68
TARGET=i486
ARCH=${ARCH:-noarch}
BUILD=1as

SOURCE=http://search.cpan.org/CPAN/authors/id/I/IN/INGY/$NAME-$VERSION.tar.gz

if [ ! -e $NAME-$VERSION.tar.gz ]; then
wget -c $SOURCE
fi

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O3 -march=i686 -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "athlon64" ]; then
  SLKCFLAGS="-O2 -march=athlon64 -pipe"
elif [ "$ARCH" = "athlonxp" ]; then
  SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

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

cd $TMP
tar xvzf $CWD/YAML-$VERSION.tar.gz

echo -e "\E[0;32m+-----------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild $NAME |\E[0;0m"
echo -e "\E[0;32m+-----------------------+\E[0;0m"

cd YAML-$VERSION
perl Makefile.PL
make || exit 1
make test || exit 1
make install DESTDIR=$PKG || exit 1

mkdir -p $PKG/usr/man/man3

cp -a $PKG/usr/share/man/man3/* $PKG/usr/man/man3
rm -r $PKG/usr/share

mkdir -p $PKG/usr/doc/perl-yaml-$VERSION
cp -a Changes MANIFEST README $PKG/usr/doc/perl-yaml-$VERSION

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


gzip -9 $PKG/usr/man/*/*

cd $PKG
chown -R root:root .
ADD="yaml >= 0.1.2-i486-1as" \
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/perl-yaml-$VERSION-$ARCH-$BUILD.tgz

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