#!/bin/bash

set -e
set -u

if [[ -d .git ]] && [[ -n "$(git status --porcelain)" ]]; then
	echo "'git status' needs to be clean for build-rpm.sh"
	exit -1
fi

dkms=0
ofed=auto
ofaparam=""
NOKABICHK=1

while [ "$#" != "0" ] ; do
    if [ "$1" = "--no-ofed" ] ; then
	ofed=
	shift
	continue
    fi
    if [ "$1" = "--dkms" ] ; then
	dkms=1
	shift
	continue
    fi
    if [ "$1" = "--kabichk" ] ; then
	NOKABICHK=0
	shift
	continue
    fi
    break
done

if [[ "${ofed}" == "auto" ]] ; then
    OFED_RELEASE=$(ofed_info -s 2>/dev/null | awk -F":" '{print $1}' | tr '-' '.')
    if [[ "$OFED_RELEASE" != "" ]] ; then
	OFED_RELEASE=${OFED_RELEASE}_
    fi
else
    OFED_RELEASE=
fi

if [[ "${OFED_RELEASE}" == "" ]] ; then
    ofaparam="OFA_DIR="
fi

GIT_VERSION=$(./git-version.sh HEAD)
if [[ -e .base-version ]] ; then
	BASE_GIT_VERSION="$(cat .base-version)"
else
	BASE_GIT_VERSION=${GIT_VERSION}
fi
PKG_VERSION=$(echo ${GIT_VERSION} | cut -c2- | tr - .)

if [[ "${OFED_RELEASE}" != "" ]] ; then
    if rpm -q kmod-mlnx-ofa_kernel > /dev/null 2>&1 ;then
	dkms=0
    elif rpm -q mlnx-ofa_kernel-modules > /dev/null 2>&1 ;then
	dkms=0
    elif rpm -q mlnx-ofa_kernel-dkms > /dev/null 2>&1 ;then
	dkms=1
    else
	echo "OFED setup type not detected"
	exit -1
    fi
else
    rpm -q dkms > /dev/null 2>&1 || dkms=0
fi
VERSION=${PKG_VERSION}
BUILD_AREA=.rpm-build

if [[ -e .git ]] ; then
	rm -rf ${BUILD_AREA}
	mkdir -p ${BUILD_AREA}
	git archive HEAD -o ${BUILD_AREA}/vastnfs_${PKG_VERSION}.orig.tar.gz
else
	tar -czf ../vastnfs_${PKG_VERSION}.orig.tar.gz .git-* *
	rm -rf ${BUILD_AREA}
	mkdir -p ${BUILD_AREA}
	mv ../vastnfs_${PKG_VERSION}.orig.tar.gz ${BUILD_AREA}
fi

cd ${BUILD_AREA}
mkdir source
cd source
tar -zxf ../vastnfs_${PKG_VERSION}.orig.tar.gz
echo '#define NFS_BUNDLE_VERSION "'${VERSION}'"' > build-info.h
VERSION_ID=v_$(echo ${VERSION} | cksum | awk -F' ' '{print $1}')
echo '#define NFS_BUNDLE_VERSION_ID '${VERSION_ID} >> build-info.h
echo '#define NFS_BUNDLE_GIT_VERSION "'${GIT_VERSION}'"' >> build-info.h
echo '#define NFS_BUNDLE_BASE_GIT_VERSION "'${BASE_GIT_VERSION}'"' >> build-info.h
sed -i 's/^PACKAGE_VERSION=".*/PACKAGE_VERSION="'${PKG_VERSION}'"/' dkms.conf
test -z "${VERSION}" || sed -i "1s/^/%{!?_version: %define _version ${VERSION}}\n/" vastnfs.spec
test -z "${OFED_RELEASE}" || sed -i "1s/^/%{!?_release: %define _release ${OFED_RELEASE}}\n/" vastnfs.spec
tar -czf ../vastnfs_${PKG_VERSION}.orig.tar.gz *

rm -rf ../../rpm-dist
mkdir ../../rpm-dist
if [[ "$dkms" == "1" ]] ; then
    # Remove nfsv2 on RHEL, and conditionally add nfs_localio if required
    if ! modinfo nfs_localio > /dev/null 2>&1 ;then
	sed -e '/BUILT_MODULE_NAME\[14\]=/d' -e '/BUILT_MODULE_LOCATION\[14\]=/d' -e '/DEST_MODULE_LOCATION\[14\]=/d' -i dkms.conf
    else
	sed -e 's:BUILT_MODULE_LOCATION\[14\]="./bundle/fs/nfs/":BUILT_MODULE_LOCATION\[14\]="./bundle/fs/nfs_common/":' \
				-e 's:BUILT_MODULE_NAME\[14\]="nfsv2":BUILT_MODULE_NAME\[14\]="nfs_localio":' -i dkms.conf
    fi
    make dkmsrpm ${ofaparam} RELEASE=${OFED_RELEASE} VERSION=${VERSION} NOKABICHK=${NOKABICHK}
    mv -f rpm-dist/noarch/vastnfs-*.rpm ../../rpm-dist/
else
    make srcrpm ${ofaparam} RELEASE=${OFED_RELEASE} VERSION=${VERSION} NOKABICHK=${NOKABICHK}
    make binrpm ${ofaparam} RELEASE=${OFED_RELEASE} VERSION=${VERSION} NOKABICHK=${NOKABICHK}

    mv -f rpm-dist/$(uname -m)/akmod-vastnfs-*.rpm ../../rpm-dist/ >/dev/null 2>&1 || true
    mv -f rpm-dist/$(uname -m)/vastnfs-*.rpm ../../rpm-dist/
fi
cd ../..
ls -l rpm-dist
