#!/bin/sh -e

. $(dirname $0)/_common
if [ -z ${tmp+x} ]; then
    setup_tmpdir
fi

openqa_host="${openqa_host:-"https://openqa.suse.de"}"
recv="${recv:-"openqa-suse-status@suse.de"}"
html_target_file="${html_target_file:-"openqa_suse_de_status.html"}"
html_target="${html_target:-"/suse/okurz/Export/${html_target_file}"}"
openqa_review_args="${openqa_review_args:-"--host ${openqa_host} -n -r -T --query-issue-status --no-empty-sections --include-softfails --running-threshold=2 --exclude-job-groups ^(Released|Development|old)"}"
load_args="${load_args:-"--load --load-dir=${tmp}"}"
openqa_review_email_args="${openqa_review_email_args:-"${load_args}"}"
openqa_review_html_args="${openqa_review_html_args:-"${load_args}"}"
# this is also putting reminder comments on issues. We can not do this in an
# explicit later steps as we need all requests to be done when saving the data
# and if we call it here and also in a later step we would end up with
# duplicate reminder comments
openqa_review_save_args="${openqa_review_save_args:-"--reminder-comment-on-issues --save --save-dir ${tmp}"}"
openqa_review="${openqa_review:-"$(command -v openqa-review)"}"
TPL="${TPL:-"$(dirname $0)/../dashboard_files/dashboard.html.in"}"
# We have to preserve the '$@' until here to prevent too early evaluation of parameters with spaces
save_report="$(${openqa_review} ${openqa_review_args} ${openqa_review_save_args} "$@")"
email_report="$(${openqa_review} $openqa_review_args $openqa_review_email_args "$@")"
(echo -e "This is an automated message generated by 'openqa-review', see https://github.com/os-autoinst/openqa_review for details. An HTML version of this report is available on https://w3.nue.suse.com/~okurz/${html_target_file}. Status of tests and builds on ${openqa_host} as of $(date --iso-8601='seconds'):\n" && \
 echo "$email_report" && \
 echo -e "\nAny feedback regarding the script processing or contact can be communicated using issues on the github repo or directly to okurz@suse.de\n\nRegards,\nYour openqa_review") | mutt -s "Daily status from ${openqa_host}" -e 'my_hdr From: openqa-review <okurz@suse.de>' $recv

html_report="$(${openqa_review} $openqa_review_args $openqa_review_html_args "$@" | decrease_header | markdown)"
html_report_closed="$(${openqa_review} $openqa_review_args $load_args -f closed "$@" | decrease_header | markdown)"
html_report_unassigned="$(${openqa_review} $openqa_review_args $load_args -f unassigned "$@" | decrease_header | markdown)"

TPL_OPENQA_HOST=${openqa_host}

TPL_OPENQA_HEADER_LINKS=$(cat <<EOF
$(header_line "complete_report_box" "Complete report")
$(header_line "closed_box" "Closed bugs")
$(header_line "unassigned_box" "Unassigned bugs")
EOF
)

TPL_OPENQA_CONTENT=$(cat <<EOF
$(entry_block "complete_report_box" "Complete report" "${html_report}")
$(entry_block "closed_box" "Closed bugs" "${html_report_closed}")
$(entry_block "unassigned_box" "Unassigned bugs" "${html_report_unassigned}")
EOF
)

eval "cat <<EOF
$(<$TPL)
EOF
" > ${html_target}
