#!/bin/bash

################################################################################
#
# Driver for Xilinx network controllers and boards
# Copyright 2021 Xilinx Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation, incorporated herein by reference.
#
################################################################################

######################################################################
# rpm-builder
######################################################################

me=$(basename "$0")
bin=$(cd "$(dirname "$0")" && /bin/pwd)

err  () { echo 1>&2 "$*";   }
log  () { err "$me: $*";     }
vlog () { $verbose && err "$me: $*"; }
fail () { log "$*"; exit 1; }
try  () { "$@" || fail "'$*' failed"; }
trace() { log "invoking: $*"; "$@"; }
vtrace() { vlog "invoking: $*"; "$@"; }


usage() {
  err
  err "usage:"
  err "  $me [options] <dir> <tag> net <distro> <sp> <arch:kernel-version>..."
  err
  err "options:"
  err "  --disttag <x>     -- specify disttag (only used for log file name)"
  err "  -v                -- verbose logging"
  err
  err "eg: $me /home/bob/v5 v1_1_5010 net RHEL5 base i686:2.4.20-8"
  err
  exit 1
}


buildrpms() {
  local arch="$1"
  local kernel="$2"
  local logf="$logd/rpm-$TAG-$disttag-$arch-$kernel.log"
  local makerpm="$dir/SPECS/xilinx-efct.mmakerpm"

  try [ -x "$makerpm" ]

  # Ensure consistent (and fast) sorting.  This appears to be significant
  # for SLES10 at least.
  export LC_ALL=C

  # Build everything with -Werror to spot build problems
  export EXTRA_CFLAGS=-Werror

  if [ "$kernel" = user ]; then
    # Net drivers do not have a user-land component.
    if [ ! "$type" = net ]; then
      log "Generating userland rpm: arch=$arch log=$(basename "$logf")"
      vtrace "$makerpm" --$type --without-modules --arch "$arch" &> "$logf"
    fi
  else
    log "Generating rpm: arch=$arch kernel=$kernel log=$(basename "$logf")"
    vtrace "$makerpm" \
	--$type --without-userland --arch "$arch" "$kernel" &> "$logf"
  fi
  rc=$?

  $verbose && [ -f "$logf" ] && cat "$logf"

  [ $rc = 0 ] || {
    log "FAILED: $rc"
    try mkdir -p "$logd/failures"
    try mv "$logf" "$logd/failures"
  }
  return $rc
}


######################################################################
# main()

verbose=false
disttag=


# Parse the command line arguments.
while [ $# -gt 0 ]; do
  case "$1" in
    --disttag)	disttag="$2"; shift;;
    -v)		verbose=true;;
    -*)		usage;;
    *)		break;;
  esac
  shift
done

[ $# -lt 5 ] && usage
dir="$1/rpm"
shift
TAG="$1"
shift
type="$1"
shift
DISTRO=$1
shift
SP=$1
shift

vlog "whoami=$(whoami)"
vlog "$(uname -a)"

# Some sanity please!
try [ -d "$dir" ]
try cd "$dir"

# Put the scripts directory on the path.
logd="$dir/logs"
try mkdir -p "$logd"

# Generate binary RPMs (if any kernels specified).
anybad=false

while [ $# -gt 0 ]; do
  kernel=${1##*:}
  arch=${1%%:*}
  shift

  [ -n "$LD_PRELOAD" ] && {
    # This bit of hackery combines with hackery in the rpmbuild-master
    # scripts to make it look like we're running on a different system from
    # the one the kernel thinks we're running.  (Needed because we run this
    # script in a chroot environment).
    export "FAKEARCH_MACHINE=$arch"
    vlog "fakearch: $(uname -a)"
  }

  buildrpms "$arch" "$kernel" || anybad=true
done

$anybad && {
  log "***** One or more builds failed! *****"
  exit 1
}

vlog "Exiting."
exit 0
