##
 # Copyright OpenSearch Contributors
 # SPDX-License-Identifier: Apache-2.0
##

# raw note means the draft release content copied down which is generated by Github workflow
# this script helps add the URLs to all PR and have the right release note filename

import os
import sys
import fileinput
import re

link_prefix = "https://github.com/opensearch-project/dashboards-reports/tree/main/reports-scheduler"
searchExp = re.compile("([\(\[]).*?([\)\]])")

current_date = raw_input("what day is today (e.g. 2020-06-29): ")
file_path = raw_input("Path to raw note file (e.g., note.md): ")
plugin_name = "reports-scheduler"
plugin_version = raw_input('Plugin version (x.x.x.x): ')

app_num = int(
    raw_input('OpenSearch plugin (enter 1) or Kibana plugin (enter 2)? '))
app = 'OpenSearch'
if app_num is 2:
    app = 'OpenSearch-Dashboards'

app_version = raw_input(app + ' compatibility version (x.x.x): ')

for line in fileinput.input(file_path, inplace=True):
    # Add title and ES/Kibana compatibility
    if fileinput.isfirstline():
        line = "## Version " + plugin_version + " " + current_date + "\n\n" \
            "Compatible with " + app + " " + app_version + "\n"

    # Add link to PRs
    if '*' in line:
        start = line.find('#') + 1
        end = line.find(')', start)
        pr_num = line[start:end]
        line = re.sub(searchExp, "([#" + pr_num +
                      "](" + link_prefix + pr_num + "))", line)
    sys.stdout.write(line)

# Rename file to be consistent with OpenSearch standards
new_file_path = "opensearch-" + plugin_name + ".release-notes-" + \
    plugin_version + ".md"
os.rename(file_path, new_file_path)

print('\n\nDone!\n')