#!/bin/bash
# ---------------------------------------------------------------------------
# Pack the whole kit into one tarball to carry to the offline machine.
#
# Run this on the connected machine, after build-ltp.sh and download-rpms.sh.
#
#   ./make-bundle.sh
#   ./make-bundle.sh --no-ltp          scripts and RPMs only (a few MB)
#
# The result is kernel-test-<date>-<arch>.tar.gz next to this directory.
# ---------------------------------------------------------------------------
set -uo pipefail

KT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=lib/common.sh
. "$KT_ROOT/lib/common.sh"
kt_load_settings

OUT=$(dirname "$KT_ROOT")
WITH_LTP=1

usage() { sed -n '3,10p' "$0" | sed 's/^# \{0,1\}//'; cat <<'EOF'

Options:
  -o, --out DIR   where to write the tarball (default: the parent directory)
      --no-ltp    leave the LTP tarball out
  -h, --help      this text
EOF
}

while [ $# -gt 0 ]; do
    case $1 in
        -o|--out) OUT=$2; shift 2 ;;
        --no-ltp) WITH_LTP=0; shift ;;
        -h|--help) usage; exit 0 ;;
        *) kt_err "unknown option: $1"; usage; exit 2 ;;
    esac
done

NAME=kernel-test
STAMP=$(date +%Y%m%d)
ARCH=$(uname -m)
TARBALL=$OUT/$NAME-$STAMP-$ARCH.tar.gz

kt_head "Checking the bundle"
shopt -s nullglob
rpms=( "$KT_ROOT"/bundle/rpms/*.rpm )
ltps=( "$KT_ROOT"/bundle/ltp-*.tar.gz )
shopt -u nullglob

[ ${#rpms[@]} -gt 0 ] && kt_ok "${#rpms[@]} RPM(s)" \
    || kt_warn "no RPMs -- KUnit and kselftest will be skipped on the target"
if [ "$WITH_LTP" = 1 ]; then
    [ ${#ltps[@]} -gt 0 ] && kt_ok "LTP: $(basename "${ltps[0]}") ($(du -h "${ltps[0]}" | cut -f1))" \
        || kt_warn "no LTP tarball -- run build-ltp.sh first"
fi

# A run's results belong to the machine that produced them, and downloads/ is
# build scratch.  Neither should travel.
EXCLUDES=(--exclude=results --exclude=downloads --exclude='*.pyc'
          --exclude=__pycache__ --exclude='.git')
[ "$WITH_LTP" = 0 ] && EXCLUDES+=(--exclude='bundle/ltp-*.tar.gz')

# Record what this bundle is, so the target can tell one from another.
{
    printf 'built: %s\n'        "$(date '+%Y-%m-%d %H:%M:%S %Z')"
    printf 'built_on: %s\n'     "$(kt_osrel)"
    printf 'built_kernel: %s\n' "$(kt_kver)"
    printf 'arch: %s\n'         "$ARCH"
    printf 'ltp_version: %s\n'  "$(cat "$KT_ROOT/bundle/ltp-version.txt" 2>/dev/null || echo none)"
    for r in "${rpms[@]:-}"; do
        [ -n "$r" ] && printf 'rpm: %s\n' "$(basename "$r")"
    done
    for l in "${ltps[@]:-}"; do
        [ -n "$l" ] && [ "$WITH_LTP" = 1 ] && printf 'ltp: %s\n' "$(basename "$l")"
    done
} > "$KT_ROOT/BUNDLE-INFO.txt"

kt_head "Packing"
mkdir -p "$OUT"
( cd "$(dirname "$KT_ROOT")" && tar czf "$TARBALL" "${EXCLUDES[@]}" "$(basename "$KT_ROOT")" ) \
    || kt_die "packing failed"
( cd "$OUT" && sha256sum "$(basename "$TARBALL")" > "$(basename "$TARBALL").sha256" )

kt_ok "$TARBALL  ($(du -h "$TARBALL" | cut -f1))"
cat <<EOF

  Carry $(basename "$TARBALL") and its .sha256 to the offline machine, then:

      sha256sum -c $(basename "$TARBALL").sha256
      tar xzf $(basename "$TARBALL")
      cd $(basename "$KT_ROOT")
      ./install-offline.sh
      ./test-kernel.sh --profile smoke

EOF
