#!/bin/bash
# Handy script to duplicate the status of a package(s) from one release to
# another where it is listed as DNE originally

# Primarily to use in the case where a package gets added to a release
# where it previously was DNE - and then we get a heap of errors from
# check-syntax like:

# package 'foo' DOES exist in 'bar'!

src="$1"
shift
if [ -z "$src" ]; then
  echo "Which release to copy status from?" >&2
  exit 1
fi
dst="$1"
shift
if [ -z "$dst" ]; then
  echo "Which release to copy status to?" >&2
  exit 1
fi

for pkg in "$@"; do
  echo "Duplicating status from ${src} to ${dst} for ${pkg} where was DNE for ${dst}"
  # find files to change
  for file in $(grep -l '^'"${dst}_${pkg}"': DNE' active/00boilerplate* active/CVE-* retired/CVE-*); do
    status=$(grep '^'"${src}_${pkg}"': ' "${file}" | cut -f2- -d' ')
    if [ ! -z "${status}" ]; then
      echo "Duplicating status ${status} from ${src} to ${dst} in ${file}"
      sed -i -e 's/^\('"${dst}_${pkg}"'\): \(DNE\)/\1: '"${status}"'/' "${file}"
    else
      echo "Failed to extract status from ${src} in ${file}"
    fi
  done
done
