#!/bin/bash
# Autopsy 0.90
# Cc)2004 Dagmar d'Surreal and the Dropline project
# Released under the GPL

if [ -z "$DLG_ROOT" ]; then
  export DLG_ROOT=`pwd`
fi

if [ -r $DLG_ROOT/config ]; then
  . $DLG_ROOT/config
else
  echo "Error! Unable to source global config!"
  echo ""
  exit 1
fi

if [ -r $DLG_ROOT/SCRIPTS/dropline-functions.sh ]; then
  . $DLG_ROOT/SCRIPTS/dropline-functions.sh
else
  echo "Error! Unable to source dropline-functions.sh!"
  echo ""
  exit 1
fi

if [ -z $@ ]; then
cat <<XusagedocsX
Dropline Autopsy v0.90
  Usage: $0 [packagename]

  This will bundle up the log files from your current attempt at building
  the named package and yeild you a tarball in the form of:

  NAME-VERSION-REVISION-autopsy.tgz

XusagedocsX
exit
fi

packagename=$@

if [ ! -e $DLG_ROOT/SCRIPTS/$packagename ]; then
  die "Error! Package '$packagename' does not exist!"
  exit 1
fi

if [ ! -r $DLG_ROOT/SCRIPTS/$packagename/build ]; then
  die "Error! Package '$packagename' contains no build definition!" 
fi

# Get the particularys of the package.
eval `grep "^NAME=" $DLG_ROOT/SCRIPTS/$packagename/build | head -n 1`
eval `grep "^VERSION=" $DLG_ROOT/SCRIPTS/$packagename/build | head -n 1`
eval `grep "^REVISION=" $DLG_ROOT/SCRIPTS/$packagename/build | head -n 1`
eval `grep "^PKG_ARCH=" $DLG_ROOT/SCRIPTS/$packagename/build | head -n 1`

spew 1 "Preparing autopsy on package $NAME-$VERSION-$PKG_ARCH-$REVISION"

firstarg comptype $DLG_ZPREF

case $comptype in
  bz2)
    compressor=bzip2
    ;;
  gz)
    compressor=gzip
    ;;
  *)
    die "Unrecognized compression type heads up DLG_ZPREF!"
    ;;
esac

LOGDIR=$DLG_LOGROOT/$NAME-$VERSION-$REVISION
AUTOPSYFILE=$NAME-$VERSION-$PKG_ARCH-$REVISION-autopsy.tar.$comptype

if [ ! -d $LOGDIR ]; then
  # PKG_LOGDIR is not supported and likely to be deprecated soon!
  die "No logs were found for this revision of $packagename."
fi

( cd $DLG_LOGROOT
  tar --use=$compressor -cf $DLG_ROOT/$AUTOPSYFILE $NAME-$VERSION-$REVISION/*
)

spew 1 "Autopsy file created: $AUTOPSYFILE"

