#!/bin/bash

me="$(readlink -f $0)"
cd $(dirname "${me}")

perlMinor=$(
  pacman -Q perl | \
    sed '
      s/^\S\+ //
    '
)
perlMajor="${perlMinor%%.*}"
perlMinor="${perlMinor#${perlMajor}.}"
perlMinor="${perlMinor%%.*}"

for pkgbuild in perl-*/PKGBUILD demeter/PKGBUILD; do

  ./bumpPkgrel -n "${pkgbuild%/PKGBUILD}"
  sed -i '
    /^depends=(/{
      :a
      /)/! {
        N
        ba
      }
      s/\n\(\s*\('"'"'\?\)perl[<>]\S\+\2\n\)\+/\n/
      s/^depends=([^\n]*\n/\0  '"'"'perl>='"${perlMajor}.${perlMinor}'"'\n  '"'"'perl<'"${perlMajor}.$((perlMinor+1))'"'\n/
    }
  ' "${pkgbuild}"
  if ! grep -q '^depends=(' "${pkgbuild}"; then
    sed -i '
      /^arch=(/ a depends=(\
  '"'"'perl>='"${perlMajor}.${perlMinor}'"'\
  '"'"'perl<'"${perlMajor}.$((perlMinor+1))'"'\
)
    ' "${pkgbuild}"
  fi
  git add "${pkgbuild}"

done

git commit -m 'recompile all perl packages'