#!/bin/bash
# Usage:
# ./otp-get-patches.sh /path/to/otp OTP_R14B02 fedora-R14B02
#
# otp-get-patches.sh - update erlang.spec and otp-00*.patch files
#
# otp-get-patches.sh updates the erlang.spec and otp-00*.patch
# files in the git index. After an otp-get-patches.sh run, you
# will need to review the staged git changes, possibly adapt the
# 'Release:' and '%changelog' parts of erlang.spec, and can then
# "git commit" everything.
#
# Caution: Leave the four special comment lines untouched in the
# spec file, as otp-get-patches.sh requires them and will only
# touch the parts of erlang.spec between the respective start/end
# comment pair:
#
# # start of autogenerated patch tag list
# # end of autogenerated patch tag list
# Command line parsing
otp_dir="${1:?'Fatal: otp git repo dir required'}"
otp_upstream="${2:?'Fatal: git ref to upstream release required'}"
otp_fedora="${3:?'Fatal: git ref to branch with fedora patches required'}"
# Setup
set -e
# set -x
tmpdir="$(mktemp -d --tmpdir="$PWD")"
# Generate patch files
pushd "$otp_dir"
git format-patch -N --no-signature --no-stat -o "$tmpdir" "${otp_upstream}..${otp_fedora}" > "$tmpdir/patch-list.txt"
popd
test -s "$tmpdir/patch-list.txt"
# Process patch files
echo "# start of autogenerated patch tag list" > "$tmpdir/patch-list-tags.txt"
n=1
while read patch
do
otppatch="$(dirname "$patch")/otp-$(basename "$patch")"
${SED-sed} -e '1d' -e '/^-- $/,$d' "$patch" > "$otppatch"
rm -f "$patch"
echo "Patch$n: $(basename "$otppatch")" >> "$tmpdir/patch-list-tags.txt"
n=$(($n + 1))
done < "$tmpdir/patch-list.txt"
echo "# end of autogenerated patch tag list" >> "$tmpdir/patch-list-tags.txt"
# Create updated spec file
specfile="erlang.spec"
newspec1="${tmpdir}/${specfile}.new1"
sed '/^# start of autogenerated patch tag list$/,$d' "$specfile" > "$newspec1"
cat "$tmpdir/patch-list-tags.txt" >> "$newspec1"
sed '1,/^# end of autogenerated patch tag list/d' "$specfile" >> "$newspec1"
# Actually put all changes into git index
git rm -f otp-00*.patch
mv "$tmpdir/otp-00"*.patch .
git add otp-00*.patch
mv -f "$newspec1" "$specfile"
git add "$specfile"
rm -rf "$tmpdir"
# End of file.