#!/bin/bash version=$(sed -rn '/^Version:[[:space:]]+/{s/^Version:[[:space:]]+([0-9]+\.[0-9]+)(\.[0-9+])?/\1/p}' ncurses.spec) || exit 1 test -e ncurses-${version}-patches.tar.bz2 || exit 1 OFS="$IFS" IFS=- last=($(tar -tf ncurses-${version}-patches.tar.bz2 | sed -r 's@\.patch$@@'| sort -t '-' -k 3,3 -n | tail -n 1)) IFS="$OFS" patch=${last[2]} sed -ri "/^%define[[:space:]]+patchlvl/{ s/(^%define[[:space:]]+patchlvl)[[:space:]]+.*/\1 ${patch}/ }" ncurses.spec