smeserver-mock/root/usr/bin/BogusDateBot.sh

77 lines
3.0 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# Copyright 2014 Edward J Huff ejhuff at gmail.com, not a work for hire.
# License: any GPL or BSD or MIT or any free software license.
# bash script to fix bogus weekdays in rpm spec files.
# This script automatically corrects bogus weekdays in spec files, generating a
# changelog entry and adding a line after the bogus date noting the original date.
# (The weekday might have been correct and the day of the month wrong).
# The input file is copied to a backup file, and a diff is printed.
# JP PIALASSE : added ? to handle date with one digit
[[ $# > 0 ]] || { echo "Usage: $0 packagename.spec ..."; exit 1; }
export LC_ALL=C
Www='[A-Z][a-z][a-z]'
Mmm='[A-Z][a-z][a-z]'
DD1='[0-9]'
DD0='[0-9]{1,2}'
DD='[0-9][0-9]'
YYYY='[12][90][0-9][0-9]'
WwwMmmDDYYYY="\($Www\) \($Mmm\) \($DD\) \($YYYY\)"
WwwMmmDD1YYYY="\($Www\) \($Mmm\) \($DD1\) \($YYYY\)"
WwwMmmDD0YYYY="\($Www\) \($Mmm\) \($DD0\) \($YYYY\)"
while [[ $# > 0 ]]; do
[[ -f "$1" ]] || { echo "$0: $1: no such file."; exit 1; }
changelog=$(mktemp --tmpdir=. changelog-XXXXXXXXX.txt)
sedscript=$(mktemp --tmpdir=. sedscript-XXXXXXXXX.sed)
printf "%s\n" \
"* $(date +'%a %b %d %Y') BogusDateBot" \
'- Eliminated rpmbuild "bogus date" warnings due to inconsistent weekday,' \
" by assuming the date is correct and changing the weekday." \
> "$changelog"
cat "$1" | \
grep -P "^\* $Www $Mmm $DD0 $YYYY" | \
grep -v '^echo' | \
sed 's/^\* '"$WwwMmmDDYYYY"'.*$/echo "$(date --date="\2 \3 \4" +"%a \1 %b %d %Y")"/' | \
sed 's/^\* '"$WwwMmmDD1YYYY"'.*$/echo "$(date --date="\2 0\3 \4" +"%a \1 %b %d %Y")"/' | \
grep '^echo' | \
bash | \
egrep -v "Mon Mon|Tue Tue|Wed Wed|Thu Thu|Fri Fri|Sat Sat|Sun Sun" | \
sort -u --key=5,5n --key=3,3M --key=4,4n | \
while read correct wrong Month Day Year; do
date="$Month $Day $Year"
alternatives="$wrong $Month $Day $Year --> "$(
for ((i = -6; i < 7; i++ )); do
date --date "$date $i day" +"%a %b %d %Y or ";
done | egrep "$date|$wrong" | tr -d \\n;
printf "%s" "....")
printf " %s\n" "$alternatives" >> "$changelog"
# re='^\* '"$wrong $Month $Day $Year"'\(.*\)$'
# subs='* '"$correct $Month $Day $Year"'\1\n '"$alternatives"
# printf "%s\n" "s:$re:$subs:" >> "$sedscript"
# if $Day only one digit
if [[ "$Day" =~ ^0([0-9])$ ]];
then echo "${BASH_REMATCH[1]}";
re='^\* '"$wrong $Month ${BASH_REMATCH[1]} $Year"'\(.*\)$'
subs='* '"$correct $Month $Day $Year"'\1\n '"$alternatives"
else
re='^\* '"$wrong $Month $Day $Year"'\(.*\)$'
subs='* '"$correct $Month $Day $Year"'\1\n '"$alternatives"
fi
printf "%s\n" "s:$re:$subs:" >> "$sedscript"
done
printf "\n" >> "$changelog"
backup="$1"-$(date --utc --ref="$1" +"%Y-%m-%d-%H-%M-%S-%NZ")
cp -vpf "$1" "$backup"
cat "$backup" | sed -f "$sedscript" -e '/^%changelog *$/ r '"$changelog" > "$1"
rm -f "$changelog" "$sedscript"
diff -u "$backup" "$1"
shift
done