#!/bin/sh
#
# File: 25-log-to-disk\&.autorun
# 
# Set up syslog so that it will log to a disk file, named with the
# current date.
# Rotate at midnight.
# Do not rotate by size.
# /var/log/syslog always is a symlink to the current logfile.
#

### For development & testing.
[[ "${0:0:5}" == "/cifs" ]] && exit

# If we are already logging to a disk, quit now.
if ( /bin/ps | grep -q "syslog[d] .*-O " ) ; then exit ; fi

#######################################################################
#######################################################################
# Define the syslogd parameters.
# 5000 9 = 5MB logfile size, keep 9 backups
PARAMS="5000 9"


# If there is a 1st parameter (which there will be, if this is autorun'ed) 
# then use it as the disk to log to, IFF there is a "log" directory on it.
# Otherwise use this hardcoded default diskname.
DISK=/mnt/TOSH_120

# Pick the format according to the frequency that you
# have it rolling over to a new file (see cru command line at the bottom).
# Comment out the date formats you don't want.
SUFFIX='$(date +%F_%H%M)'
#SUFFIX='$(date +%F)'
#SUFFIX='$(date +%F_%H%M_%S)'

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

# Define the syslogd parameters.
if [[ "$1" != "" ]] ; then
    if [[ -d $1/log ]] ; then
	DISK=$1
    fi
fi
LOGDIR=$DISK/log

FILNAM=syslog_
eval EXPFN=${SUFFIX}
FFN=$LOGDIR/${FILNAM}${EXPFN}

# Quit if we can't write to the file.
mkdir -p $LOGDIR
(( ! touch  $FFN 2>/dev/null )) && exit


# Get whatever has been logged since bootup and restart with the new logfile.
# Note: Until the system is totally up and stable it takes about 5 to 50 seconds
# to restart logging.  Anything that is logged in this interval is lost.
# So.....lets just sleep until the sysup signal or we've been up for 5 minutess.
A=600
while [ $A -gt 0 ] && [ $(cut -f1 -d"." /proc/uptime) -lt 300 ]  ; do
A=$(( $A - 1 ))
if [ -f /var/notice/sysup ] ; then  break; fi
sleep 1
done

echo "$PARAMS $FFN" >/etc/syslogd.cfg

# Do some fancy dancing to try to keep all the log entries.
cat /var/log/messages.0 /var/log/messages 2>/dev/null >> $FFN
rm -f /var/log/messages.0 /var/log/messages 
service logging restart
cat /var/log/messages.0 /var/log/messages 2>/dev/null >> $FFN
rm -f /var/log/messages.0 /var/log/messages 
ln -sf $FFN /var/log/syslog

# Setup to reset logging when this disk is unmounted.
mkdir -p /etc/config
RESETNAME=/etc/config/25-reset-log-${DISK##*/}.autostop
cat <<-EOF  > $RESETNAME 
#!/bin/sh
if [[ "\${1#/tmp/}" != "${DISK#/tmp/}" ]] ; then  exit ; fi
rm -f /etc/syslogd.cfg
rm -f $RESETNAME 
service logging restart
EOF
chmod 0777 $RESETNAME

# Set a cron job to rotate at midnight.
#    * * * * *   = every minute
#    */5 * * * * = every 5 minutes
#    0 * * * *   = every hour
#    0 0 * * *   = every day at midnight
#    0 0 * * 0   = once a week, sunday midnight
# Note:  '${}'  means to use the value that it is right now (in this script).
#        '\${}' means to use the value that it is when the cron job executes.

cru a logrot "0 0 * * 0  EXPFN="$SUFFIX" ; \
echo \"$PARAMS $LOGDIR/${FILNAM}\${EXPFN}\"  >/etc/syslogd.cfg; \
service logging restart ; \
ln -sf $LOGDIR/${FILNAM}\${EXPFN} /var/log/syslog"
