#!/bin/sh12#3# bump-revision.sh category/portname category/portname ...4# Bump PORTREVISION if it exists or create it with number 1 if it does not5#6# ----------------------------------------------------------------------------7# "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp):8# Bartek Rutkowski <[email protected]> wrote this file. As long as you retain9# this notice you can do whatever you want with this stuff. If we meet some10# day, and you think this stuff is worth it, you can buy me a beer in return.11#12# Bartek Rutkowski13# ----------------------------------------------------------------------------14#15# MAINTAINER= [email protected]1617#18# functions19#2021printc () {22# $1 - msg is obligatory, $2 - color (red/green)of the message, default if not passed23if [ -t 1 ]; then24if [ $# -eq 2 ]; then25if [ $2 = "red" ]; then26echo -e "\033[1;31m$1\033[m"27elif [ $2 = "green" ]; then28echo -e "\033[1;32m$1\033[m"29else30echo "$1"31fi32fi33else34echo $135fi36}3738#39# main loop40#4142tempfile=$(mktemp)43trap "rm -f $tempfile" 0 1 2 3 154445while [ $# -gt 0 ]46do47if [ -f "$1/Makefile" ]; then48# See what the port thinks its PORTREVISION is and save that.49pre=$(make -C "$1" -V PORTREVISION)5051# If the Makefile exists, continue and empty the tempfile, set up variables52echo -n > $tempfile53revision_str=`grep "^PORTREVISION?\?=" "$1/Makefile"`5455case $? in560)57# If the exit code is 0, then PORTREVISION line was found58if [ `echo "$revision_str" | wc -l` = 1 ]; then59# If the $revision_str variable has only 1 line, then proceed with processing it60case `echo "$revision_str" | awk -F "\t+" '{ print $2 }'` in61(*[^0-9]*|'')62# If the value of PORTREVISION is not an integer, we can't bump its value63printc "ERROR: $1 PORTREVISION value is not a number, unable to solve!" "red"64;;65(*)66# If the value of PORTREVISION is an integer, increase it by 167printc "INFO: $1 $revision_str found, bumping it by 1." "green"68rm -f $tempfile && awk -F "\t+" '/^PORTREVISION\??=/{ gsub ($2, $2+1) }; { print }' "$1/Makefile" > $tempfile \69&& cat $tempfile > "$1/Makefile" \70|| printc "ERROR: $1 PORTREVISION found but failed to bump it!" "red"71;;72esac73else74# If the $revision_str variable had more than 1 line, we can't bump its value safely75printc "ERROR: $1 PORTREVISION found more than once, unable to bump it reliably!" "red"76fi77;;781)79# If the exit code is 1 then PORTREVISION wasn't found, so we need to add one with value of 180printc "INFO: $1 PORTREVISION not found, adding PORTREVISION= 1" "green"81rm -f $tempfile && awk '/^(PORT|DIST)VERSION\??=\t/{ print; print "PORTREVISION=\t1"; next } { print }' "$1/Makefile" > $tempfile \82&& cat $tempfile > "$1/Makefile"83# If there is not PORTREVISION line, maybe it is a slave port, try84# to add it before a CATEGORIES, PKGNAMESUFFIX or PKGNAMEPREFIX line:85for line in CATEGORIES PKGNAMEPREFIX PKGNAMESUFFIX; do86if ! grep -q "^PORTREVISION?\?=" $1/Makefile; then87rm -f $tempfile && awk '/^'${line}'\??=\t/{ print "PORTREVISION=\t1"; print; next } { print }' "$1/Makefile" > $tempfile \88&& cat $tempfile > "$1/Makefile"89fi90done91# If it still is not there, bail out92if ! grep -q "^PORTREVISION?\?=" $1/Makefile; then93printc "ERROR: $1 PORTREVISION not found and failed to add it!" "red"94fi9596# See what the port now has for PORTREVISION.97post=$(make -C "$1" -V PORTREVISION)9899if [ "$post" -le "$pre" ]; then100printc "ERROR: $1 PORTREVISION went backwards from $pre to $post!" "red"101fi102;;103*)104printc "ERROR: PORTREVISION grep for $1 exited with error!" "red"105;;106esac107else108# The directory specified had no Makefile, so it seems like a mistake109printc "ERROR: $1 might not be a port directory because $1/Makefile is missing!" "red"110fi111shift112done113114115116