#!/bin/sh
# $1 == Region
# $2 == Server
# $3 == OrigStr
# $4 == Email File
touch /mnt/ramdisk/getlogs.progress

msg()
{
    echo $1
    echo $1 >> /mnt/ramdisk/getlogs.progress
}
msg "STATUS: getting assured logs region $1 ..."
cd /mnt/ramdisk

# make sure we are not loading code to ourselves
source "fcns.shinc";  # lib of shell fcns
isPfuInProgress pfuInProgress
if $pfuInProgress
then
    msg "ERROR: Code load is running on this controller, get logs cannot continue."
    msg "RETURN_CODE: 12"
    rm /mnt/ramdisk/getlogs.progress
    exit 1
fi

touch /mnt/ramdisk/getManagedLogsInProgress
source /app/etc/app.env
mydate=`date +%Y_%m_%d__%H_%M_%S`
logfifo=$1_$mydate.logs
#logfifo=logs.logs
assuredzipfile=$1_$mydate.zip
zipfile=managedlogs.zip
mkfifo /mnt/ramdisk/$logfifo
zip $zipfile $logfifo &
su -c "wrapcli --cmd 'get logs /mnt/ramdisk/$logfifo region $1'" -s /bin/sh admin

if [ "$1" == "all" ]
then
    /usr/bin/getmclogs 
fi
rm /mnt/ramdisk/$logfifo

msg "Done getting managed logs region $1 ..."
mv $zipfile $assuredzipfile

TO=`sed -n '1,1 p' < $4 |  awk '{print $2}'`
FROM=`sed -n '2,1 p' < $4 |  awk '{print $2}'`
SUBJECT=`sed -n '3,1 p' < $4 |  awk '{print $2,$3,$4,$5,$6}'`
sed -n '1,4 !p' < $4  > /mnt/ramdisk/emailOut/temp
msg "To:$TO From:$FROM Subject:$SUBJECT"
/bin/makemime -c multipart/mixed -o /mnt/ramdisk/gtemp -a "Subject: $SUBJECT" /mnt/ramdisk/emailOut/temp $assuredzipfile
/sbin/sendmail -S $2 -f $FROM  -t $TO  < /mnt/ramdisk/gtemp
msg "Done sending managed logs email $2 $3 $4 ..."

rm $4
rm $assuredzipfile
rm /mnt/ramdisk/emailOut/temp
rm /mnt/ramdisk/gtemp
rm /mnt/ramdisk/getManagedLogsInProgress
msg "Managed logs complete."

mv /mnt/ramdisk/getlogs.progress managedlogs.last

exit 0
