#!/bin/bash -xe

JENKINS_URL='https://build.hpdd.intel.com/'
BRANCH='ppc'

# it's tempting to use $JENKINS_HOME instead of this WORKSPACE_TOP but experience
# shows that they are not always the same thing
WORKSPACE_TOP="$(pwd)/../"
# we need to traverse up two directories for every axis
if [ -n "${platform:-}" ]; then
    WORKSPACE_TOP="${WORKSPACE_TOP}../../"
else
    if [ -n "${distro:-}" ]; then
        WORKSPACE_TOP="${WORKSPACE_TOP}../../"
    fi
    if [ -n "${arch:-}" ]; then
        WORKSPACE_TOP="${WORKSPACE_TOP}../../"
    fi
fi
if [ -n "${build_type:-}" ]; then
    WORKSPACE_TOP="${WORKSPACE_TOP}../../"
fi

# how do we get nice generic rpm names?
HUDSON_URL=http://build.lab.whamcloud.com:8080

# v1.41.10-lustre builds against 1.8.5
if [ "$GIT_BRANCH" = "v1.41.10-lustre" ]; then
    PROJECT=lustre-185
elif [ "$GIT_BRANCH" = "v1.41.12-lustre" ]; then
    # only v1.41.12-lustre is not required to build with 2.1
    BRANCH="b1_8"
else
    if [ "$distro" == "el5" ]; then
        BRANCH="b2_3"
    elif [ "$distro" == "sles11" ]; then
        BRANCH="b2_5"
    else
        BRANCH="master"
	BRANCH='ppc'
    fi
fi
distro='el6'
arch='ppc64'

PROJECT=${PROJECT:-lustre-${BRANCH}/build_type=client,distro=$distro,arch=$arch,ib_stack=inkernel}
echo "JENKINS_URL/job/$PROJECT/lastSuccessfulBuild/api/python?depth=0"
## https://build.hpdd.intel.com/job/lustre-master/arch=x86_64,build_type=client,distro=el6,ib_stack=inkernel/lastSuccessfulBuild/

    ARTIFACTS=$(python <<EOF
import urllib2

url="$JENKINS_URL/job/$PROJECT/lastSuccessfulBuild/api/python?depth=0"
response = urllib2.urlopen(url)
thedict = eval(response.read())

for artifact in thedict['artifacts']:
    print artifact['fileName']
EOF
)

for ARTIFACT in ${ARTIFACTS[*]}; do
    case "$ARTIFACT" in
        lustre-client-[0-9]*.$arch.rpm) LUSTRE_RPM="$ARTIFACT" ;;
    esac
done

LUSTRE_VER=${LUSTRE_RPM#lustre-client-*}
LUSTRE_VER=${LUSTRE_VER%%-*}

# undo any previous changes (i.e. applied patches)
git reset --hard
git clean -dfx

[ -d "_topdir" ] && rm -fr _topdir
mkdir -p _topdir/{BUILD,RPMS,SPECS,SOURCES,SRPMS,TMPDIR}

if [ -r patches/series ]; then
    while read patch; do
        if [ ! -r "patches/$patch" ]; then
            echo "patches/$patch is missing or unreadable"
            exit 1
        fi
        if ! patch -p1 < "patches/$patch"; then
            echo "patches/$patch failed to apply"
            exit 1
        fi
    done < patches/series
fi

autoconf

if [ $arch = x86_64 -o $arch = ppc64 ]; then
    LIBDIR=lib64
else
    LIBDIR=lib
fi

pushd ..

rm -rf usr
rm -rf lustre-release
git clone git://git.whamcloud.com/fs/lustre-release
mkdir usr
mkdir usr/src
mkdir usr/src/lustre-$LUSTRE_VER

ln -s $(pwd)/lustre-release/libcfs usr/src/lustre-$LUSTRE_VER/libcfs
ln -s $(pwd)/lustre-release/lustre usr/src/lustre-$LUSTRE_VER/lustre

if [ ! -f "$LUSTRE_RPM" ]; then
    # going to have to get a new one, so remove old ones
    rm lustre-[0-9]* || true
    if ! curl -L -O "$JENKINS_URL/job/$PROJECT/lastSuccessfulBuild/artifact/artifacts/RPMS/$arch/$LUSTRE_RPM"; then
        echo "error trying to get latest lustre rpm"
        rm -f "$JENKINS_URL/job/$PROJECT/lastSuccessfulBuild/artifact/BUILD/RPMS/$arch/$LUSTRE_RPM"
        exit 1
    fi
fi

rpm2cpio < "$LUSTRE_RPM" | cpio -id ./usr/${LIBDIR}/liblustreapi.a
ln usr/${LIBDIR}/liblustreapi.a usr/src/lustre-$LUSTRE_VER/lustre/utils/

popd

./configure --with-lustre=$(pwd)/../usr/src/lustre-$LUSTRE_VER

echo "Building e2fsprogs version $ver"
export RPM_TOPDIR=$(pwd)/_topdir
export ENABLE_XTRACE="true"
# older branches need to be told where to find lustre
if ! grep "%configure --enable-elf-shlibs --enable-nls --with-lustre=@LUSTRE@" e2fsprogs.spec; then
    export EXTRA_CONFIG_FLAGS="--with-lustre=$(pwd)/../usr/src/lustre-$LUSTRE_VER"
fi

# don't include .git subdir in the tarball for the RPM
# XXX - this needs to be turned into a patch and pushed upstream
# don't include _topdir in the tarball for the RPM
ed contrib/build-rpm <<"EOF"
/^EXCLUDE=/s/"$/ --exclude .git --exclude _topdir"/
.
w
q
EOF

# Removce check from SPEC file
## sed 's/^\(make check\)/# \1/' e2fsprogs.spec > new.spec
## mv new.spec e2fsprogs.spec
## sed 's/^\(make .* check\)/# \1/' e2fsprogs-RHEL-6.spec > new.spec
## mv new.spec e2fsprogs-RHEL-6.spec
## echo "-- e2fsprogs-RHEL-6.spec"
## cat e2fsprogs-RHEL-6.spec
## echo "---------------------------"
make rpm

pushd _topdir/RPMS
createrepo --pretty .
popd
