#!/bin/bash

# Packager : Jean-Philippe Guillemin <jp.guillemin@free.fr>
# Sources for perl : http://www.perl.com
# Sources for modules : http://search.cpan.org


VERSION='5.14.0'
BRANCHVERSION='5.14.0'
BUILD='72.1'
NAME='perl-Net'
arch='i486'
cpu='i686'

PKGBUILD=$(pwd)
TMP="$PKGBUILD/tmp"
rm -rf $TMP
mkdir -p $TMP
tarball="$PKGBUILD/$NAME-$VERSION.tar.gz"
PKG="/tmp/$NAME-$VERSION-$arch-$BUILD"
rm -f $PKG.tgz
rm -rf $PKG
mkdir -p $PKG


cd $PKGBUILD

# Modules:
sources="\
Net-DBus \
Net-DNS "

for source in $sources ; do
  echo -n "Checking $source : "
  cd $PKGBUILD
  tarball="$(ls ${source}*tar* 2>/dev/null)"
  if [ "$tarball" == "" ] ; then
    echo "not found :("
    exit 1
  else
    echo "found $tarball :)"
  fi
done

cd $PKGBUILD
for source in $sources ; do

  cd $PKGBUILD
  tarball="$(ls ${source}*tar* )"

  rm -rf $TMP
  mkdir -p $TMP
  
  if [ "$(echo $tarball | grep ".tar.bz2" )" ]; then
      tar jxvf $tarball -C $TMP
  elif [ "$(echo $tarball | grep ".tar.gz" )" ]; then
      tar zxvf $tarball -C $TMP
  fi
  
  SRC=$(ls -d $TMP/*/)

    cd $SRC
    chown -R root.root .
    perl Makefile.PL 
    make || exit 1
    make install DESTDIR=$PKG || exit 1

    mkdir -p $PKG/usr/doc/$(basename $SRC)
    cp -a \
      AUTHORS COPYING INSTALL NEWS README TODO ChangeLog doc \
      $PKG/usr/doc/$(basename $SRC)

  cd $PKGBUILD
done


# Strip everything:
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)


# Man pages
( 
  mkdir -p $PKG/usr/man
  mv $PKG/usr/share/man/* $PKG/usr/man
  rm -rf $PKG/usr/share/man
  cd $PKG/usr/man
  find . -name "*.?" | xargs gzip -f -9
)

# "site_perl" is for user custom added modules, not for us
rm -rf $PKG/usr/share
cp -rf $PKG/usr/lib/perl5/site_perl/$VERSION/* $PKG/usr/lib/perl5/$VERSION/
rm -rf $PKG/usr/lib/perl5/site_perl/$VERSION/*

# perllocal is useless for in default distribution package
find $PKG/usr/lib/perl5 -name perllocal.pod | xargs rm -f

# Adjust owner/perms to standard values:
(
	cd $PKG
	chown -R root:root .
	find . -perm 555 -exec chmod 755 {} \;
	find . -perm 444 -exec chmod 644 {} \;
)
cd $PKGBUILD


# Slack desc

mkdir -p $PKG/install
cat << EODESC > $PKG/install/slack-desc
$NAME: $NAME 
$NAME:
$NAME: Larry Wall's "Practical Extraction and Report Language".  Perl is a
$NAME: language optimized for scanning arbitrary text files, extracting
$NAME: information from those text files, and printing reports based on that
$NAME: information.  It's also a good language for many system management
$NAME: tasks.  The language is intended to be practical (easy to use,
$NAME: efficient, complete) rather than beautiful (tiny, elegant, minimal).
$NAME:
$NAME:
$NAME:
EODESC

# Welcome home

mkdir -p $PKG/usr/src/$NAME-$VERSION
cp $PKGBUILD/build-$NAME.sh $PKG/usr/src/$NAME-$VERSION/

# And finally, a package

cd $PKG
makepkg  -l y -c n $PKG.txz

# cleanup
rm -rf $PKG
rm -rf $TMP


