#!/bin/bash

# Lauch a script (or a command) passed as $1 
# and create a log file in $LOGDIR/$1.currenttime
# if LOG is unset. If LOG is set, output will be redirected to this file.
#
#	18/08/2004	L.Faillie	Creation
#	08/09/2004	L.Faillie	add write access for the group (to let run the
#							cleaning job).
#	13/09/2004	L.Faillie	handle job failure.
#	04/01/2015	L.Faillie	Small changes to set local environment in the script itself
#							Adapt to Gentoo
#

# load environment
# where live logs file
LOGDIR='/var/log/batches/logs'

# where to put links about failled jobs
FAILDIR='/var/log/batches/fails'

if [ $# -lt 1 ]
then
	echo '*F* Command to launch missing'
	exit 1
fi


LOG=${LOG:-${LOGDIR}/$(basename $1).$(date +'%Y%m%d.%H%M')}

SECONDS=0
if $* >${LOG} 2>&1; then
	chmod g+w ${LOG}
else
	chmod g+w ${LOG}
	ln ${LOG} ${FAILDIR};
#	chown www ${FAILDIR}/$(basename ${LOG})
fi

printf "\n-- spent %02d:%02d" $(( $SECONDS/60 )) $(( $SECONDS%60 )) >> ${LOG}
