#!/bin/sh
#
# Copyright (c) 2007-2008 Marco Antonio Frias B. <marcofrias@linuxpackages.net>
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this script for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
#   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
#
# Based on Template SlackBuild written by Eric Hameleers <alien@slackware.com>
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild Script
# ===========================
#
# By:    Marco Antonio Frias B. <marcofrias@linuxpackages.net>
# For:   autopsy
# Descr: Forensic Browser for use with Sleuth Kit
# URL:   http://www.sleuthkit.org/autopsy/
# Needs: sleuthkit
#
# Changelog:
#
# -- Slackware 12.1
#
# 2.20-1: 30/Oct/2008 by Marco Antonio Frias B. <marcofrias@linuxpackages.net>
#         Release new version 2.20.
#
# 2.10-1: 10/Sep/2008 by Marco Antonio Frias B. <marcofrias@linuxpackages.net>
#         * Initial build.
#
# NOTE:
#      Run 'sh autopsy.SlackBuild --cleanup' to build a Slackware package.
# -----------------------------------------------------------------------------

## Name and version of the package
PRGNAME=autopsy
VERSION=2.20

## The arch we will build for
ARCH=${ARCH:-noarch}

## Build tag
RELEASE=1
BUILDTAG=mfb

## The documentation files:
DOCFILES="CHANGES.txt COPYING INSTALL.txt README* TODO.txt \
          docs/sleuthkit-informer-13.txt"

## Where do we look for sources?
CWD=$(pwd)
SRCDIR=$(dirname $0)
[ "${SRCDIR:0:1}" == "." ] && SRCDIR=$CWD/${SRCDIR}

## Place to build (TMP) package (PKG) and output (OUTPUT) the program:
TMP=${TMP:-/tmp/build}
PKG=$TMP/package-${PRGNAME}
OUTPUT=${OUTPUT:-/usr/src/slackpackages}

## Full path of source:
SOURCE="$SRCDIR/${PRGNAME}-${VERSION}.tar.gz"

##
## --- with a little luck, you won't have to edit below this point ---
##

## Exit the script on errors:
set -e
trap 'echo "$0 FAILED at line ${LINENO}" | tee $TMP/error-${PRGNAME}.log' ERR

## Catch unitialized variables:
set -u
P1=${1:-1}

## Create working directories:
mkdir -p $OUTPUT             # place for the package to be saved
mkdir -p $TMP/tmp-${PRGNAME} # location to build the source
mkdir -p $PKG                # place for the package to be built

rm -rf $PKG/*                # always erase old package's contents
rm -rf $TMP/tmp-${PRGNAME}/* # remove the remnants of previous build

## Source file availability:
if ! [ -f ${SOURCE} ]; then
  echo -e "\E[1;31m"
  echo -ne "File '$(basename ${SOURCE})' not available... aborting the build."
  echo -e "\E[0m\n"
  exit 1
fi

## --- PACKAGE BUILDING ---
cd $TMP/tmp-${PRGNAME}

echo -e "\E[1;32m"
echo -ne "Extracting the source archive(s) for $PRGNAME..."
echo -e "\E[0m\n"
sleep 1

if $(file ${SOURCE} | grep -q ": bzip2"); then
  tar -xjvf ${SOURCE}
elif $(file ${SOURCE} | grep -q ": gzip"); then
  tar -xzvf ${SOURCE}
fi

cd ${PRGNAME}-${VERSION}
find . \( -perm 777 -o -perm 775 -o -perm 711 \) -exec chmod 755 {} \;
find . \( -perm 555 -o -perm 511 \) -exec chmod 755 {} \;
find . \( -perm 666 -o -perm 664 -o -perm 600 \) -exec chmod 644 {} \;
find . \( -perm 444 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;
chown -R root.root .

echo -e "\E[1;32m"
echo -ne "Installing $PRGNAME..."
echo -e "\E[0m\n"
sleep 1

mkdir -p $PKG/usr/lib/autopsy
echo "#!/usr/bin/perl -wT" > autopsy
echo "use lib '/usr/lib/autopsy/';" >> autopsy
echo "use lib '/usr/lib/autopsy/lib/';" >> autopsy
cat base/autopsy.base >> autopsy
sed -i 's|conf.pl|/etc/autopsy.pl|' autopsy lib/Main.pm
install -m 755 autopsy $PKG/usr/lib/autopsy

mkdir -p $PKG/usr/lib/autopsy/help
install -m 644 help/* $PKG/usr/lib/autopsy/help

mkdir -p $PKG/usr/lib/autopsy/lib
install -m 644 lib/* $PKG/usr/lib/autopsy/lib

mkdir -p $PKG/usr/lib/autopsy/pict
install -m 644 pict/* $PKG/usr/lib/autopsy/pict

mkdir -p $PKG/usr/bin
( cd $PKG/usr/bin ; ln -sf /usr/lib/autopsy/autopsy autopsy )

mkdir -p $PKG/etc
install -m 644 $SRCDIR/autopsy.pl $PKG/etc/autopsy.pl.new

mkdir -p $PKG/usr/man/man1
install -m 644 man/man1/autopsy.1 $PKG/usr/man/man1

## Add documentation:
mkdir -p $PKG/usr/doc/${PRGNAME}-${VERSION}
cp -a $DOCFILES $PKG/usr/doc/${PRGNAME}-${VERSION} || true
find $PKG/usr/doc/${PRGNAME}-${VERSION} -type d -exec chmod 755 {} \;
find $PKG/usr/doc/${PRGNAME}-${VERSION} -type f -exec chmod 644 {} \;
chown -R root.root $PKG

## Compress man pages:
if [ -d $PKG/usr/man ]; then
  (cd $PKG/usr/man
   for manpagedir in $(find . -type d -name "man*"); do
   (cd $manpagedir
    for eachpage in $(find . -type l -maxdepth 1); do
      ln -s $( readlink $eachpage ).gz $eachpage.gz ; rm $eachpage
    done
    gzip -9 *.?
   )
   done
  )
fi

echo -e "\E[1;32m"
echo -ne "Building package..."
echo -e "\E[0m\n"
sleep 1

## Add slackbuild files:
mkdir -p $PKG/usr/src/slackbuilds/${PRGNAME}-${VERSION}

(cd $PKG/usr/src/slackbuilds/${PRGNAME}-${VERSION}
install -m 644 $SRCDIR/slack-desc .
install -m 644 $SRCDIR/slack-required .
install -m 644 $SRCDIR/doinst.sh .
install -m 644 $SRCDIR/${PRGNAME}.info .
install -m 644 $SRCDIR/${PRGNAME}.SlackBuild .
install -m 644 $SRCDIR/${PRGNAME}.pl .
)

# Add a package description:
mkdir -p $PKG/install
install -m 644 $SRCDIR/slack-desc $PKG/install
install -m 644 $SRCDIR/slack-required $PKG/install
install -m 644 $SRCDIR/doinst.sh $PKG/install

## Build the package:
cd $PKG
makepkg -l y -c n $OUTPUT/${PRGNAME}-${VERSION}-${ARCH}-${RELEASE}${BUILDTAG}.tgz \
  2>&1 | tee $TMP/makepkg-${PRGNAME}.log

echo -e "\E[1;32m"
echo -ne "Your $PRGNAME package is complete. It is located in $OUTPUT"
echo -e "\E[0m\n"

if [ "$P1" = "--cleanup" ]; then
  rm -rf $TMP/tmp-${PRGNAME}
  rm -rf $PKG
fi
