#!/bin/bash
#
# __copy1__
# __copy2__
#
CMD=$(basename $0)
CMDVER="1.2"
CMDSTR="$CMD v$CMDVER (2021-11-17)"

now=$(date '+%Y%m%d-%H%M%S')
num=0


logdir="/var/log/__TOOLKIT__"

[ -d $logdir ] || {
	mkdir -p $logdir
}

cd $logdir

if [ "$(ls -d [0-9][0-9][0-9][0-9] 2>/dev/null)" == "" ]
then
	num="0000"
else
	num=$(ls -d [0-9][0-9][0-9][0-9] | tail -1)
	num=$(expr $num + 1)
	num=$(printf "%04d" $num)
fi

export LOGDIR="$logdir/$num"
export LOGFILE="$LOGDIR/$now.log"
export WARNFILE="$LOGDIR/$now.warn"

mkdir -p $LOGDIR

:> $LOGFILE
:> $WARNFILE

chmod 770 /var/log/__TOOLKIT__ $LOGDIR
chmod 660 $LOGFILE $WARNFILE

cd $logdir

rm -f __TOOLKIT__.log  ; ln -s $LOGFILE __TOOLKIT__.log
rm -f __TOOLKIT__.warn ; ln -s $WARNFILE __TOOLKIT__.warn

echo "recording on logfile: $LOGFILE, $WARNFILE"
exec kusa-reconf "$@" 2>&1 | tee $LOGFILE
