git: 1eba137b81f9 - main - Tools/scripts: Add add-port-to-category-makefile.sh, command that adds new port to category's Makefile

From: Yuri Victorovich <yuri_at_FreeBSD.org>
Date: Mon, 16 Jan 2023 21:07:30 UTC
The branch main has been updated by yuri:

URL: https://cgit.FreeBSD.org/ports/commit/?id=1eba137b81f9c3e322da49764244d17c5c7c59b2

commit 1eba137b81f9c3e322da49764244d17c5c7c59b2
Author:     Yuri Victorovich <yuri@FreeBSD.org>
AuthorDate: 2023-01-16 20:56:41 +0000
Commit:     Yuri Victorovich <yuri@FreeBSD.org>
CommitDate: 2023-01-16 21:07:27 +0000

    Tools/scripts: Add add-port-to-category-makefile.sh, command that adds new port to category's Makefile
    
    PR:             268737
---
 Tools/scripts/add-port-to-category-makefile.sh | 48 ++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/Tools/scripts/add-port-to-category-makefile.sh b/Tools/scripts/add-port-to-category-makefile.sh
new file mode 100755
index 000000000000..691b21fff996
--- /dev/null
+++ b/Tools/scripts/add-port-to-category-makefile.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+#
+# MAINTAINER: yuri@FreeBSD.org
+
+PORT="$1"
+
+set -e
+set -o pipefail
+
+##
+## add-port-to-category-makefile.sh: adds a new port to {category}/Makefile
+##
+
+
+# sanity checks
+[ -z "$PORT" ] && echo "this command requires the <port> argument" && exit 1
+(echo "$PORT" | grep -q "/") && echo "port's name can't contain slash" && exit 1
+! [ -f Makefile ] && echo "no Makefile found, are you in the ports tree?" && exit 1
+! grep -q "^    SUBDIR += " Makefile && echo "this command can only be run from the ports tree category directory" && exit 1
+! grep -q "^\\.include <bsd\\.port\\.subdir\\.mk>$" Makefile && echo "this command can only be run from the ports tree category directory" && exit 1
+! [ -d "$PORT" ] && echo "the '$PORT' directory is missing" && exit 1
+! [ -f "$PORT/Makefile" ] && echo "'$PORT/Makefile' is missing" && exit 1
+grep -q "^    SUBDIR += $PORT$" Makefile && echo "port '$PORT' is already added" && exit 1
+
+
+# add port to Makefile
+/usr/bin/awk '
+BEGIN {
+	done = 0
+	seen = 0
+	str = "    SUBDIR += '$PORT'"
+}
+/^    SUBDIR \+= / {
+	if (!done && str < $0) {
+		print str
+		done = 1
+	}
+	print $0;
+	seen = seen + 1
+}
+!/^    SUBDIR \+= / {
+	if (seen > 0 && !done) {
+		print str
+		done = 1
+	}
+	print $0
+}' < Makefile > Makefile.new &&
+/bin/mv Makefile.new Makefile