#!/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://search.cpan.org/dist/XML-LibXML-Common/Common.pm
# source           : http://search.cpan.org/CPAN/authors/id/P/PH/PHISH/XML-LibXML-Common-0.13.tar.gz

# Source package name
NAME="XML-LibXML-Common"
# Zenwalk package name
ZENNAME="perl_XML-LibXML-Common"
# Perl package name
PERLNAME="XML::LibXML::Common"
# Package version
VERSION="0.13"
# Source code URL
SOURCE="http://search.cpan.org/CPAN/authors/id/P/PH/PHISH/XML-LibXML-Common-$VERSION.tar.gz"
# Files that should be placed in /usr/doc/package-version/
DOCS="Changes LICENSE MANIFEST README"

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="i486"
BUILD="66.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
$ZENNAME: perl_XML-LibXML-Common - Routines and Constants common for XML
$ZENNAME:
$ZENNAME: XML::LibXML::Common provides routines and constants common for
$ZENNAME: XML::LibXML and XML::GDOMEXML::LibXML and XML::GDOME share some
$ZENNAME: of the same functionality. This package should bundle some
$ZENNAME: shared constansts and functions, so both modules may coexist
$ZENNAME: within the same scripts.
$ZENNAME:
$ZENNAME:
$ZENNAME:
$ZENNAME:
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

# make
cd $NAME-$VERSION
perl Makefile.PL
# Patch for proper mandir
sed -i -e 's,share/,,' Makefile
# Now do make
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 "$PERLNAME|" "\$OLD" &> /dev/null ; then # don't append it twice
    rm "\$NEW" &>/dev/null
  else
    cat "\$NEW" >> "\$OLD"
    rm "\$NEW"
  fi
}
configcat $NAME
EOF

# Copy Docs
mkdir -p $PKG/usr/doc/$ZENNAME-$VERSION
cd $CWD/$NAME-$VERSION
cp -a $DOCS $PKG/usr/doc/$ZENNAME-$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/$ZENNAME-$VERSION
cp "$CWD/build-$ZENNAME.sh" "$PKG/usr/src/$ZENNAME-$VERSION/build-$ZENNAME.sh"
chmod a+x "$PKG/usr/src/$ZENNAME-$VERSION/build-$ZENNAME.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/$ZENNAME-$VERSION-$ARCH-$BUILD.txz

# Calculate md5sum
cd $CWD/
md5sum $ZENNAME-$VERSION-$ARCH-$BUILD.txz > $ZENNAME-$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 > $ZENNAME-$VERSION-$ARCH-$BUILD.src << EOF
http://pnboy.pinguix.com/gapan/$ZENNAME/build-$ZENNAME.sh
http://search.cpan.org/CPAN/authors/id/P/PH/PHISH/XML-LibXML-Common-$VERSION.tar.gz
EOF
