#!/bin/sh

onapp_version='3.0.5'

operating_systems="Linux FreeBSD Windows/XEN Windows/KVM Windows/BAREMETAL"
#to_addresses="artem.vasiliev@virtuozzo.com, vitaliy.novoseletskyy@onapp.com, vlukashkin@onapp.com, vova@onapp.com, techsupportteam@onapp.com, admins@onapp.com, onapptemplatesconfluence@gmail.com, oleksandr.oleynik@onapp.com tetyana.tsyupka@onapp.com artem.pavliuchenko@onapp.com"
to_addresses="artem.vasiliev@virtuozzo.com"
from_address="ykohut@onapp.com"

templates_url='http://templates.repo.onapp.com'

log_dir='/var/log'
root_dir='/home/templates/template/public_html'
diff_file="${root_dir}/generate_new_templates_email.list"
out_file="${root_dir}/generate_new_templates_email.txt"
html_file="${root_dir}/generate_new_templates_email.html"
log_file="${log_dir}/generate_new_templates_email.log"
generate_extra_options=''

rm -f ${out_file} ${diff_file}.new

echo "Started new templates list e-mail generation at `date`" >> ${log_file}

cd ${root_dir} || exit 1
find $operating_systems -name \*.tar.gz | sort --dictionary-order --ignore-leading-blanks --ignore-case --output=${diff_file}.new

if [ ! -e ${diff_file} -o ! -e ${diff_file}.new ]; then
    echo "The ${diff_file} or ${diff_file}.new doesn't exists."
fi

for template in `diff ${diff_file} ${diff_file}.new | egrep '>[[:space:]]*' | sed 's/>[[:space:]]*//g' 2>/dev/null`; do
    template_dir=`dirname $template`
    template_name=`basename $template`
    template_url="<a href=\"${templates_url}/${template_dir}/${template_name}\">${template_name}</a>"
    ${root_dir}/generate-onapp-templates-sql.sh ${generate_extra_options} -s -z -5 -m TXT -p "LABEL TMPLVERSION FILE" -l ';' -v ${onapp_version} -d ${root_dir}/${template_dir} -t $template_name 2>>${log_file} | egrep -v -e 'LABEL|DONE' 2>>${log_file} | egrep -v -e '^$' 2>>${log_file} | sed "s#$template_name#$template_url#g" 2>>${log_file} >> ${out_file} 
    unset template_dir template_name template_url
done

if [ ! -e ${out_file} ]; then
    rm -f ${out_file} ${diff_file}.new ${html_file}
    echo "No new templates found at `date`" >> ${log_file}
    exit 0
fi

sed -i 's/.\+Load Balancer Virtual Appliance.\+//g;s/^;/<tr><td>/g;s/$/<\/td><\/tr>/g;s/;/<\/td><td>/g;s/LABEL/Template Name/g;s/TMPLVERSION/Version/g;s/VIRTUALIZATION/Virtualizations/g;s/DISKSIZE/min. Disk (Gb)/g;s/MIN_MEMORY_SIZE/min. RAM (Mb)/g;s/RESIZE/Resizable/g;s/MIGRATE/Hot-migrate/g;s/ONAPP_VERSION_MIN_REQ/OnApp version/g;s/FILE/File Name/g;' ${out_file} >> ${log_file} 2>&1

table_caption=

echo "
<!DOCTYPE html>
<html>
<body>
<table border=\"0\">
<tr><td><b><i>Template Name</i></b></td><td><b><i>Version</i></b></td><td><b><i>File Name</i></b></td><tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
" > ${html_file}

cat ${out_file} 1>>${html_file} 2>>${log_file}

echo "
</table>
</body>
</html>" >> ${html_file}

html_data=`cat ${html_file}`

(
echo "To: $to_addresses"
echo "From: $from_address"
echo "Subject: OnApp templates updated [`date +\"%Y/%m/%d %R\"`]"
echo "Content-Type: text/html"
echo "MIME-Version: 1.0"
echo
echo "$html_data"
echo
) | /usr/sbin/sendmail -t

mv -f ${diff_file}.new ${diff_file}
rm -f ${out_file} ${diff_file}.new ${html_file}

echo "Finished new templates list e-mail generation at `date`" >> ${log_file}
cd - >/dev/null 2>&1 || exit 1

exit 0
