From 4fceae8176786a8a21e70a6398ece71f0abcadcd Mon Sep 17 00:00:00 2001
From: Daniel van der Schuur <schuur@astron.nl>
Date: Tue, 3 May 2016 09:19:36 +0000
Subject: [PATCH] -SVN copied generic.sh from $UNB to $RADIOHDL as it is used
 by almost  all build scripts.

---
 tools/quartus/generic.sh        | 256 ++++++++++++++++++++++++++++++++
 tools/quartus/run_all_qsys      |   2 +-
 tools/quartus/run_all_sopc      |   2 +-
 tools/quartus/run_app           |   2 +-
 tools/quartus/run_app_clean     |   2 +-
 tools/quartus/run_bsp           |   2 +-
 tools/quartus/run_mif           |   2 +-
 tools/quartus/run_qcomp         |   2 +-
 tools/quartus/run_qsys          |   2 +-
 tools/quartus/run_rbf           |   2 +-
 tools/quartus/run_reg           |   2 +-
 tools/quartus/run_regtest_synth |   2 +-
 tools/quartus/run_sof           |   2 +-
 tools/quartus/run_sopc          |   2 +-
 tools/quartus/run_term          |   2 +-
 15 files changed, 270 insertions(+), 14 deletions(-)
 create mode 100755 tools/quartus/generic.sh

diff --git a/tools/quartus/generic.sh b/tools/quartus/generic.sh
new file mode 100755
index 0000000000..25ea0f76d3
--- /dev/null
+++ b/tools/quartus/generic.sh
@@ -0,0 +1,256 @@
+# This file contains a lot of
+# convenience functions and
+# definitions.
+#
+# You will notice that mostly the caller
+# has little or no influence on the function
+# call; this is done on purpose.
+#
+# (at least some of) The scripts which 
+# build on/make decisions based on the values
+# returned from these functions are either
+# run as root or run with root privilege.
+#
+# For the moment I (HV) decided to eliminate
+# as much user-input-error as possible,
+# giving the user as little possibility
+# of passing in 'the wrong thing' before
+# ruining something.
+#
+
+# make sure that use of unset vars triggers an error (-u)
+# and automatically export to subsequent commands (-a)
+# DS: no longer using set -u as it renders some tests 
+#(e.g. test -z $1) unusable.
+#set -ua 
+set -a
+#exits on any error in pipeline, not just the last error
+set -o pipefail
+
+# only set variables if we didn't set them before
+if [ "${generic_read:-not_set}" = "not_set" ]; then
+
+# display a (colourfull ...) error message. 
+#    the script will be terminated immediately
+# exit with <errorcode> (default=1)
+# usage:  unb_error <caller> <message> [<errorcode>]
+unb_error() {
+    caller=${1:-""}
+    msg=${2:-""}
+    exitcode=${3:-1}
+    if [ -z "${caller}" -o -z "${msg}" ]; then
+        echo "usage: unb_error <caller's name> <message> [<exitcode>]"
+        exit 1
+    fi
+    caller=`basename ${caller} | tr [a-z] [A-Z]`
+    echo -en '\E[40;36m'"\033[1m[${caller}]\033[0m "
+    echo -e '\E[40;31m'"\033[1mERROR - ${msg}. \033[0m "
+    tput sgr0
+    exit ${exitcode} 
+}
+
+# Non-exiting version of unb_error in case we wish to accumulate errors and
+# call an exiting unb_error after displaying accumulated errors.
+unb_error_noexit() {
+    caller=${1:-""}
+    msg=${2:-""}
+    if [ -z "${caller}" -o -z "${msg}" ]; then
+        echo "usage: unb_error <caller's name> <message> [<exitcode>]"
+        exit 1
+    fi
+    caller=`basename ${caller} | tr [a-z] [A-Z]`
+    echo -en '\E[40;36m'"\033[1m[${caller}]\033[0m "
+    echo -e '\E[40;31m'"\033[1mERROR - ${msg}. \033[0m "
+    tput sgr0
+}
+
+unb_warning() {
+    caller=${1:-""}
+    msg=${2:-""}
+    exitcode=${3:-1}
+    if [ -z "${caller}" -o -z "${msg}" ]; then
+        echo "usage: unb_warning <caller's name> <message> [<exitcode>]"
+        exit 1
+    fi
+    caller=`basename ${caller} | tr [a-z] [A-Z]`
+    echo -en '\E[40;36m'"\033[1m[${caller}]\033[0m "
+    echo -e '\E[40;35m'"\033[1mWARNING - ${msg}. \033[0m "
+    tput sgr0
+    return 0 
+}
+
+
+# usage:  unb_info <caller> <message>
+unb_info() {
+    caller=${1:-""}
+    shift
+    if [ -z "${caller}" -o -z "$*" ]; then
+        echo "usage: unb_info <scriptname> <msg1> [<msg2> .. <msgN>]"
+        exit 1
+    fi
+    caller=`basename ${caller} | tr [a-z] [A-Z]`
+    echo -e '\E[40;36m'"\033[1m[${caller}] $* \033[0m "
+    tput sgr0
+    return 0
+}
+
+# usage:
+#   unb_exec <calling script> [OPTS] <command to run>
+#  OPTS:
+#     [msg=<override defaultmsg>]
+#           msg=no => suppress displaying of messages
+#                     if command fails, do display the
+#                     command that failed
+#     [expect=<expected exit code>] (default: 0)
+# exits with same exitcode as the command
+unb_exec() {
+    # step one: extract calling scriptname, which is $1
+    caller=$1; shift
+    # anything left is supposedly the command to exec + args
+    # prepare the "msg" to display
+    msg=
+    output=
+    expect=0
+    # unless someone gave msg="...." as orginal 2nd arg
+    #  (and now, since the first "shift", it is 1st)
+    for ac ; do
+        case ${ac} in 
+            output=*)
+                # well allrighty then, override default msg 
+                output=`echo "${ac}" | sed 's/^output=//'`
+                shift
+                ;;
+            msg=*)
+                # well allrighty then, override default msg 
+                msg=`echo "${ac}" | sed 's/^msg=//'`
+                shift
+                ;;
+            expect=*)
+                expect=`echo "${ac}" | sed 's/^expect=//'`
+                shift
+                ;;
+            * )
+                # first non-option argument; stop for loop!
+                break
+                ;;
+        esac
+    done
+    if [ -z "${msg}" ]; then
+        msg="Running \"$*\""
+    fi
+    # show usr what we're up to
+    if [ "${msg}" != "no" ]; then
+        unb_info ${caller} "${msg}"
+    fi
+    # and let's actually do it!
+    if [ "${output}" = "no" ]; then
+      $* >/dev/null 2>/dev/null
+    else
+      $*
+    fi
+
+    exitcode=$?
+    if [ "${exitcode}" -ne "${expect}" ]; then
+        if [ "${msg}" == "no" ]; then
+            echo "****** Failed command ****"
+            echo $*
+            exit ${exitcode}
+        fi
+        unb_error ${caller} "\"${msg}\" failed" $?
+    fi
+}
+
+# format the date in a specific form
+# if changing the format, make sure
+# that dateindent has the same length
+# again (dateindent used for pretty
+# printing multiline stuff without
+# having to print the date in every line)
+date="/bin/date +'%d %m %Y %T'"
+# format    dd mm yyyy HH:MM:ss
+dateindent='                   '
+
+#
+# Some generic, often used functions
+#
+
+# return the current date/time in a
+# predefined format - see above
+# Use eg as
+# echo "`timestamp` Aaargh - Failed to clobber!"
+timestamp() {
+	eval ${date}
+}
+
+
+# strip both leading/trailing whitespace
+# characters
+strip_ws() {
+    rv=
+    if [ -n "$1" ]; then
+        # actually, you can do it in one go [when you
+        # finally read the fine manual of sed ;)]
+        cmd="echo '$1' | ${sed} 's/^ \{0,\}//;s/ \{0,\}$//'"
+        rv=`eval ${cmd}`
+    fi
+    echo ${rv}
+}
+
+# escape special characters
+escape_chars() {
+    rv=
+    cmd="echo '${1}' | ${sed} 's#\([].,*[() \\\\/]\)#\\\\\1#g'"
+    rv=`eval ${cmd}`
+    echo ${rv}
+}
+
+# write all arguments to the logfile
+dbglogfile="/tmp/dbglogfile"
+dbglog() {
+    touch ${dbglogfile}
+    txt=
+    for ac do
+        txt="${txt} ${ac}"
+    done
+    echo ${txt} >> ${dbglogfile}
+}
+
+# if the argument is a single string
+# return 1 otherwise return 0
+# Single string meaning 
+# 'series of characters without
+#  space'
+# Leading/trailing whitespace is
+# ignored in the comparison
+#
+# Note: an empty string will
+#       NOT be matched as
+#       a single string and
+#       hence returns '0'
+is_single_string() {
+    rv=0
+    if [ -n "$1" ]; then
+        ltrem=`strip_ws "$1"`
+        # now see what happens if we remove remaining ws
+        cmd="echo '${ltrem}' | ${sed} 's/ //g'"
+        sstr=`eval ${cmd}`
+        if [ "${sstr}" = "${ltrem}" ]; then
+            rv=1
+        fi
+    fi
+    echo ${rv}
+}
+
+
+# Mark the fact that we read this file...
+generic_read="yes"
+
+# this is the final fi of the 'include guard'
+fi
+
+# Add to the $PATH, only once to avoid double entries
+pathadd() {
+    if [[ ":$PATH:" != *":$1:"* ]]; then
+        export PATH="${PATH:+"$PATH:"}$1"
+    fi
+}
diff --git a/tools/quartus/run_all_qsys b/tools/quartus/run_all_qsys
index 05056ff9b5..3db7158cae 100755
--- a/tools/quartus/run_all_qsys
+++ b/tools/quartus/run_all_qsys
@@ -45,7 +45,7 @@ fi
 set -u
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_all_sopc b/tools/quartus/run_all_sopc
index bc880f4a67..da8d133579 100755
--- a/tools/quartus/run_all_sopc
+++ b/tools/quartus/run_all_sopc
@@ -46,7 +46,7 @@ fi
 set -u
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_app b/tools/quartus/run_app
index 6da1d68dc1..941d14628b 100755
--- a/tools/quartus/run_app
+++ b/tools/quartus/run_app
@@ -45,7 +45,7 @@ fi
 set -u
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_app_clean b/tools/quartus/run_app_clean
index 8f194ec833..75c45a093e 100755
--- a/tools/quartus/run_app_clean
+++ b/tools/quartus/run_app_clean
@@ -45,7 +45,7 @@ fi
 set -u
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_bsp b/tools/quartus/run_bsp
index 55b1ef7d70..628b5429c3 100755
--- a/tools/quartus/run_bsp
+++ b/tools/quartus/run_bsp
@@ -17,7 +17,7 @@ fi
 set -u
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_mif b/tools/quartus/run_mif
index c0421cbb97..2592020ce1 100755
--- a/tools/quartus/run_mif
+++ b/tools/quartus/run_mif
@@ -43,7 +43,7 @@ fi
 set -u
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_qcomp b/tools/quartus/run_qcomp
index 704d394738..a7341c5f73 100755
--- a/tools/quartus/run_qcomp
+++ b/tools/quartus/run_qcomp
@@ -46,7 +46,7 @@ set -u
 
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_qsys b/tools/quartus/run_qsys
index bb534f2e64..c0751d760e 100755
--- a/tools/quartus/run_qsys
+++ b/tools/quartus/run_qsys
@@ -37,7 +37,7 @@ fi
 set -u
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_rbf b/tools/quartus/run_rbf
index a98d2e9bbf..a38aa86693 100755
--- a/tools/quartus/run_rbf
+++ b/tools/quartus/run_rbf
@@ -36,7 +36,7 @@ fi
 set -u
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_reg b/tools/quartus/run_reg
index f96f36f8a4..f5c5d31302 100755
--- a/tools/quartus/run_reg
+++ b/tools/quartus/run_reg
@@ -36,7 +36,7 @@ fi
 set -u
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_regtest_synth b/tools/quartus/run_regtest_synth
index f2a1cdbfd1..a18e6e1656 100755
--- a/tools/quartus/run_regtest_synth
+++ b/tools/quartus/run_regtest_synth
@@ -33,7 +33,7 @@
 ###############################################################################
 # Source generic.sh for functions such as unb_info
 ###############################################################################
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 ###############################################################################
 # We're assuming the local 'SVN' dir is up to date. Copy it to a timestamped
diff --git a/tools/quartus/run_sof b/tools/quartus/run_sof
index faf276d20b..34011399b0 100755
--- a/tools/quartus/run_sof
+++ b/tools/quartus/run_sof
@@ -33,7 +33,7 @@ fi
 
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_sopc b/tools/quartus/run_sopc
index 651669bf87..b9090a8ebf 100755
--- a/tools/quartus/run_sopc
+++ b/tools/quartus/run_sopc
@@ -39,7 +39,7 @@ fi
 set -u
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
diff --git a/tools/quartus/run_term b/tools/quartus/run_term
index 36fa82d85f..8fef2ceb37 100755
--- a/tools/quartus/run_term
+++ b/tools/quartus/run_term
@@ -32,7 +32,7 @@ if [ ! $1 ]; then
 fi
 
 # read generic functions/definitions
-. ${UNB}/Firmware/software/build/generic.sh
+. ${RADIOHDL}/tools/quartus/generic.sh
 
 toolset=$1
 . ${RADIOHDL}/tools/quartus/set_quartus ${toolset}
-- 
GitLab