#!/bin/bash

set -euo pipefail

if [[ $# -eq 3 ]]; then
	echo "Fix config doesn't exist, nothing to merge"
    exit
fi

DEVP_MERGED_LINT_CONFIG=$1
DEVP_YQ_EXE=$2
DEFAULT_CONFIG=$3
FIX_CONFIG=$4

TEMP_CONFIG="${DEVP_MERGED_LINT_CONFIG}.tmp"
${DEVP_YQ_EXE} eval-all 'select(fileIndex == 0) *+ select(fileIndex == 1)' "${DEFAULT_CONFIG}" "${FIX_CONFIG}" > "${TEMP_CONFIG}"

(echo "# Generated and used by devp/lint-go-default. Can be safely added to .gitignore" ; cat "${TEMP_CONFIG}") > "${DEVP_MERGED_LINT_CONFIG}"
rm "${TEMP_CONFIG}"

echo "File '${FIX_CONFIG}' merged into final config '${DEVP_MERGED_LINT_CONFIG}'"
