#! /bin/sh
#
# A wrapper to create packages of the Kernel.
#

set -e  ## Get out in errors.

MYNAME=${0##*/}

# Functions.
print_help() {
  printf '%s\n'                                              \
   ""                                                        \
   "Usage:"                                                  \
   "./${MYNAME} <keyword> <version> <architecture> [suffix]" \
   ""                                                        \
   "Examples:"                                               \
   "./${MYNAME} headers 3.2.78 x86"                          \
   "./${MYNAME} image   3.2.78 i486   -gen"                  \
   "./${MYNAME} image   3.2.78 x86_64 -smp64"                \
   ""                                                        \
   "Valid keywords:"                                         \
   "headers, image, firmware, modules"                       \
   ""
}

warn() { printf '%b\n' "$@" >&2; }

# Variables:
CWD=$(pwd)                 ## Current Work Directory.

TMP=${TMP:-/tmp/sources}   ## Temporary directory of the source(s).
OUT=${OUT:-/tmp/packages}  ## Out of the package(s).

KVERSION=$2                ## Kernel version.
PKGARCH=$3                 ## Architecture for the package, ex.: i686.
SUFFIX=$4                  ## Suffix for the package, ex.: smp.

# Check arguments:
if [ -z "$1" ]; then
  warn "You are missing an argument. See the help."
  print_help
  exit 1;
fi
if [ -z "$KVERSION" ] || [ -z "$PKGARCH" ]; then
  warn "You are missing an argument. See the help."
  print_help
  exit 1;
fi

# Options:
case "$1" in
  headers)
    # Determine the ARCH to build the 64-bit headers:
    if [ "$PKGARCH" = "x86_64" ]; then
      export ARCH=x86_64
    fi
    . ${CWD}/scripts/headers
  ;;
  image)
    . ${CWD}/scripts/image
  ;;
  firmware)
    . ${CWD}/scripts/firmware
  ;;
  modules)
    . ${CWD}/scripts/modules
  ;;
  *)
    print_help
    exit 0;
esac

