#!/bin/sh

# (c) Mattias Schlenker - License GPL v2

# First try to get the spool directory:
# If the default exists, use it

spooldir=''
bindirs="/usr/sbin /usr/local/sbin /usr/libexec"
for  d in $bindirs ; do
    if [ -x "${d}/dma" ] ; then
        [ -d /var/local/spool/dma ] && spooldir='/var/local/spool/dma'
        [ -d /var/spool/dma ] && spooldir='/var/spool/dma'
    fi
done

# If the spooldir is overwritten via /etc/dma.conf or /usr/local/etc/dma/dma.conf use that instead:

for f in /etc/dma/dma.conf /usr/local/etc/dma/dma.conf ; do
    if [ -f "$f" ] ; then
	tspool=`grep '^SPOOLDIR ' "$f" | awk '{print $2}'`
        [ -n "$tspool" -a -d "$tspool" ] && spooldir="$tspool"
    fi
done

if [ -f "$MK_CONFDIR/dma.cfg" -a -x "$MK_CONFDIR/dma.cfg" ] ; then
    . "$MK_CONFDIR/dma.cfg"
fi

# If the spooldir has not been found, exit.
[ -z "$spooldir" ] && exit 0

# Getting funny... Let's find out where we are working on...
os=`uname`

spoolsize=`du -k -s "$spooldir" | awk '{print $1}'`
spoolsize=$(( $spoolsize * 1024 ))
spoolcount=`find "$spooldir" -type f -name 'Q*.*' | wc -l`
spoololdest="-1"
flushmtime="-1"
# Do not use globbing to keep the command line short!
spoololdestfile=`ls -tr "$spooldir" | grep '^Q' | tail -n 1`

case $os in
    Linux)
        [ -n "$spoololdestfile" ] && spoololdest=`stat -c %Y "${spooldir}/${spoololdestfile}"`
        [ -f "${spooldir}/flush" ] && flushmtime=`stat -c %Y "${spooldir}/flush"`
    ;;
    *BSD|DragonFly)
        [ -n "$spoololdestfile" ] && spoololdest=`stat -f %m "${spooldir}/${spoololdestfile}"`
        [ -f "${spooldir}/flush" ] && flushmtime=`stat -f %m "${spooldir}/flush"`
    ;;
esac

echo '<<<dma_spool>>>'
echo "spooldir $spooldir"
echo "spoolsize $spoolsize"
echo "spoolcount $spoolcount"
echo "spoololdest $spoololdest"
echo "flushmtime $flushmtime"
