#!/bin/sh
#
# Generates a patch for replacing <webmasters@gnu.org> with the
# correct broken link reporting address.
#
# Copyright 2019 (C) Félicien PILLOT <felicien@gnu.org>
# 
# This is free software: you can redistribute it and/or modify under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your
# option) any later version.
# 
# This file is distributed in the hope that it will be useful, WITHOUT
# ANY WARRANTY; without even the implied warranty of or FITNESS FOR A
# PARTICULAR PURPOSE.  See the General Public License for more
# details.
# 
# You should have received a copy of the GNU General Public License
# with this file.  If not, see <http://www.gnu.org/licenses/>.

# If no argument is passed, display help message
if [ $# -eq 0 ]
then
    echo \
"This script generates a patch for replacing <webmasters@gnu.org> with
the correct broken link reporting address.
No need to apply the patch in working dir.
You must provide some arguments:
1. The package name
2. The correct address (optional: if not given, will assume bug-<package>@gnu.org)"
fi

# Get the package name -- typically it's $(basename $(pwd))
PACKAGE_NAME=$1

# If no more argument is passed, build the default ML address
if [ $# -eq 1 ]
then
    NEW_ADDRESS="bug-${PACKAGE_NAME}@gnu.org"
else
    NEW_ADDRESS=$2
fi

# Search for files and lines to edit, replace addresses with sed
for FILE in $(grep -Rls mailto:webmasters *)
do
    sed -i "/mailto:/ s/webmasters@gnu.org/${NEW_ADDRESS}/g" $FILE
done

# Get a diff from the last commit
cvs diff -U1 -r1 * > ${PACKAGE_NAME}.patch 2> /dev/null

# Warn the user if nothing has happen
[ -s $PACKAGE_NAME.patch ] || echo "WARNING: ${PACKAGE_NAME}.patch is empty."
