#!/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.
#
################################################################################

# Script to find and install binary RPMS
# Or build SRPM
# now that they are all tar'ed up

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

. $bin/sh/disttag

#

pre="/project/ci/build-products/"
post="/linux/rpm.tar.gz"
unlikely="(debug|kdump|xen|trace|vmi|domU)"

#

err  () { echo 1>&2 "$*";   }
log  () { err "$me: $*";     }
fail () { log "$*"; exit 1; }
try  () { "$@" || fail "ERROR: '$*' failed"; }
usage()	{ err "$me: [--tag=<tag>] [--install=<rpm>] [--extract=<rpm>]"; exit 1; }

cleanup() {
    local tmpdir="$1"

    if $do_cleanup; then
	echo "Removing '$tmpdir'"
	rm -Rf ${tmpdir}
    else
	find ${tmpdir} -type f
	echo "Output in '$tmpdir'. Please remove"
    fi
}

tag_set=false
do_install=false
in_sles=false
do_cleanup=true
rpm=""

# CLI processing
while [ $# -gt 0 ]; do
    case "$1" in
	--tag*)
	    tag_set=true
	    tag=${1#--tag=}
	    ;;
	--install=*)
	    do_install=true
	    rpm=${1#--install=}
	    ;;
	--extract=*)
	    do_cleanup=false
	    rpm=${1#--extract=}
	    ;;
	--*)
	    usage
            ;;
	*)
	    break
	    ;;
    esac
    shift
done
[ $# = 0 ] || usage

# Main
echo "Kernel here is `uname -r`"

# Show a choice of available tags
if ! $tag_set; then
    echo "Please select from:"
    for dir in `ls -1ct ${pre}*${post}`; do
	tag=${dir#$pre}
	tag=${tag%%$post}
	echo "  --tag=$tag"
    done
    exit 0
fi

tgz=${pre}${tag}${post}
[ -f ${tgz} ] || fail "File '$tgz' not found"

# CLI --install OR --extract
if [ -n "$rpm" ]; then
    tmpdir=`mktemp --tmpdir -d tmp.rpmfind.XXXXXXXX`
    trap "{ set +eu; cleanup $tmpdir; exit 255; }" EXIT
    cd ${tmpdir}

    echo "==> untar"
    tar xfvz ${tgz} ${rpm}
    if ${do_install}; then
	[ `whoami` == "root" ] || fail "Please rerun as root"
	echo "==> rpm -ivh ${rpm}"
	rpm -ivh ${rpm}
    fi
    echo "OK"
    exit 0
fi

# Munge the current kernel version to support matching
# Try to keep this looser rather than ignoring possible packages
distro=$(xilinx-efct_disttag)
case "$distro" in
    RHEL*) find="$1" ;;
    *)           find="." ;;
esac

arch=$(uname -m)

# Find the leading part of the kernel name
unamer=$(uname -r | sed -r 's/^((2\.)?[0-9]+\.[0-9]+).*/\1/')

echo "DEBUG: find '$find' arch '$arch' distro '$distro' unamer '$unamer'"

echo "======> Possibles (specify with --install or --extract):"
tar tfvz ${pre}${tag}${post} | egrep ${find} | grep ${arch} | grep ${unamer} \
    | cut -d ' ' -f 6 | egrep -v ${unlikely}

echo "=====> Less likely:"
tar tfvz ${pre}${tag}${post} | egrep ${find} | grep ${arch} | grep ${unamer} \
    | cut -d ' ' -f 6 | egrep ${unlikely}
