#!/bin/sh

operating_system='Linux'
if [ "x${1}" != "x" ]; then
    operating_system=$1
fi

onapp_version='6.8'
PARAMETERS="LABEL TMPLVERSION CREATED VIRTUALIZATION DISKSIZE MIN_MEMORY_SIZE"
out_file_suf=''
caption=
if [ "x${2}" != "x" ]; then
    onapp_version="$2"
    out_file_suf="_$2"
fi

if echo $onapp_version | grep '^3.0' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^3.1' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^3.2' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^3.3' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^3.4' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^3.5' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^4.0' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^4.1' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^4.2' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^4.3' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.0' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.1' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.2' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.3' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.4' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.5' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.6' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.7' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.8' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.9' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.10' > /dev/null 2>&1 ; then
#|| echo $onapp_version | grep '^6.0' > /dev/null 2>&1 \
#|| echo $onapp_version | grep '^6.1' > /dev/null 2>&1 \
#|| echo $onapp_version | grep '^6.2' > /dev/null 2>&1 \
#|| echo $onapp_version | grep '^6.3' > /dev/null 2>&1 \
#|| echo $onapp_version | grep '^6.4' > /dev/null 2>&1 \
#|| echo $onapp_version | grep '^6.5' > /dev/null 2>&1 \
    if [ "x${operating_system}" = "xLinux" ]; then
        PARAMETERS="${PARAMETERS} EXT4"
    fi
fi
if echo $onapp_version | grep '^3.1' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^3.2' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^3.3' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^3.4' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^3.5' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^4.0' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^4.1' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^4.2' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^4.3' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.0' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.1' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.2' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.3' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.4' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.5' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.6' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.7' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.8' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.9' > /dev/null 2>&1 \
|| echo $onapp_version | grep '^5.10' > /dev/null 2>&1; then
#|| echo $onapp_version | grep '^6.0' > /dev/null 2>&1 \
#|| echo $onapp_version | grep '^6.1' > /dev/null 2>&1 \
#|| echo $onapp_version | grep '^6.2' > /dev/null 2>&1 \
#|| echo $onapp_version | grep '^6.3' > /dev/null 2>&1 \
#|| echo $onapp_version | grep '^6.4' > /dev/null 2>&1 \
#|| echo $onapp_version | grep '^6.5' > /dev/null 2>&1; then
    PARAMETERS="${PARAMETERS} BAREMETAL SMART"
    PARAMETERS="${PARAMETERS} ONAPP_VERSION_MIN_REQ"
    caption="OnApp version <b>${onapp_version}</b>"
fi

root_dir='/home/templates/template/public_html'
#log_dir='/home/templates/template/public_html/log'
log_dir='/var/log'
tmp_dir='/tmp'
out_file="${tmp_dir}/${operating_system}${out_file_suf}_templates.txt"
html_file="${root_dir}/${operating_system}${out_file_suf}_templates.html"
log_file="${root_dir}/${operating_system}${out_file_suf}_generate_templates.log"
#log_file="${tmp_dir}/${operating_system}${out_file_suf}_generate_templates.log"
generate_extra_options='' # -z

rm -f ${out_file}

echo "Started ${operating_system} templates list generation at `date`" >> ${log_file}

${root_dir}/generate-onapp-templates-sql.sh ${generate_extra_options} -m TXT -p "${PARAMETERS}" -l ';' -v ${onapp_version} -5 -d ${root_dir}/${operating_system}/ -t \*.tar.gz -o ${out_file} >> ${log_file} 2>&1 

if [ ! -e ${out_file} ]; then
    echo "FAILED ${operating_system} templates list generation at `date`" >> ${log_file}
    exit 1
fi

sed -i 's/.\+Load Balancer Virtual Appliance.\+//g;s/^;/<tr><td>/g;s/$/<\/td><\/tr>/g;s/;/<\/td><td style="text-align:right">/g;s/LABEL/<b>Template Name<\/b>/g;s/TMPLVERSION/<b>Version<\/b>/g;s/VIRTUALIZATION/<b>Virtualizations<\/b>/g;s/DISKSIZE/<b>min. Disk (Gb)<\/b>/g;s/MIN_MEMORY_SIZE/<b>min. RAM (Mb)<\/b>/g;s/RESIZE/<b>Resizable<\/b>/g;s/MIGRATE/<b>Hot-migrate<\/b>/g;s/ONAPP_VERSION_MIN_REQ/<b>OnApp min. required version<\/b>/g;s/EXT4/<b>ext4<\/b>/g;s/BAREMETAL/<b>Baremetal<\/b>/g;s/SMART/<b>SmartServer<\/b>/g;s/CREATED/<b>Created at<\/b>/g;' ${out_file} >> ${log_file} 2>&1

if [ ! -e ${out_file} ]; then
    echo "FAILED ${operating_system} templates list generation at `date`" >> ${log_file}
    exit 1
fi

table_caption=
if [ "x${operating_system}" = "xLinux" ]; then
    table_caption="<br>Please note: the <i>Resizable</i> and <i>Hot-migrate</i> values are only for <b><i>Xen 3.x</i></b> hypervisors on <b><i>RHEL/CentOS 5.x</i></b>"
elif [ "x${operating_system}" = "xWindows" -o "x${operating_system}" = "xFreeBSD" ]; then
    table_caption="<br>Please note: the <i>Resizable</i> and <i>Hot-migrate</i> values are only for <b><i>Xen 3.x</i></b> and <b><i>KVM</i></b> hypervisors on <b><i>RHEL/CentOS 5.x</i></b>"
fi
table_caption="<br><br>"

echo "<!DOCTYPE html>
<html>
<body>
<table border=\"0\" width=\"100%\">
<caption>${caption}<br><b>${operating_system}</b> templates list [`date`]${table_caption}</caption>
" > ${html_file}

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

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

if [ ! -e ${html_file} ]; then
    echo "FAILED ${operating_system} templates list generation at `date`" >> ${log_file}
    exit 1
else
    rm -f ${out_file}
    echo "Finished ${operating_system} templates list generation at `date`" >> ${log_file}
fi

exit 0
