#!/bin/sh
#
# Simple logrotation for Coda server logfiles
#

vicedir=/vice
# shellcheck disable=SC1090
. "$(codaconfedit server.conf)"

rotate ()
{
    for i in $(seq 9 -1 0) ; do
        [ -f "$1-$i" ] && mv -f "$1-$i" "$1-$((i + 1))"
    done
    [ -f "$1" ] && mv -f "$1" "$1-0"
}

srvdir=$vicedir/srv
authdir=$vicedir/auth2

rotate "$srvdir/SrvLog"
rotate "$srvdir/SrvErr"
rotate "$authdir/AuthLog"

# Tell codasrv and auth2 to reopen their logfiles
[ -f "$srvdir/pid" ] && kill -HUP "$(cat "$srvdir/pid")"  2>/dev/null
[ -f "$authdir/pid" ] && kill -HUP "$(cat "$authdir/pid")" 2>/dev/null

# alternatively use one of the following,
## run volutil in the background, just in case the server isn't running.
# volutil swaplog &
# killall -HUP codasrv
#
# and,
# killall -HUP auth2
