From a614865d3592ef7aed3c0c2d570aea8c6e508ea3 Mon Sep 17 00:00:00 2001
From: GoldenTails <milestailsprower101n2@gmail.com>
Date: Sun, 27 Mar 2022 21:16:07 -0500
Subject: [PATCH] Make comptime.sh conform to POSIX and less redundant, among
 other improvements

- Shebang now directly calls /bin/sh
- Improved indenting within the comprevision() function
- Quoted several strings to prevent possible argument splitting
- Replaced archaic backtick command statements with more descriptive `"$(...)"` statements
- Replaced direct references to `test` with more readable, but equivalent `[ ... ]` statements
- Replaced the ${str:0:8} bashism with an equivalent statement using POSIX cut
- Moved redundant definitions of the comptime.h body to a single function that takes arguments
---
 comptime.sh | 54 ++++++++++++++++++++++-------------------------------
 1 file changed, 22 insertions(+), 32 deletions(-)

diff --git a/comptime.sh b/comptime.sh
index d5ef7271a..a619f6a1a 100755
--- a/comptime.sh
+++ b/comptime.sh
@@ -1,54 +1,44 @@
-#!/bin/bash -e
+#!/bin/sh
 path="."
 if [ x"$1" != x ]; then
 	path="$1"
 fi
 
-versiongit() {
-	gitbranch=`git rev-parse --abbrev-ref HEAD`
-	gitversion=`git rev-parse HEAD`
-	cat <<EOF > $path/comptime.h
+version() {
+	cat <<EOF > "$path/comptime.h"
 
 // Do not edit!  This file was autogenerated
 // by the $0 script with git
 //
-const char* compbranch = "$gitbranch";
-const char* comprevision = "${gitversion:0:8}";
+const char* compbranch = "$1";
+const char* comprevision = "$2";
 EOF
-exit 0
 }
 
-versionsvn() {
-	svnrevision=`svnversion -n $1`
-	cat <<EOF > $path/comptime.h
+versiongit() {
+	gitbranch="$(git rev-parse --abbrev-ref HEAD)"
+	gitversion="$(git rev-parse HEAD | cut -c -8)"
+	version "$gitbranch" "$gitversion";
+	exit 0
+}
 
-// Do not edit!  This file was autogenerated
-// by the $0 script with subversion
-//
-const char* compbranch = "Subversion";
-const char* comprevision = "r$svnrevision";
-EOF
-exit 0
+versionsvn() {
+	svnrevision="$(svnversion -n $1)"
+	version "Subversion" "r$svnrevision";
+	exit 0
 }
 
 versionfake() {
-	cat <<EOF > $path/comptime.h
-
-// Do not edit!  This file was autogenerated
-// by the $0 script with an unknown or nonexist SCM
-//
-const char* compbranch = "Unknown";
-const char* comprevision = "illegal";
-EOF
+	version "Unknown" "illegal";
 }
 
 compversion() {
-touch $path/comptime.c
-versionfake
-test -d $path/.svn && versionsvn
-test -d $path/../.git && versiongit
-exit 1
+	touch "$path/comptime.c"
+	versionfake
+	[ -d "$path/.svn" ] && versionsvn
+	[ -d "$path/../.git" ] && versiongit
+	exit 1
 }
 
-test -f $path/comptime.c && compversion
+[ -f "$path/comptime.c" ] && compversion
 exit 2
-- 
GitLab