]> diplodocus.org Git - nmh/blobdiff - docs/contrib/build_nmh
Reworked output handling.
[nmh] / docs / contrib / build_nmh
index b69dc08a98c14e9f28a20b7b242a919a69e6fb15..60913b6856f4a80847782c5033ee18e2aab5eb17 100755 (executable)
@@ -9,7 +9,7 @@
 #   walks you through confirmation of common configuration settings.
 #
 # This file can be downloaded and immediately run using, e.g.,
 #   walks you through confirmation of common configuration settings.
 #
 # This file can be downloaded and immediately run using, e.g.,
-#   wget http://git.savannah.gnu.org/cgit/nmh.git/plain/docs/contrib/build_nmh
+#   wget http://git.savannah.gnu.org/cgit/nmh.git/plain/build_nmh
 #   sh build_nmh
 #
 # Typical usage:
 #   sh build_nmh
 #
 # Typical usage:
@@ -44,7 +44,8 @@
 # See the nmh MACHINES file for build prerequisites.  In addition, the rpmbuild
 # is required to be available if the -r option is used.
 
 # See the nmh MACHINES file for build prerequisites.  In addition, the rpmbuild
 # is required to be available if the -r option is used.
 
-usage="usage: $0
+logfile=build_nmh.log
+usage="usage:
   [-b <branch>, only if downloading]
   [-c to run 'make distcheck' instead of 'make check']
   [-d to build nmh with asserts enabled and optimization disabled]
   [-b <branch>, only if downloading]
   [-c to run 'make distcheck' instead of 'make check']
   [-d to build nmh with asserts enabled and optimization disabled]
@@ -55,15 +56,6 @@ usage="usage: $0
   [-v to display progress]
   [-y to accept all configuration options without confirmation]"
 
   [-v to display progress]
   [-y to accept all configuration options without confirmation]"
 
-#### Exit with error message.
-die() {
-  status=1
-  exec 1>&3 3>&- 2>&4 4>&-
-  cat "$tmpfile" 1>&2
-  echo "$0: $*" 1>&2
-  exit 1
-}
-
 #### Find location of a program.  Bourne shell just puts the name in
 #### $0 if it's found from the PATH, so search that if necessary.
 finddir() {
 #### Find location of a program.  Bourne shell just puts the name in
 #### $0 if it's found from the PATH, so search that if necessary.
 finddir() {
@@ -76,22 +68,106 @@ finddir() {
   esac
 }
 
   esac
 }
 
-#### Make sure user sees error output even on early termination.  Assumes
-#### that called programs exit with non-zero status when terminated.
+#### Make sure user sees error output even on early termination.
 cleanup() {
 cleanup() {
-  if [ $status -eq 0 ]; then
-    rm -f "$tmpfile"
+  if [ "$tmpfile" ]; then
+    #### Disable output redirection (and flush) so that we can grep.
+    #### If $tmpfile is null, don't need to do this because the
+    #### outputs weren't redirected, modulo a small race condition
+    #### between setting tmpfile and redirecting the outputs.
+    exec 1>&3 3>&- 2>&4 4>&-
+
+    if [ "$logfile" != - ]; then
+      exec 3>&1 >"$logfile" 2>&1
+    fi
+
+    if [ -f "$tmpfile" ]; then
+      cat "$tmpfile"
+      grep -E 'Error|warn' "$tmpfile"
+      rm "$tmpfile"
+    fi
+  fi
+
+  #### Put info message on stdout, and in log if not stdout.
+  if [ $status -ne 0  -o  $verbose -ge 1  -o  "$directory" ]; then
+    [ $status -eq 0 ]  &&  result=succeeded  ||  result=failed
+    if [ "$logfile" = - ]; then
+      echo "build $result"
+    else
+      message="build $result, build log is in ${directory:+$directory/}$logfile"
+      echo "$message" >&3
+    fi
+  fi
+
+  #### Heirloom shell does not like "trap - signal".
+  trap '' EXIT
+  exit $status
+}
+
+#### Exit with error message.
+die() {
+  status=1 # It should already be, but just in case the code below changes.
+  echo "$0: $*" 1>&2
+  cleanup
+}
+
+#### Download sources from repo.  With git and on master, the
+#### directory will be nmh, but with snapshot, it will be
+#### nmh-master.
+download_sources() {
+  [ $verbose -ge 1 ]  &&  echo downloading . . . >&3
+  gitdir=`finddir git`
+  if [ "$gitdir" ]; then
+    #### Use git repo.
+    [ "$verbose" -eq 0 ]  &&  git_opts=--quiet
+    [ "$branch" = master ]  ||
+      git_opts="${git_opts:+$git_opts }--branch $branch"
+    if "$gitdir"/git clone --depth 1 $git_opts "git://$gitrepo/nmh.git" >&3
+    then
+      directory=nmh
+      cd "$directory"  ||  die "failed to clone $directory"
+      printf "commit %s\n" `git log --max-count=1 --pretty=format:%H`
+    else
+      die 'failed to clone git repo'
+    fi
   else
   else
-    [ $logfile = - ] && cat "$tmpfile" 1>&3 || mv "$tmpfile" "$logfile"
+    [ -e nmh-"$branch" ]  &&  die "nmh-$branch exists, will not overrwrite"
+
+    #### Use snapshot.
+    tarball="nmh-$branch.tar.gz"
+    repo="http://$gitrepo/cgit/nmh.git/snapshot"
+    snapshot="$repo/$tarball"
+    if [ "`finddir wget`" ]; then
+      [ "$verbose" -eq 0 ] && wget_opts='--quiet'
+      wget --output-document - $wget_opts "$snapshot" 2>&3 | gzip -d | tar xf -
+    elif [ "`finddir curl`" ]; then
+      [ "$verbose" -eq 0 ] && curl_opts='--silent --show-error'
+      curl --location $curl_opts "$snapshot" 2>&3 | gzip -d | tar xf -
+    else
+      die 'unable to find program to download nmh sources'
+    fi
+
+    if [ -d nmh-"$branch" ]; then
+      directory=nmh-"$branch"
+      cd "$directory"  ||  die "failed to download and extract $directory"
+    else
+      die "failed to download nmh-$branch sources"
+    fi
   fi
 }
   fi
 }
-trap cleanup EXIT
 
 directory=
 
 directory=
+download=0
 gitrepo=git.savannah.nongnu.org
 invocation="$0 $*"
 gitrepo=git.savannah.nongnu.org
 invocation="$0 $*"
+status=1
 tmpfile=/tmp/build_nmh-$$.log
 
 tmpfile=/tmp/build_nmh-$$.log
 
+#### Redirect all output to tmp file.  Then on EXIT, copy it to either
+#### logfile or stdout.  Also, grep it for errors and warnings.  Set
+#### tmpfile just prior to this, see cleanup().
+exec 3>&1 4>&2 >"$tmpfile" 2>&1
+
 
 ####
 #### Interpret command arguments.
 
 ####
 #### Interpret command arguments.
@@ -100,12 +176,15 @@ branch=master
 check=check
 debug=0
 install=0
 check=check
 debug=0
 install=0
-logfile=build_nmh.log
 build_rpm=0
 superclean=0
 verbose=0
 yes=0
 
 build_rpm=0
 superclean=0
 verbose=0
 yes=0
 
+#### With dash, heirloom shell, and posh, need to catch INT and QUIT
+#### in order for cleanup to be call:  just EXIT isn't sufficient.
+trap cleanup EXIT INT QUIT
+
 while getopts 'cb:dil:rsvy?' arg; do
   case $arg in
     b  ) branch="$OPTARG" ;;
 while getopts 'cb:dil:rsvy?' arg; do
   case $arg in
     b  ) branch="$OPTARG" ;;
@@ -117,16 +196,11 @@ while getopts 'cb:dil:rsvy?' arg; do
     s  ) superclean=1 ;;
     v  ) verbose=1 ;;
     y  ) yes=1 ;;
     s  ) superclean=1 ;;
     v  ) verbose=1 ;;
     y  ) yes=1 ;;
-    '?') echo "$usage"; exit 0 ;;
+    '?') echo "$0: $usage"; logfile=-; status=0; exit ;;
   esac
 done
 shift `expr $OPTIND - 1`
 
   esac
 done
 shift `expr $OPTIND - 1`
 
-#### Redirect all output to tmp file.  Then at end of script, copy
-#### it to either logfile or stdout.  Also, grep it for errors and
-#### warnings.
-exec 3>&1 4>&2 >"$tmpfile" 2>&1
-
 echo "$invocation"
 
 #### No non-option command line arguments are supported.
 echo "$invocation"
 
 #### No non-option command line arguments are supported.
@@ -136,45 +210,7 @@ echo "$invocation"
 if grep 'the authors of nmh' COPYRIGHT >/dev/null 2>&1; then
   :
 else
 if grep 'the authors of nmh' COPYRIGHT >/dev/null 2>&1; then
   :
 else
-  #### Download sources from repo.
-  [ $verbose -ge 1 ]  &&  echo downloading . . . >&3
-  gitdir=`finddir git`
-  if [ "$gitdir" ]; then
-    #### Use git repo.
-    [ "$verbose" -eq 0 ]  &&  git_opts=--quiet
-    [ "$branch" == master ]  ||
-      git_opts="${git_opts:+$git_opts }--branch $branch"
-    if "$gitdir"/git clone --depth 1 $git_opts "git://$gitrepo/nmh.git" >&3; then
-      directory=nmh
-      cd "$directory"  ||  die "failed to clone $directory"
-      printf "commit %s\n" `git log --max-count=1 --pretty=format:%H`
-    else
-      die 'failed to clone git repo'
-    fi
-  else
-    [ -e nmh-"$branch" ]  &&  die "nmh-$branch exists, will not overrwrite"
-
-    #### Use snapshot.
-    tarball="nmh-$branch.tar.gz"
-    repo="http://$gitrepo/cgit/nmh.git/snapshot"
-    snapshot="$repo/$tarball"
-    if [ "`finddir wget`" ]; then
-      [ "$verbose" -eq 0 ] && wget_opts='--quiet'
-      wget --output-document - $wget_opts "$snapshot" 2>&3 | gzip -d | tar xf -
-    elif [ "`finddir curl`" ]; then
-      [ "$verbose" -eq 0 ] && curl_opts='--silent --show-error'
-      curl --location $curl_opts "$snapshot" 2>&3 | gzip -d | tar xf -
-    else
-      die 'unable to find program to download nmh sources'
-    fi
-
-    if [ -d nmh-"$branch" ]; then
-      directory=nmh-"$branch"
-      cd "$directory"  ||  die "failed to download and extract $directory"
-    else
-      die "failed to download nmh-$branch sources"
-    fi
-  fi
+  download=1
 fi
 
 ####
 fi
 
 ####
@@ -241,7 +277,7 @@ if install-mh -check >/dev/null 2>&1; then
   fi
 fi
 
   fi
 fi
 
-#### Don't confirm debug interactively below; obey the -d option to this script.
+#### Don't confirm debug interactively below; obey the -d option.
 [ $debug -ge 1 ]  &&  config_debug=y
 
 if [ $yes -eq 0 ]; then
 [ $debug -ge 1 ]  &&  config_debug=y
 
 if [ $yes -eq 0 ]; then
@@ -300,6 +336,8 @@ fi
 printf '\n%s %s %s %s\n\n' "`uname -m`" "`uname -s`" "`uname -r`" "`uname -v`"
 [ -f /etc/os-release ]  &&  printf '%s\n\n' "`cat /etc/os-release`"
 
 printf '\n%s %s %s %s\n\n' "`uname -m`" "`uname -s`" "`uname -r`" "`uname -v`"
 [ -f /etc/os-release ]  &&  printf '%s\n\n' "`cat /etc/os-release`"
 
+[ $download -eq 1 ]  &&  download_sources
+
 ####
 #### Set up with autoconfig if necessary.
 ####
 ####
 #### Set up with autoconfig if necessary.
 ####
@@ -402,27 +440,5 @@ else
   echo "see nmh MACHINES file for build dependences"
 fi
 
   echo "see nmh MACHINES file for build dependences"
 fi
 
-####
-#### Report results.
-####
-
-#### Disable output redirection (and flush) so that we can grep.
-exec 1>&3 3>&- 2>&4 4>&-
-
-if [ "$logfile" != - ]; then
-  rm -f "$logfile"
-  exec 3>&1 >"$logfile" 2>&1
-fi
-
-[ -f "$tmpfile" ]  &&  cat "$tmpfile"
-[ -f "$tmpfile" ]  &&  grep -E 'Error|warn' "$tmpfile"
-
-#### Put message on stdout, and in log if different.
-if [ $status -ne 0  -o  $verbose -ge 1  -o  "$directory" ]; then
-  [ $status -eq 0 ]  &&  indication=succeeded  ||  indication=failed
-  message="build $indication, build log is in ${directory:+$directory/}$logfile"
-  echo "$message"
-  [ "$logfile" = - ]  ||  echo "$message" >&3
-fi
-
-exit $status
+#### Will call cleanup, which will exit with $status.
+exit