#!/bin/bash
# Version: 1.3 GSB SMBuild.
# Copyright (c) 2007 Darren 'Tadgy' Austin <darren (at) gnomeslackbuild.org>
#
# Licenced under the terms of the GNU General Public Licence version 3.
#
# Modified and trimmed extensively for use with SMLinux

# The default list of GSB sections, in the order they should be processed.
SECTIONS="${SECTIONS:-  base xorg gtk extra net xfce }"

# Generate HTML and TXT reports. Use 1 to enable and 0 to disable.
SM_GENERATEREPORTS=0

# Usage.
function usage() {
  cat << EOF
Usage: ${0##*/} [options]

Options:
  -help			Show this help screen.

  -list			For each of the GSB sections, list the packages which
			will be built.  When the SECTIONS environment variable
			is set (see README) only those sections are displayed.
EOF
}

export -f usage

function list_packages() {
  echo "The sections and packages below (listed in processing order) will be built."
  for SECTION in $SECTIONS
  do
    echo
    echo "  $SECTION:"
    echo "$( $SECTION/ssb.$SECTION.SMBuild --list | cut -d$'\n' -f2- )"
  done
}

function runtime() {
  # $1 = Number of seconds to convert to readable text

  local D=$(( $1 / 86400 ))
  local H=$(( ($1 - ($D * 86400)) / 3600 ))
  local M=$(( ($1 - ($D * 86400) - ($H * 3600)) / 60 ))
  local S=$(( $1 - ($D * 86400) - ($H * 3600) - ($M * 60) ))

  if [ $D -gt 0 ]; then
    echo -n "${D}d, ${H}h ${M}m ${S}s"
  else
    echo -n "${H}h, ${M}m ${S}s"
  fi
  return 0
}

export -f runtime

# Make sure we are in the right directory (you can never trust users..)
cd $( cd ${BASH_SOURCE%/*} ; pwd )

# Setup the environment
# Use custom build variables file 
BUILDVARS="/etc/buildvars.conf"
SM_PARENTTMP="/tmp"
PKGDEST=${PKGDEST:-$SM_PARENTTMP/sml/packages}
LOGSDIR=${LOGSDIR:-$SM_PARENTTMP/sml/sml-buildlogs}
SM_AUTOBUILD=1
ARCH="$HOSTTYPE"
export BUILDVARS PKGDEST LOGSDIR SM_AUTOBUILD ARCH SM_PARENTTMP

# Temporary space, package and log file storage.
mkdir -p $PKGDEST $LOGSDIR

SM_AUTOBUILDTEMP="$(mktemp $SM_PARENTTMP/SMBUILD.XXXXXX)"
export SM_AUTOBUILDTEMP

function autointerruptsummary() {
   cat "$SM_AUTOBUILDTEMP"
   rm "$SM_AUTOBUILDTEMP"
}

trap "autointerruptsummary" INT
###################################################


# Parse command line arguments.
while [ $# -gt 0 ]; do
  if [ "$1" = "-help" ] || [ "$1" = "--help" ]; then
    usage
    exit 0
  elif [ "$1" = "-list" ] || [ "$1" = "--list" ]; then
    list_packages
    exit 0
    shift
  else
    echo "${0##*/}: Unknown option: $1"
    echo "Try: $0 -help"
    exit 1
  fi
done

# Do the build.
( echo
  echo "*********************************************************************"
  echo "* Building SMLinux - this may take a while..."
  echo "*********************************************************************"
  
  for SECTION in $SECTIONS
  do
    ( cd $SECTION && ./ssb.$SECTION.SMBuild 2>&1 ) || {
      echo
      echo "*********************************************************************"
      echo "* Build aborted: error building in section '$SECTION'."
      echo "* Check the build logs in '$LOGSDIR' and try again."
      echo "*********************************************************************"
      echo "* Build failed after $( runtime $SECONDS )."
      echo "*********************************************************************"
      rm -f "$SM_AUTOBUILDTEMP"
      exit 1
    }
  # Generate HTML tables
  if [ "$SM_GENERATEREPORTS" = "1" ] && [ -x /bin/genreports ] ; then
    /bin/genreports $SECTION 1.0 aarch64
  fi
  done

  echo
  echo "*********************************************************************"
  echo "* Successfully finished building SMLinux!"
  echo "* SMLinux packages are in '$PKGDEST/$ARCH'."
  echo "* Build logs are in '$LOGSDIR'."
  echo "*********************************************************************"
  echo "* Complete build time was $( runtime $SECONDS )."
  echo "*********************************************************************"
  rm -f "$SM_AUTOBUILDTEMP"
) 2>&1 | tee $LOGSDIR/$( basename $0 .SMBuild )-$ARCH.log.txt

# Return the exit status from the sub-shell, not the tee command.
exit ${PIPESTATUS[0]}
