1#!/bin/sh
2
3#
4# bump-revision.sh category/portname category/portname ...
5# Bump PORTREVISION if it exists or create it with number 1 if it does not
6#
7# ----------------------------------------------------------------------------
8# "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp):
9# Bartek Rutkowski <robak@FreeBSD.org> wrote this file.  As long as you retain
10# this notice you can do whatever you want with this stuff. If we meet some
11# day, and you think this stuff is worth it, you can buy me a beer in return.
12#
13# Bartek Rutkowski
14# ----------------------------------------------------------------------------
15#
16
17#
18# functions
19#
20
21printc () {
22# $1 - msg is obligatory, $2 - color (red/green)of the message, default if not passed
23    if [ -t 1 ]; then
24        if [ $# -eq 2 ]; then
25            if [ $2 = "red" ]; then
26                echo -e "\033[1;31m$1\033[m"
27            elif [ $2 = "green" ]; then
28                echo -e "\033[1;32m$1\033[m"
29            else
30                echo "$1"
31            fi
32        fi
33    else
34        echo $1
35    fi
36}
37
38#
39# main loop
40#
41
42tempfile=$(mktemp)
43trap "rm -f $tempfile" 0 1 2 3 15
44
45while [ $# -gt 0 ]
46do
47    if [ -f "$1/Makefile" ]; then
48        # See what the port thinks its PORTREVISION is and save that.
49        pre=$(make -C "$1" -V PORTREVISION)
50
51        # If the Makefile exists, continue and empty the tempfile, set up variables
52	echo -n > $tempfile
53        revision_str=`grep "^PORTREVISION?\?=" "$1/Makefile"`
54
55        case $? in
56        0)
57            # If the exit code is 0, then PORTREVISION line was found
58            if [ `echo "$revision_str" | wc -l` = 1 ]; then
59                # If the $revision_str variable has only 1 line, then proceed with processing it
60                case `echo "$revision_str" | awk -F "\t+" '{ print $2 }'` in
61                    (*[^0-9]*|'')
62                        # If the value of PORTREVISION is not an integer, we can't bump its value
63                        printc "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 1
67                        printc "INFO: $1 $revision_str found, bumping it by 1." "green"
68                        rm -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                        ;;
72                esac
73            else
74                # If the $revision_str variable had more than 1 line, we can't bump its value safely
75                printc "ERROR: $1 PORTREVISION found more than once, unable to bump it reliably!" "red"
76            fi
77            ;;
78        1)
79            # If the exit code is 1 then PORTREVISION wasn't found, so we need to add one with value of 1
80            printc "INFO: $1 PORTREVISION not found, adding PORTREVISION= 1" "green"
81            rm -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, try
84            # to add it before a CATEGORIES, PKGNAMESUFFIX or PKGNAMEPREFIX line:
85            for line in CATEGORIES PKGNAMEPREFIX PKGNAMESUFFIX; do
86                    if ! grep -q "^PORTREVISION?\?=" $1/Makefile; then
87                            rm -f $tempfile && awk '/^'${line}'\??=\t/{ print "PORTREVISION=\t1"; print; next } { print }' "$1/Makefile" > $tempfile \
88                                    && cat $tempfile > "$1/Makefile"
89                    fi
90            done
91            # If it still is not there, bail out
92            if ! grep -q "^PORTREVISION?\?=" $1/Makefile; then
93                    printc "ERROR: $1 PORTREVISION not found and failed to add it!" "red"
94            fi
95
96            # See what the port now has for PORTREVISION.
97            post=$(make -C "$1" -V PORTREVISION)
98
99            if [ "$post" -le "$pre" ]; then
100                printc "ERROR: $1 PORTREVISION went backwards from $pre to $post!" "red"
101            fi
102            ;;
103        *)
104            printc "ERROR: PORTREVISION grep for $1 exited with error!" "red"
105            ;;
106        esac
107    else
108        # The directory specified had no Makefile, so it seems like a mistake
109        printc "ERROR: $1 might not be a port directory because $1/Makefile is missing!" "red"
110    fi
111    shift
112done
113
114