#!/usr/bin/env bash
# Copyright 2022 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Mark all existing Markdown files with a deprecation notice and redirect link.
# This is also known as a "tombstone".

set -euo pipefail

SCRIPT_ROOT="$(cd "$(dirname "$0")" && pwd)"

declare -a FILES_TO_MARK=()

# Find all files to mark. These are all *.md files under the "prow/"
# directory in kubernetes/test-infra.
function identify_files_to_mark() {
    find "${SCRIPT_ROOT}"/../prow -type f -name \*.md | sort
}

function mark_files() {
    local notice_tmp
    local file_contents
    local file_path_prefix
    local file_path_rest
    file_path_prefix="${SCRIPT_ROOT}/../prow"

    for file in "${FILES_TO_MARK[@]}"; do
        file_contents="$(cat "${file}")"
        file_path_rest="${file#${file_path_prefix}/}"

        # Add deprecation notice at the beginning of each file.
        cat <<EOF > "${file}"
DOCUMENTATION DEPRECATION NOTICE: This file is deprecated. Please refer to the
[new migrated
location](https://github.com/kubernetes-sigs/prow/tree/main/site/content/en/docs/Legacy%20Snapshot/prow/${file_path_rest}).
Please do not edit this file; instead, make changes to the new location!

The new location is served on the web at
https://docs.prow.k8s.io/docs/legacy-snapshot/.

This file will be deleted on 2022-11-30.

EOF
    done

    echo "Finished adding tombstone to files."
}

function main() {
    mapfile -t FILES_TO_MARK < <(identify_files_to_mark)
    mark_files
}

main "$@"
