#!/bin/sh

onapp_version='3.0.5'

operating_systems="Linux-testing"

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_test.list"
out_file="${root_dir}/generate_new_templates_test.txt"
html_file="${root_dir}/generate_new_templates_test.html"
log_file="${log_dir}/generate_new_templates_test.log"
generate_extra_options=''

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

echo "Started $operating_systems templates to test new list 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`
    sh -c "curl -X POST http://109.123.127.88:8080/job/HotResize%20matrix%20tests/build -u 'resultviewer:7QevDjyhmpaBCBKLBzYBNGdWaXYiLrmBR8wuzhKJ' --data 'token=7QevDjyhmpaBCBKLBzYBNGdWaXYiLrmBR8wuzhKJ' --data-urlencode json='{\"parameter\": [{\"name\":\"TEMPLATE_FILE_NAME\", \"value\":\"$template_name\"}]}'"
    echo $template_name >> ${out_file}
    echo "Send request for the $template_name" >> ${log_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

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

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

exit 0
