From ff6e62ab9a4ca256525bff4126c3481be3ed8a86 Mon Sep 17 00:00:00 2001
From: Leonardo Arena <rnalrd@alpinelinux.org>
Date: Mon, 14 Oct 2024 05:54:55 +0000
Subject: [PATCH] make shell scripts POSIX compatible

---
 iscsiuio/src/unix/build_date.sh           | 32 +++++++++++------------
 utils/iscsi-gen-initiatorname.sh.template | 24 +++++++++--------
 utils/iscsi_discovery.sh                  |  4 +--
 utils/iscsi_fw_login.sh.template          |  2 +-
 utils/iscsi_offload.sh                    |  4 +--
 5 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/iscsiuio/src/unix/build_date.sh b/iscsiuio/src/unix/build_date.sh
index 65888fe..f6252e7 100755
--- a/iscsiuio/src/unix/build_date.sh
+++ b/iscsiuio/src/unix/build_date.sh
@@ -1,9 +1,4 @@
-#!/bin/bash
-#
-# build the build_date.c and build_date.h files
-#
-# (bash required for getopts)
-#
+#!/bin/sh
 
 THIS_CMD=${0##*/}
 
@@ -20,17 +15,20 @@ usage()
 generate_source_file()
 {
     outfile="$1"
-    if [ -n "$SOURCE_DATE_EPOCH" ] ; then
-	echo 'char *build_date = "'`LC_ALL=C.UTF-8 date --date=@$SOURCE_DATE_EPOCH -u`'";' >"$outfile"
+    if [ -n "$SOURCE_DATE_EPOCH" ]; then
+        # Convert epoch to human-readable date; assume GNU date or fallback to awk.
+        build_date=$(date -u -d "@$SOURCE_DATE_EPOCH" 2>/dev/null || \
+                     awk -v epoch="$SOURCE_DATE_EPOCH" 'BEGIN { print strftime("%Y-%m-%d %H:%M:%S", epoch) }')
+        echo "char *build_date = \"$build_date\";" > "$outfile"
     else
-	echo 'char *build_date = "'`date`'";' >"$outfile"
+        echo "char *build_date = \"$(date)\";" > "$outfile"
     fi
 }
 
 generate_include_file()
 {
     outfile="$1"
-    echo 'extern char *build_date;' >"$outfile"
+    echo 'extern char *build_date;' > "$outfile"
 }
 
 do_source=
@@ -38,17 +36,17 @@ do_include=
 
 while getopts :c:i:S:h opt; do
     case "$opt" in
-    c) do_source="$OPTARG" ;;
-    i) do_include="$OPTARG" ;;
-    S) SOURCE_DATE_EPOCH="$OPTARG" ;;
-    h) usage; exit 0 ;;
-    ?) echo "unknown option" 1>&2; usage; exit 1 ;; 
+        c) do_source="$OPTARG" ;;
+        i) do_include="$OPTARG" ;;
+        S) SOURCE_DATE_EPOCH="$OPTARG" ;;
+        h) usage; exit 0 ;;
+        ?) echo "unknown option" 1>&2; usage; exit 1 ;;
     esac
 done
 
 if [ -n "$do_source" ]; then
-   generate_source_file $do_source
+   generate_source_file "$do_source"
 fi
 if [ -n "$do_include" ]; then
-    generate_include_file $do_include
+    generate_include_file "$do_include"
 fi
diff --git a/utils/iscsi-gen-initiatorname.sh.template b/utils/iscsi-gen-initiatorname.sh.template
index 5c2bfdf..474c0af 100644
--- a/utils/iscsi-gen-initiatorname.sh.template
+++ b/utils/iscsi-gen-initiatorname.sh.template
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 #
 # iscsi-gen-initiatorname
 #
@@ -16,7 +16,7 @@ INAME_DIR="@HOMEDIR@"
 INAME_FILE="$INAME_DIR/initiatorname.iscsi"
 
 # our default IQN prefix
-DEFAULT_IQN_PREFIX="iqn.1996-04.de.suse:01"
+DEFAULT_IQN_PREFIX="iqn.1996-04.org.alpinelinux:01"
 
 #
 # set up comments for initiatorname files using variables
@@ -86,15 +86,17 @@ usage_and_exit()
 #
 # usage: INAME=$(kernel_supplied_initiatorname)
 #
-kernel_supplied_initiatorname()
-{
-    kcl="$(</proc/cmdline)"
-    if [[ "$kcl" =~ rd.initiatorname ]] ; then
-	kcl=${kcl##*rd.initiatorname=}
-	echo ${kcl%% *}
-    else
-	echo ""
-    fi
+kernel_supplied_initiatorname() {
+    kcl="$(cat /proc/cmdline)"
+    case "$kcl" in
+        *rd.initiatorname=*)
+            kcl=${kcl#*rd.initiatorname=}
+            echo "${kcl%% *}"
+            ;;
+        *)
+            echo ""
+            ;;
+    esac
 }
 
 #
diff --git a/utils/iscsi_discovery.sh b/utils/iscsi_discovery.sh
index be2f390..5547ad3 100755
--- a/utils/iscsi_discovery.sh
+++ b/utils/iscsi_discovery.sh
@@ -72,7 +72,7 @@ parse_cmdline()
 	fi
 
 	# check if the IP address is valid
-	ip=`echo $1 | awk -F'.' '$1 != "" && $1 <=255 && $2 != "" && $2 <= 255 && $3 != "" && $3 <= 255 && $4 != "" && $4 <= 255 {print $0}'`
+	ip=$(echo $1 | awk -F'.' '$1 != "" && $1 <=255 && $2 != "" && $2 <= 255 && $3 != "" && $3 <= 255 && $4 != "" && $4 <= 255 {print $0}')
 	if [ -z "$ip" ]; then
 		echo "$1 is not a vaild IP address!"
 		exit 1
@@ -131,7 +131,7 @@ try_login()
 	ret=$?
 	if [ ${ret} = 0 ]; then
 		echo "Set target ${target} to automatic login over ${transport} to portal ${portal}"
-		((connected++))
+		connected=$((connected + 1))
 		if [ "$log_out" = "1" ]; then
 			iscsiadm -m node --targetname ${target} --portal ${portal} --logout
 		fi
diff --git a/utils/iscsi_fw_login.sh.template b/utils/iscsi_fw_login.sh.template
index aae9e4c..6758da2 100644
--- a/utils/iscsi_fw_login.sh.template
+++ b/utils/iscsi_fw_login.sh.template
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 #
 # iscsi_fw_login -- login to iscsi firmware targets, if any
 #
diff --git a/utils/iscsi_offload.sh b/utils/iscsi_offload.sh
index 1869fe1..6a12338 100755
--- a/utils/iscsi_offload.sh
+++ b/utils/iscsi_offload.sh
@@ -222,7 +222,7 @@ elif [ "$mod" = "be2iscsi" ] ; then
     mac=$(iscsi_macaddress_from_pcifn $pcipath $IFNAME 1)
 elif [ "$mod" = "qla4xxx" ] ; then
     mac=$(iscsi_macaddress_from_pcifn $pcipath $IFNAME 1)
-elif [ "$mod" = "qede" -o "$mod" = "qedi" ] ; then
+elif [ "$mod" = "qede" ] || [ "$mod" = "qedi" ] ; then
     mac=$(iscsi_macaddress_from_pcifn $pcipath $IFNAME 4)
 fi
 
@@ -239,7 +239,7 @@ if iscsiadm -m iface -I $ioe_iface > /dev/null 2>&1 ; then
     ioe_mac=$(iscsiadm -m iface -I $ioe_iface 2> /dev/null| sed -n "s/iface\.hwaddress = \(.*\)/\1/p")
     ioe_mod=$(iscsiadm -m iface -I $ioe_iface 2> /dev/null| sed -n "s/iface\.transport_name = \(.*\)/\1/p")
     ipaddr=$(iscsiadm -m iface -I $ioe_iface 2> /dev/null| sed -n "s/iface\.ipaddress = \(.*\)/\1/p")
-    if [ "$ipaddr" == "<empty>" ] ; then
+    if [ "$ipaddr" = "<empty>" ] ; then
 	ipaddr=
     fi
 elif [ "$mod" = "be2iscsi" ] ; then
-- 
2.46.2

