prepbuildsummary() {
  # Start of the SM_SHOWSUMMARY if/else check
  if [ -n "$SM_SHOWSUMMARY" ] && [ "$SM_SHOWSUMMARY" = "1" ]; then
    # Get the build completion time and store it in a variable
    SM_FINISHDATE="$(date '+%a, %d %b %Y, %T')"

    # Stick to 8/16 colours, those are supported on most terminals
    if [ "$SM_COLOURS" = "1" ]; then
      SM_COLOURSCHECK="$(tput colors 2>/dev/null)"
      if [ "$?" = "0" ] && [ "$SM_COLOURSCHECK" -gt "2" ] ; then
        # Red when the build fails
        SM_COLOURR="\e[41m"
        # Yellow when the build is interrupted
        SM_COLOURY="\e[93m"
        # Green when the build succeeds
        SM_COLOURG="\e[92m"
        # Cyan for the short questions
        SM_COLOURC="\e[96m"
        # APPNAME/VERSION Colours
        SM_COLOURV="\e[92m"
        # Restore to default
        SM_COLOURD="\e[0m"
      fi
    fi

    # Determine the build type
    if [ -n "$SM_AUTOBUILD" ]; then
      # We are using Tadgy's autobuild system
      SM_BUILDSYS="SSB Autobuild"
    else
      # We compiled the package manually
      SM_BUILDSYS="Manual"
    fi


    # Determine if distcc was used. If SM_GLOBALDISTCC is enabled and  set to 1 and SM_DISTCC is not declared in the
    # package build file, then set SM_DSTATS in the build summary 
    if [ -n "$SM_GLOBALDISTCC" ] && [ "$SM_GLOBALDISTCC" = "1" ] && \
       [ -z "$SM_DISTCC" ]; then
      SM_DSTATS="Yes"
    # Else if SM_GLOBALDISTCC is enabled and set to 1 and SM_DISTCC is set to 0 in the package build file, then set
    # SM_DSTATS in the build summary
    elif [ -n "$SM_GLOBALDISTCC" ] && [ "$SM_GLOBALDISTCC" = "1" ] && \
         [ "$SM_DISTCC" = "0" ]; then
      SM_DSTATS="Nope, disabled but global variable set"
    # Else If SM_GLOBALDISTCC is unset, set SM_DSTATS in the build summary
    elif [ -z "$SM_GLOBALDISTCC" ] || [ "$SM_GLOBALDISTCC" = "0" ]; then
      SM_DSTATS="No, disabled globally"
    fi


    # Determine if ccache was used. Ccache caches compiler objects to disk, which speeds up subsequent compiles.
    # However, if we are compiling  inside tmpfs, we are not using ccache at all. So we set SM_CSTATS accordingly
    # in the build summary
    if [ -n "$SM_GLOBALCCACHE" ] && [ "$SM_GLOBALCCACHE" = "1" ] && \
       [ -n "$SM_USETMPFS" ] && [ "$SM_USETMPFS" = "1" ] && \
       [ -n "$SM_TMPFSENABLEDFORTHISPACKAGE" ] && \
       [ "$SM_TMPFSENABLEDFORTHISPACKAGE" = "1" ] ; then
       SM_CSTATS="Enabled globally but disabled due to tmpfs"

    elif [ -n "$SM_GLOBALCCACHE" ] && [ "$SM_GLOBALCCACHE" = "1" ] && \
         [ -n "$SM_USETMPFS" ] && [ "$SM_USETMPFS" = "0" ] ; then
       SM_CSTATS="Yes"

    elif [ -n "$SM_GLOBALCCACHE" ] && [ "$SM_GLOBALCCACHE" = "1" ] && \
         [ -z "$SM_USETMPFS" ] ; then
       SM_CSTATS="Yes"

    elif [ -z "$SM_GLOBALCCACHE" ] || [ "$SM_GLOBALCCACHE" = "0" ] ; then
       SM_CSTATS="No, disabled globally"
    fi


    # Determine the build type
    if [ "$SM_DEBUG" = "1" ] ; then
      SM_BLDTYPE="*DEBUG* build"
    else
      SM_BLDTYPE="General build, no debug symbols"
    fi


    # Determine whether tmpfs was used
    if [ "$SM_USETMPFS" = "1" ] && \
       [ "$SM_TMPFSENABLEDFORTHISPACKAGE" = "1" ] ; then
      SM_TMPFSSTATE="Yes"

    elif [ "$SM_USETMPFS" = "1" ] && \
         [ "$SM_TMPFSENABLEDFORTHISPACKAGE" = "0" ]; then
      SM_TMPFSSTATE="*Not for this package* but enabled globally"

    elif [ "$SM_USETMPFS" = "1" ] && \
         [ "$SM_TMPFSCHECKFAILED" = "1" ]; then
      SM_TMPFSSTATE="*NOPE, TMPFS DIRECTORY CHECK FAILED* but enabled globally"

    else
      SM_TMPFSSTATE="No, disabled globally"
    fi


    if [ -n "$SM_CPUTHREADS" ]; then
      SM_MAKEFLAGS="$MAKEFLAGS, manually set"
    else
      SM_MAKEFLAGS="$MAKEFLAGS, auto-detected"
    fi
       
    #  If SM_COMPILETIMEA is set, then do a sum total of SM_COMPILETIMEA and SM_PACKAGETIMEA variables to get the
    # total time in seconds and use that as an argument for the runtime function. If SM_COMPILETIMEA is not set,
    # invoke the runtime function alone on new reset $SECONDS
    if [ -n "$SM_COMPILETIMEA" ]; then
      SM_FINALCOMPILETIME="$(( $SM_COMPILETIMEA + $SM_PACKAGETIMEA ))"
      SM_TOTALTIME="$( runtime $SM_FINALCOMPILETIME )"
    else
      SM_TOTALTIME="$( runtime $SECONDS )"
    fi

    # Determine if the build was successful or not
    if [ "$SM_PKGSTATUS" = "0" ] ; then
      SM_BLDSTATUS="$(echo -e "$SM_COLOURG"'Successful! :-D' \
"$SM_COLOURD")"

    if [ -n "$SM_HTMLOUTPUT" ] && [ "$SM_HTMLOUTPUT" = "1" ] ; then
      cat << EOF >> $SM_PARENTTMP/BUILDMONITOR.html
<tr><td><b><i><a href="/smlinux/pkgresults?pkg=$APP&amp;smver=1.0&amp;arch=all&amp;resultnum=25">$APP $VERSION</a></i></b></td><td>$SM_COMMENCEDATE</td><td>$SM_FINISHDATE</td><td>$SM_TOTALTIME</td><td><b style="color:#00cc00;">SUCCEEDED</b></td></tr>
EOF

      rm -f $SM_PARENTTMP/BUILDING
    fi

    # Determine the compressed size
    SM_PACKSIZE="$(du -bk "$SM_PACKLOCATION" | awk '{print $1}')"

    # Determine the uncompressed size
    SM_PACKUSIZE="$(echo $SM_PACKUSIZE1)"

    elif [ "$SM_WASINTERRUPTED" = "1" ]; then
      SM_BLDSTATUS="$(echo -e "$SM_COLOURY"\
'** INTERRUPTED ** :-/'"$SM_COLOURD")" 

      if [ -n "$SM_HTMLOUTPUT" ] && [ "$SM_HTMLOUTPUT" = "1" ] ; then
        cat << EOF >> $SM_PARENTTMP/BUILDMONITOR.html
<tr><td><b><i><a href="/smlinux/pkgresults?pkg=$APP&amp;smver=1.0&amp;arch=all&amp;resultnum=25">$APP $VERSION</a></i></b></td><td>$SM_COMMENCEDATE</td><td>$SM_FINISHDATE</td><td>$SM_TOTALTIME</td><td><b>INTERRUPTED</b></td></tr>
EOF

         rm -f $SM_PARENTTMP/BUILDING
       fi

      else 
        SM_BLDSTATUS="$(echo -e "$SM_COLOURR"'!! FAILED !! :-('\
"$SM_COLOURD")"

      if [ -n "$SM_HTMLOUTPUT" ] && [ "$SM_HTMLOUTPUT" = "1" ] ; then
        cat << EOF >> $SM_PARENTTMP/BUILDMONITOR.html
<tr><td><b><i><a href="/smlinux/pkgresults?pkg=$APP&amp;smver=1.0&amp;arch=all&amp;resultnum=25">$APP $VERSION</a></i></b></td><td>$SM_COMMENCEDATE</td><td>$SM_FINISHDATE</td><td>$SM_TOTALTIME></td><td><b style="color:#ff1a1a;">FAILED</b></td></tr>
EOF

        rm -f $SM_PARENTTMP/BUILDING
        fi


      fi


    # Finally prepare the summary
    echo "" >> "$SM_TEMPFILE"

    echo -e ""$SM_COLOURC"---------------------------------------------\
----------------------------------" \
    >> "$SM_TEMPFILE"

    echo -e " BUILD SUMMARY FOR PACKAGE "$SM_COLOURD"'"$SM_COLOURV"\
"$APP""$SM_COLOURD"'"$SM_COLOURC" VERSION "$SM_COLOURD"'"$SM_COLOURV"\
"$VERSION"'" "$SM_COLOURC"TAG"$SM_COLOURV" "'$BUILD'"$SM_COLOURD"" \
    >> "$SM_TEMPFILE"

    echo -e ""$SM_COLOURC"---------------------------------------------\
----------------------------------"$SM_COLOURD"" \
    >> "$SM_TEMPFILE"

    echo -e ""$SM_COLOURC" Build Status:"$SM_COLOURD"  \
$SM_BLDSTATUS" \
    >> "$SM_TEMPFILE"


    # Output the section name if SM_AUTOBUILDTEMP is set. This means we are running an autobuild.
    if [ -n "$SM_AUTOBUILDTEMP" ]; then
      echo -e ""$SM_COLOURC" Build Section:"$SM_COLOURD" $SECTION" \
      >> "$SM_TEMPFILE"
    fi


    # If we have $SM_COMPILETIMEB set, then assume the compile went well and output compile and packaging times
    # into SM_TEMPFILE.
    if [ -n "$SM_TOTALTIME" ] && [ -z "$SM_PACKAGETIMEB" ]; then
      echo -e ""$SM_COLOURC" Total Time: "$SM_COLOURD"   $SM_TOTALTIME" \
      >> "$SM_TEMPFILE"
    elif [ -n "$SM_TOTALTIME" ] && [ -n "$SM_PACKAGETIMEB" ]; then
      echo -e ""$SM_COLOURC" Total Time: "$SM_COLOURD"   $SM_TOTALTIME \
( $SM_COMPILETIMEB Compile ) + ( $SM_PACKAGETIMEB Packaging )" \
      >> "$SM_TEMPFILE"
    fi


    echo -e ""$SM_COLOURC" Started:"$SM_COLOURD"       $SM_COMMENCEDATE" \
    >> "$SM_TEMPFILE"
    echo -e ""$SM_COLOURC" Stopped:"$SM_COLOURD"       $SM_FINISHDATE" \
    >> "$SM_TEMPFILE"

    # If the package was built successfully, output the installer sizes
    if [ "$SM_PKGSTATUS" = "0" ]; then
      #SM_COMPRESSEDSIZE="$(echo $(($SM_PACKSIZE * 100 / $SM_PACKUSIZE)))"
      # Space saving = 1 - Compressed Size / Uncompressed size. 
      # Also, bc code taken from:
      # https://stackoverflow.com/questions/56945130/bash-echo-percentage-with-no-decimal-point-with-result-returned-from-bc-comman
      SM_COMPRESSEDSIZE="$(echo $(echo "scale=2 ; 1 - "$SM_PACKSIZE" / \
      "$SM_PACKUSIZE"" | bc ) | sed 's@.@@')"

      echo -e ""$SM_COLOURC" Source Size:  "$SM_COLOURD" Compressed: \
$SM_SRCDIRSIZE"K", Uncompressed: $SM_BUILDDIRSIZE"K"" \
      >> "$SM_TEMPFILE"

      echo -e ""$SM_COLOURC" Package Size: "$SM_COLOURD" Uncompressed: \
$SM_PACKUSIZE"K", Compressed: $SM_PACKSIZE"K" ("$SM_COMPRESSEDSIZE'%'")" \
      >> "$SM_TEMPFILE"

      echo -e ""$SM_COLOURC" Package Has: "$SM_COLOURD"  \
$SM_TOTALFILECOUNT files and $SM_TOTALSYMCOUNT symlinks in $SM_TOTALDIRCOUNT \
directories" \
      >> "$SM_TEMPFILE"

    fi


    # If ccache was used, output the current cache used size and max allocated size
    if [ "$SM_GLOBALCCACHE" = "1" ] && [ "$SM_CCACHE" != "0" ] && \
       [ "$SM_CSTATS" = "Yes" ]; then
      SM_CCACHEUSEDSIZE="$(ccache -s | grep "cache size" | head -n 1 | \
      awk '{ $1=$2="" ; print $0}')"
      SM_CCACHETOTALSIZE="$(ccache -s | grep "max cache size" | \
      awk '{ $1=$2=$3="" ; print $0}')"

      echo -e ""$SM_COLOURC" Ccache Used?"$SM_COLOURD"   "$SM_CSTATS",\
"$SM_CCACHEUSEDSIZE" /"$SM_CCACHETOTALSIZE" Allocated" \
      >> "$SM_TEMPFILE"

    else
      echo -e ""$SM_COLOURC" Ccache Used?"$SM_COLOURD"   "$SM_CSTATS"" \
      >> "$SM_TEMPFILE"

    fi

    echo -e ""$SM_COLOURC" Distcc Used?"$SM_COLOURD"   $SM_DSTATS" \
    >> "$SM_TEMPFILE"


    # If distcc was used, cut out --randomize and output rest of the DISTCC_HOSTS variable
    if [ "$SM_GLOBALDISTCC" = "1" ] && [ "$SM_DISTCC" != "0" ]; then
      echo -e ""$SM_COLOURC" Distcc Args: "$SM_COLOURD" $(echo \
"$DISTCC_HOSTS" | sed 's@--randomize@@')" \
      >> "$SM_TEMPFILE"
    fi


    echo -e ""$SM_COLOURC" TMPFS Used? "$SM_COLOURD"   "$SM_TMPFSSTATE" $SM_TMPFSSAVINGSSIZE" \
    >> "$SM_TEMPFILE"

    echo -e ""$SM_COLOURC" CPU Threads: "$SM_COLOURD"  $(echo $SM_MAKEFLAGS | \
    sed 's@-j@@')" \
    >> "$SM_TEMPFILE"

    echo -e ""$SM_COLOURC" CFLAGS Used: "$SM_COLOURD"  $CFLAGS" \
    >> "$SM_TEMPFILE"

    echo -e ""$SM_COLOURC" Compressor: "$SM_COLOURD"   $COMPRESSOR \
($COMPRESSOPTS)" \
    >> "$SM_TEMPFILE"

    echo -e ""$SM_COLOURC" Build Type:" $SM_COLOURD"   $SM_BUILDSYS \
& $SM_BLDTYPE" \
    >> "$SM_TEMPFILE"

    echo -e ""$SM_COLOURC"---------------------------------------------------\
----------------------------"$SM_COLOURD"" \
    >> "$SM_TEMPFILE"


    # Output the build summary to the user on every build
    cat $SM_TEMPFILE

    # Once the output is shown, discard all temporary files
    rm -f $SM_TEMPFILE 
    if [ -z "$SM_AUTOBUILD" ] ; then
      rm -f $SM_PARENTTMP/$APP.{APP,BUILD,VERSION}
    fi
  fi
  # Completion of the SM_SHOWSUMMARY if/else check

  # Invoke the promptuser variable 
  promptuser
}  
  
