#!/usr/bin/bash

# Replace %patchN syntax with modern syntax in a specfile
# More efficent version of new_patch_syntax.py that uses sed(1) instead of specfile

set -euo pipefail

patch_p="-P"
while getopts "n" OPT; do
    case "${OPT}" in
        n)
            patch_p=""
            ;;
        *)
            echo "Invalid arg: ${OPT}"
            exit 1
            ;;
    esac
done
shift "$((OPTIND-1))"

sed -Ei "s/(^|[^%])%patch([[:digit:]]+)/\1%patch ${patch_p}\2/" -- "$@"
