#!/bin/sh
#   Testing newsyslog...

PATH=/bin:/usr/bin:/usr/sbin

ACTION="$1"
DSTAMP="`date`"
STATFMT="%i%t%l %Sp %Su:%Sg%t%z%t%N"
BASEOUT=/var/tmp/log-nsl/standard
TESTOUT=/var/tmp/log-nsl/neworder
TESTCONF=/var/tmp/log-nsl/newsyslog-neworder.conf
if [ ! -d /var/tmp/log-nsl ] ; then
   mkdir /var/tmp/log-nsl
   chmod 750 /var/tmp/log-nsl
fi
#  "NOcopy" is short for "NewOrder-copy"...
LCOPY=/var/log-NOcopy

touch ${BASEOUT} ${BASEOUT}.stat1 ${BASEOUT}.stat2
touch ${TESTOUT} ${TESTOUT}.stat1 ${TESTOUT}.stat2

printf "\n# -- Run $$ of newsyslog at ${DSTAMP}\n\n" >> ${BASEOUT}
printf "\n# -- Run $$ of newsyslog at ${DSTAMP}\n\n" >> ${TESTOUT}

time newsyslog -nv -D oldorder   >> ${BASEOUT} 2>&1
time newsyslog -nv -D neworder   >> ${TESTOUT} 2>&1
printf "# -- Done with test (-nv) run.\n\n"  >> ${BASEOUT}
printf "# -- Done with test (-nv) run.\n\n"  >> ${TESTOUT}

#  It obviously takes a bunch of extra time & disk-space to duplicate
#  all of /var/log and then make both a "oldorder" run on the original
#  and a "neworder" run on the copy.  Also, if you do both runs, then
#  the test run will be re-signaling processes which do not need to
#  get signalled (because they are not writing to the copy of the log
#  files that it will be rotating).  If you do not want that extra
#  overhead, just do a regular run here, or maybe exit here and have
#  some other process do the real rotation.
case "$1" in
runold)   time newsyslog -v -D oldorder   >> ${BASEOUT}.run 2>&1
          exit 0 ;;
runnew)   time newsyslog -v -D neworder   >> ${TESTOUT}.run 2>&1
          exit 0 ;;
runboth)  ;;
testonly) printf "# -- Done with test $$\n\n"       >> ${BASEOUT}
          printf "# -- Done with test $$\n\n"       >> ${TESTOUT}
          exit 0 ;;
*)        echo "Finished with 'testonly' run of $0"
          echo "Results are in /var/tmp/log-nsl/*"
          exit 0 ;;
esac

sleep 5
if [ ! -d ${LCOPY} ] ; then
   mkdir ${LCOPY}
fi
rm -rf ${LCOPY}/* ${TESTCONF}
sed "s;/var/log/;${LCOPY}/;" /etc/newsyslog.conf > ${TESTCONF}
cd /var/log
pax -rw -p e . ${LCOPY}

cd /var/log
find . -type f | sort | xargs stat -f "${STATFMT}" > ${BASEOUT}.stat1
cd ${LCOPY}
find . -type f | sort | xargs stat -f "${STATFMT}" > ${TESTOUT}.stat1

time newsyslog -vv -D oldorder   >> ${BASEOUT} 2>&1
time newsyslog -vv -D neworder -f ${TESTCONF}  2>&1 | \
             sed "s;${LCOPY}/;/var/log/;" >> ${TESTOUT}
printf "# -- Done with real run.\n\n"     >> ${BASEOUT}
printf "# -- Done with real run.\n\n"     >> ${TESTOUT}

cd /var/log
find . -type f | sort | xargs stat -f "${STATFMT}" > ${BASEOUT}.stat2
cd ${LCOPY}
find . -type f | sort | xargs stat -f "${STATFMT}" > ${TESTOUT}.stat2

#  Get a sense of what changed...
diff -u ${BASEOUT}.stat1 ${BASEOUT}.stat2 >> ${BASEOUT}
printf "# -- Done with test $$\n\n"       >> ${BASEOUT}
diff -u ${TESTOUT}.stat1 ${TESTOUT}.stat2 >> ${TESTOUT}
printf "# -- Done with test $$\n\n"       >> ${TESTOUT}

