#!/bin/sh
#
#ident  "@(#)u4ftlogctl	1.19    99/06/24"
#
#   This script manages the status log daemon u4ftlogd(1m)
#
#   It should be called with start/stop arguments as one of the rc
#   startup/shutdown scripts, and daily (with the "rotate" argument)
#   from a cron job.
#
#   Environment:
#       
#       The variable U4FTLOGPKG defines the name of the package that
#       this script is part of (used to locate the package basedir).
#       Below this, we expect to find directories "lib" and "var",
#       containing executables and log files respectively.
#
#       These two paths can be overridden by setting the variables
#       U4FTLOGPATH and U4FTLOGDIR respectively.  If these are not
#       set, and the package cannot be located, the script will exit
#       with an error.
#

# Name of package(s) containing logging binaries etc.
: ${U4FTLOGPKG:=SUNWlogu}

MYNAME="`basename $0`"
CMDS="[start|stop|suspend|resume|salvage|rotate]"

usage()
{
    echo "Usage: $MYNAME $CMDS [logname]" >&2
    exit 1
}

error()
{
    echo $MYNAME: ERROR: "$@" >&2
    exit 2
}

success()
{
    [ -n "$*" ] && echo $MYNAME: "$@" >&2
    exit 0
}

checkpid()
{
    if LOGPID=`cat $PIDFILE 2> /dev/null`
    then
        if [ -n "$LOGPID" ] && kill -0 $LOGPID 2> /dev/null
        then
            echo $LOGPID
        fi
    fi
}

report()
{
    # send a message through the logging subsystem, then wait
    # long enough for it to propagate through to the daemon
    u4ftctl report_message 0 "`date`: $*"
    sleep 3
}

setlink()
{
    /usr/bin/test "$LOGFILE" -ef "$DAYFILE" && return 1

    rm -f $LOGFILE $DAYFILE
    touch $DAYFILE
    ln -s $DAYFILE $LOGFILE
    return 0
}


#########################################################################

case "$#" in
1)
    LOGNAME="status"
    COMPRESS=0
    ;;
2)
    LOGNAME=$2
    COMPRESS=1
    ;;
*)
    usage
    ;;
esac

if [ -z "$U4FTLOGPATH" ]
then
    U4FTLOGPATH=`pkgchk -l $U4FTLOGPKG |
		sed -n '/Pathname:.*\/lib$/s,[^	 ]*[ 	],,p'`
fi
PATH="$U4FTLOGPATH:/platform/`uname -i`/lib:$PATH"
export PATH

if [ -z "$U4FTLOGDIR" ]
then
    U4FTLOGDIR=`pkgparam $U4FTLOGPKG BASEDIR`/usr/platform/`uname -i`/$U4FTLOGPKG/var
fi
[ -d $U4FTLOGDIR ] || error "Directory not found: $U4FTLOGDIR"
cd $U4FTLOGDIR

#
#   Force POSIX standard format for times & dates
#   (but the time still refers to the local timezone)
#
LC_ALL=POSIX
export LC_ALL

LOGDEV=/dev/u4ftlog:$LOGNAME
LOGFILE=u4ftlog.$LOGNAME
DAYFILE=$LOGFILE."`date '+%a'`"
PIDFILE=$LOGFILE.pid
LOGPID="`checkpid`"

case "$1" in
start)
    if [ -n "$LOGPID" ]
    then
        success "daemon already running"
    fi

    setlink
    setpgrp u4ftlogd -i $LOGDEV -o $LOGFILE < $LOGDEV &
    echo $! > $PIDFILE
    report "$LOGNAME log daemon started: process $!"
    ;;

stop)
    if [ -z "$LOGPID" ]
    then
        success "daemon not running"
    fi
    report "$LOGNAME log daemon stopped: process $LOGPID killed"
    kill -HUP $LOGPID && echo > $PIDFILE
    ;;

salvage)
    files="`u4ftlog_salvage`"
    if [ $? -eq 0 ]
    then
        report "salvaged log contents saved in $files"
    else
        report "salvage operation failed; log contents not saved"
    fi
    ;;

rotate)
    if [ -z "$LOGPID" ]
    then
        error "daemon not running"
    fi
    set -- `ls -n $LOGFILE 2> /dev/null`
    if setlink
    then
        report "end of daily $LOGNAME log"
        kill -USR1 $LOGPID
        report "start of daily $LOGNAME log"
        if [ $# -eq 11 ] && [ $COMPRESS -gt 0 ]
        then
            shift 10
            compress $1
        fi
    fi
    ;;

suspend)
    if [ -z "$LOGPID" ]
    then
        error "daemon not running"
    fi
    report "$LOGNAME logging suspended"
    u4ftctl -d $LOGDEV size 0 > /dev/null
    ;;

resume)
    if [ -z "$LOGPID" ]
    then
        error "daemon not running"
    fi
    if [ "`u4ftctl -d $LOGDEV size`" -eq 0 ]
    then
        u4ftctl -d $LOGDEV reset
        setlink
        kill -USR1 $LOGPID
        report "$LOGNAME logging resumed"
    fi
    ;;

*)
    usage
    ;;

esac
