#!/bin/sh
# Copyright (c) 2005 Eric Hameleers <alien@sox.homeip.net>
# Distributed under the terms of the GNU General Public License, Version 2
#
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild script 
# ===========================
# By:        Eric Hameleers
# For:       wakeonlan
# URL:       http://gsd.di.uminho.pt/jpo/software/wakeonlan/
# Needs:     perl
# Changelog:
# 0.40-1:    18/jan/2005 by Eric Hameleers
#            * Initial build.
# 0.41-1:    04/apr/2005 by Eric Hameleers
#            * Upgrade
# 
# Run 'sh SlackBuild --cleanup' to build a Slackware package.
# The package (.tgz) plus descriptive .txt file are created in /tmp .
# Install using 'installpkg'. 
#
# -----------------------------------------------------------------------------

# --- INIT ---
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

PRGNAM=wakeonlan
VERSION=0.41
ARCH=noarch
BUILD=1

PKG=$TMP/package-$PRGNAM

if [ ! -d $TMP/tmp-$PRGNAM ]; then
  mkdir -p $TMP/tmp-$PRGNAM # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
else
  rm -rf $PKG/*
fi


# --- PACKAGE BUILDING ---

echo "++"
echo "|| $PRGNAM-$VERSION"
echo "++"

cd $TMP/tmp-$PRGNAM

if [ ! -f $CWD/${PRGNAM}-${VERSION}.tar.gz ]; then
  echo "Retrieving missing source archive first..."
  wget -nv -O $CWD/${PRGNAM}-${VERSION}.tar.gz \
    http://gsd.di.uminho.pt/jpo/software/${PRGNAM}/downloads/${PRGNAM}-${VERSION}.tar.gz
  if [ $? -ne 0 ]; then echo "Download failed! Aborting"; exit 1; fi
fi

# --- TARBALL EXTRACTION,PATCH,MODIFY ---

echo "Extracting the source archive for $PRGNAM..."
tar -xzvf $CWD/${PRGNAM}-${VERSION}.tar.gz

cd ${PRGNAM}-${VERSION}

chown -R root.root *


# --- BUILDING ---

echo Building ...

perl Makefile.PL 2>&1 | tee $CWD/make-${PRGNAM}.log
make 2>&1 | tee -a $CWD/make-${PRGNAM}.log

#
# Install all the needed stuff to the package dir
#
# Use installwatch if available:
which installwatch > /dev/null 2>&1
if [ $? == 0 ]; then
  installwatch -o $CWD/install-${PRGNAM}.log make DESTDIR=$PKG install
else
  make DESTDIR=$PKG install 2>&1 |tee $CWD/install-${PRGNAM}.log
fi

# Clean up after install:
mv $PKG/usr/share/man $PKG/usr/
rmdir $PKG/usr/share 2>/dev/null

( cd $PKG
  # Remove 'special' files
  find . -name perllocal.pod \
    -o -name ".packlist" \
    -o -name "*.bs" \
    | xargs rm -f
  # No empty directories
  find . -type d -depth -exec rmdir {} \; 2>/dev/null
)


# --- DOCUMENTATION ---

DOCS="Changes MANIFEST README SIGNATURE examples/*"

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a $DOCS \
      $PKG/usr/doc/$PRGNAM-$VERSION
chmod -R -w $PKG/usr/doc/$PRGNAM-$VERSION/*

# Compress the man page(s)
gzip -9f $PKG/usr/man/*/*


# --- OWNERSHIP, RIGHTS ---

chmod -R o-w $PKG
chgrp root:bin $PKG/usr/bin/* $PKG/usr/sbin/* \
               $PKG/bin/* $PKG/sbin/* 2>/dev/null


# --- PACKAGE DESCRIPTION ---

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
if [ -f $CWD/doinst.sh ]; then
  cat $CWD/doinst.sh > $PKG/install/doinst.sh
fi


# --- BUILDING ---

# Build the package:
cd $PKG
makepkg --linkadd y --chown n $TMP/$PRGNAM-$VERSION-$ARCH-$BUILD.tgz \
  2>&1 | tee $CWD/makepkg-${PRGNAM}.log
(cd $TMP && md5sum $PRGNAM-$VERSION-$ARCH-$BUILD.tgz > $PRGNAM-$VERSION-$ARCH-$BUILD.tgz.md5)
cat $CWD/slack-desc | grep "^${PRGNAM}" > $TMP/$PRGNAM-$VERSION-$ARCH-$BUILD.txt


# --- CLEANUP ---

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/tmp-$PRGNAM
  rm -rf $PKG
fi
