#!/bin/bash
##################################################################
#   raider - converts a single disk linux system in to a software raid system
#            with two or mor disks
# 
#   Copyright (C) 2012 M.Carreira - Under GNU General Public License
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.

#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# This is a wrapper script to allow raider to create debug logs.
# By default, when running a command, a debug logfile is writen.
# When running a command with --debug option, all the debug list also scrolls in the screen
# Each command option generates a different name debug logfile with a suffix identifying the command option  

args=$*
export RAIDER_LIB=/usr/lib/raider
export R_LIB=/var/lib/raider
export PROGRAM_NAME="${0##*/}"	# The program name
export COLUMNS=$(tput cols)	# avoid tput stderr value 80 after DEBUG_LOG
export LOGDIR=/var/log
dirlog=${LOGDIR}/raider

timelog=$(date +%F" "%T)
if (echo "$args" | grep -q "\-R[0-9]\+"); then
	suffix=$( echo "${args}" |  sed "s/.*\-\(R[0-9]\+\).*/_\1_/g")
elif (echo "$args" | grep -q "\-\-single\-to\-raid1"); then
	suffix="_R1_"
elif (echo "$args" | grep -q "\-\-single\-to\-raid4"); then
	suffix="_R4_"
elif (echo "$args" | grep -q "\-\-single\-to\-raid5"); then
	suffix="_R5_"
elif (echo "$args" | grep -q "\-\-single\-to\-raid6"); then
	suffix="_R6_"
elif (echo "$args" | grep -q "\-\-single\-to\-raid10"); then
	suffix="_R10_"
elif (echo "$args" | grep -q "\-d"); then
	suffix="_D_"
elif (echo "$args" | grep -q "\-h"); then
	suffix="_H_"
elif (echo "$args" | grep -q "\-e"); then
	suffix="_E_"
elif (echo "$args" | grep -q "\-V"); then
	suffix="_V_"
elif (echo "$args" | grep -q '\-\-run'); then
	if [ -f ${R_LIB}/step ]; then
		NSTEP=$(head -n1 ${R_LIB}/step)
		suffix="_STEP${NSTEP}_"
	fi
fi

NAME_LOG="raider_debug${suffix}.log"
export DEBUG_LOG=${dirlog}/${NAME_LOG}
OUTPUT_LOG=${dirlog}/raider_debug_OUTPUT${suffix}.log
echo $timelog >$DEBUG_LOG
echo "Command: raider $args" >> $DEBUG_LOG
echo "User: ${SUDO_USER:=$USER}" >> $DEBUG_LOG
echo "---------------------------" >> $DEBUG_LOG
echo $timelog >$OUTPUT_LOG
chmod 666 $DEBUG_LOG 2>/dev/null
chmod 666 $OUTPUT_LOG 2>/dev/null
unset suffix
if (echo "$args" | grep -q '\-\-debug'); then
	${RAIDER_LIB}/raider $args 2>&1 | tee -a $DEBUG_LOG
else
	${RAIDER_LIB}/raider $args --debug 2>>$DEBUG_LOG | tee -a $OUTPUT_LOG
fi

