+#### 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() {
+ case $1 in
+ */*) dirname "$1" ;;
+ * ) IFS=:
+ for d in $PATH; do
+ [ -f "${d:=.}/$1" -a -x "$d/$1" ] && printf %s "$d" && break
+ done ;;
+ esac
+}
+
+#### Make sure user sees error output even on early termination.
+cleanup() {
+ 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
+ [ -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
+}
+
+directory=
+download=0
+gitrepo=git.savannah.nongnu.org
+invocation="$0 $*"
+status=1
+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