# Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# 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.

echo "Check Docforge Manifest"

docforgeVersion="v0.21.0"
docCommitHash="12922abea4c5afa207cc9c09b18414cd2348890c"

repoPath="$(readlink -f $(dirname ${0})/..)"
manifestPath="${repoPath}/.docforge/manifest.yaml"
diffDirs=".docforge/;docs/"

if [[ ! -d ${repoPath}/hack/tools/bin ]]; then
  mkdir -p ${repoPath}/hack/tools/bin
fi

docforgeBinPath="${repoPath}/hack/tools/bin/docforge"
export PATH=${repoPath}/hack/tools/bin:$PATH
if [[ ! -f "${docforgeBinPath}" || $(${docforgeBinPath} version) != "${docforgeVersion}" ]]; then
  # Installing docforge
  curl -L -o "${docforgeBinPath}" https://github.com/gardener/docforge/releases/download/${docforgeVersion}/docforge-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m | sed 's/x86_64/amd64/')
  chmod +x ${docforgeBinPath}
fi

tmpDir=$(mktemp -d)
function cleanup {
    rm -rf "$tmpDir"
}
trap cleanup EXIT ERR INT TERM

curl https://raw.githubusercontent.com/gardener/documentation/${docCommitHash}/.ci/check-manifest --output ${tmpDir}/check-manifest-script.sh && chmod +x ${tmpDir}/check-manifest-script.sh
curl https://raw.githubusercontent.com/gardener/documentation/${docCommitHash}/.ci/check-manifest-config --output ${tmpDir}/manifest-config
scriptPath="${tmpDir}/check-manifest-script.sh"
configPath="${tmpDir}/manifest-config"

${scriptPath} --repo-path ${repoPath} --repo-name "gardenctl" --use-token false --manifest-path ${manifestPath} --diff-dirs ${diffDirs} --config-path ${configPath}