#!/bin/bash
# This is lbox client script
# It renames qad raw output file and sends it to lbox windows service
# $1 = raw file name, mandatory
# $2 = section name, optional, defaults to "report" if not set
# $3 = email selection, optional, defaults to "noemail" if not set

rfile=$PWD/$1
section=${2:-report}
email=${3:-noemail}

JAVA_HOME=/qad/jre1.5.0_12
LBOX_HOME=$(dirname $0)
LBOX_EMAIL=$LBOX_HOME/lbox.email

[ -s $rfile ] || (echo "no data"; exit 1) || exit 1

nfile=$PWD/`date +'%Y%m%d%H%M%S'`$RANDOM.txt

grep ^"$email^" ${LBOX_EMAIL} | (IFS=^; while read email address subject name body
do
  echo -e '\002'email:${address}^${subject} `date`^${name}^${body}
done) > $nfile

cat $rfile >> $nfile || exit 1

cd ${LBOX_HOME}
$JAVA_HOME/bin/java -jar LBOX2c.jar $nfile $section

rm $nfile $rfile 2>/dev/null

exit 0
