#!/bin/bash
#
# raiderld
# This file is part of Raider

lgfile=""
if [ $# -ne 0 ]; then
    lgfile=$1
fi
logdebug="raider_debug"
n=0
list=""
declare -a numlist

if [ -n "$lgfile" ]; then
    pwd=$(pwd)
    cd /tmp
    tar -xzf ${pwd}/${lgfile}
    cd $pwd
    logpath=/tmp/raider
else
    logpath=/var/log/raider
fi



for logfile in $(ls -tr ${logpath}/${logdebug}* 2>/dev/null)
do
	n=$((n+1))
	if [ $n -gt 1 ]; then
		list="${list}\n\t($n) ${logfile} - $(awk 'NR==1 {print}' ${logfile})"
	else
		list="\t($n) ${logfile} - $(awk 'NR==1 {print}' ${logfile})"
	fi
	numlist[$n-1]=${logfile}
done

if [ $n -eq 0 ]; then
	echo "Logfile debug does not exist!"
	exit
elif [ $n -gt 1 ]; then
	echo "Found several debug logfiles:"
	echo -e $list
	echo -en "\nSelect the logfile number: "
	read l
	echo
	if (echo $l | grep -qv "[0-9]"); then
		echo "Invalid character"
	elif [ $l -gt ${#numlist[@]} ]; then
		echo "$l does not exist"
	else
		less ${numlist[$l-1]}
	fi
else
	less $logfile
fi
if [ -n "$lgfile" ]; then
    rm -fR /tmp/raider
fi