#!/bin/bash
################################################################################
# psad.SlackBuild -- v1.3 -- pyllyukko <~> maimed <~> org -- 26.1.2007 ->      #
#                                                                              #
# 5.4.2008: added -b to cd_rpmbuilder                                          #
#           added 'set -e'                                                     #
#                                                                              #
# NOTES:                                                                       #
#   - you might wan't to change %_sysconfdir in                                #
#     /usr/lib/rpm/`uname -m`-linux/macros                                     #
#   - you can build a different version of the program by executing:           #
#     'PSAD_VERSION=x.y.z bash ./psad.SlackBuild'                              #
#                                                                              #
# TODO:                                                                        #
#   - add slack-desc                                                           #
################################################################################
set -e
declare -r  RPM_BUILDER="http://www.cipherdyne.org/scripts/cd_rpmbuilder.tar.gz"
declare -r  RPM_ROOT_DIR="/usr/src/rpm"
declare -r  ARCH="i386"
declare -ri BUILD=1
PSAD_VERSION=${PSAD_VERSION:-`wget --no-verbose --output-document=- http://www.cipherdyne.org/psad/psad-latest`} || {
  echo "error at line $[${LINENO}-1]!" 1>&2
  exit 1
}
################################################################################
wget --no-verbose --output-document=- "${RPM_BUILDER}" | tar xz --to-stdout | perl -- - -p psad -r "${RPM_ROOT_DIR}" -b "${PSAD_VERSION}"
[ $[ ${PIPESTATUS[0]} | ${PIPESTATUS[1]} | ${PIPESTATUS[2]} ] -ne 0 ] && {
  echo "error at line $[${LINENO}-2]!" 1>&2
  exit 1
}
[ ! -f "${RPM_ROOT_DIR}/RPMS/${ARCH}/psad-${PSAD_VERSION}-${BUILD}.${ARCH}.rpm" ] && {
  echo "error: file \`psad-${PSAD_VERSION}-${BUILD}.${ARCH}.rpm' doesn't exist!" 1>&2
  exit 1
}
pushd "${RPM_ROOT_DIR}/RPMS/${ARCH}"			|| exit 1
rpm2tgz "psad-${PSAD_VERSION}-${BUILD}.${ARCH}.rpm"	|| exit 1
mv -v "psad-${PSAD_VERSION}-${BUILD}.${ARCH}.tgz" "psad-${PSAD_VERSION}-${ARCH}-${BUILD}.tgz" || exit 1
ls -l "${RPM_ROOT_DIR}/RPMS/${ARCH}/psad-${PSAD_VERSION}-${ARCH}-${BUILD}.tgz"
exit ${?}
