#!/bin/bash source ~/.cienv if [ "$WORKSPACE" = "" ]; then echo 'WORKSPACE not set' exit 2 fi if [ "$RELEASE" = "" ]; then echo 'RELEASE not set' exit 2 fi if [ "$MANIFEST" = "" ]; then echo 'MANIFEST not set' exit 2 fi if [ "$PEGGED" = "" ]; then export BLD_NUM=0 else export BLD_NUM=$PEGGED fi error_exit() { echo "

Server build failed: $1

"
  exit 2
}

echo '

Building

'
> $WORKSPACE/versions.cfg
> $WORKSPACE/make.log

if [ ! -d ~/.reference ]; then
  mkdir ~/.reference
  cd ~/.reference
  repo init -u https://github.com/couchbase/manifest -b master -m $MANIFEST -g all --mirror 1>>$WORKSPACE/make.log 2>&1
  test $? -eq 0 || error_exit "Repo Init"
fi

test "$BLD_NUM" = "0" || echo "Using pegged server, $BLD_NUM build"
echo "Calculating base"
rm -rf ~/.manifests
git clone https://github.com/couchbase/build-team-manifests ~/.manifests 1>>$WORKSPACE/make.log 2>&1
cd ~/.manifests

test "$BLD_NUM" = "0" && BLD_NUM="`git log --oneline | grep "couchbase-server" | grep "$RELEASE-[0-9]" | head -1 | awk -F- '{print $3}'`"
SHA=`git log --oneline | grep "couchbase-server" | grep "$RELEASE-$BLD_NUM" | head -1 | awk '{print $1}'`
git branch thisrun $SHA 1>>$WORKSPACE/make.log 2>&1

echo "Updating mirror"
cd ~/.reference
repo sync --jobs=32 1>>$WORKSPACE/make.log 2>&1

echo "Basing run on $RELEASE-$BLD_NUM $SHA"
find $WORKSPACE -mindepth 1 -not -name '*.log' -exec rm -rf {} + 1>>$WORKSPACE/make.log 2>&1
mkdir -p $WORKSPACE 1>>$WORKSPACE/make.log 2>&1
cd $WORKSPACE
repo init -u ~/.manifests -b thisrun -m $MANIFEST -g all --reference=~/.reference 1>>$WORKSPACE/make.log 2>&1
test $? -eq 0 || error_exit "Repo Init at $SHA"

echo "Updating tree for run $TS"
cd $WORKSPACE
repo sync --jobs=32 1>>$WORKSPACE/make.log 2>&1
test $? -eq 0 || error_exit "Updating tree"
test $WORKSPACE/cbbuild && test $WORKSPACE/voltron || error_exit "Unable to find voltron"

cd $WORKSPACE
chmod a+w CMakeLists.txt

cd $WORKSPACE/goproj/src/github.com/couchbase/plasma
current="`git rev-parse HEAD`"
test -f ~/plasma.good || (echo $current > ~/plasma.good)
good="`cat ~/plasma.good`"
echo -n "plasma is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/goproj/src/github.com/couchbase/nitro
current="`git rev-parse HEAD`"
test -f ~/nitro.good || (echo $current > ~/nitro.good)
good="`cat ~/nitro.good`"
echo -n "nitro is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/goproj/src/github.com/couchbase/indexing
current="`git rev-parse HEAD`"
test -f ~/indexing.good || (echo $current > ~/indexing.good)
good="`cat ~/indexing.good`"
echo -n "indexing is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/goproj/src/github.com/couchbase/eventing
current="`git rev-parse HEAD`"
test -f ~/eventing.good || (echo $current > ~/eventing.good)
good="`cat ~/eventing.good`"
echo -n "eventing is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/goproj/src/github.com/couchbase/query
current="`git rev-parse HEAD`"
test -f ~/query.good || (echo $current > ~/query.good)
good="`cat ~/query.good`"
echo -n "query is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/goproj/src/github.com/couchbase/gometa
current="`git rev-parse HEAD`"
test -f ~/gometa.good || (echo $current > ~/gometa.good)
good="`cat ~/gometa.good`"
echo -n "gometa is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/ns_server
current="`git rev-parse HEAD`"
test -f ~/ns_server.good || (echo $current > ~/ns_server.good)
good="`cat ~/ns_server.good`"
echo -n "ns_server is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/couchdb
echo "Switching couchdb to $BRANCH"
git checkout $BRANCH 1>>$WORKSPACE/make.log 2>&1 || error_exit "git checkout $BRANCH"
git pull 1>>$WORKSPACE/make.log 2>&1 || error_exit "git pull"
current="`git rev-parse HEAD`"
test -f ~/couchdb.good || (echo $current > ~/couchdb.good)
good="`cat ~/couchdb.good`"
echo -n "couchdb is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

if [ "$GERRIT" != "" ]; then
  for patch_id in $GERRIT;
  do
    cd $WORKSPACE/goproj/src/github.com/couchbase/couchdb
    revspec=`git ls-remote -q http://review.couchbase.org/couchdb refs/changes/*/$patch_id/* | awk '{print $2}' | sort -n -t/ -k5 | tail -1`
    echo Pulling in uncommitted change "http://review.couchbase.org/couchdb $revspec"
    git fetch -q http://review.couchbase.org/couchdb $revspec && git cherry-pick FETCH_HEAD
  done
fi

cd $WORKSPACE/couchstore
current="`git rev-parse HEAD`"
test -f ~/couchstore.good || (echo $current > ~/couchstore.good)
good="`cat ~/couchstore.good`"
echo -n "couchstore is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/forestdb
current="`git rev-parse HEAD`"
test -f ~/forestdb.good || (echo $current > ~/forestdb.good)
good="`cat ~/forestdb.good`"
echo -n "forestdb is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/geocouch
current="`git rev-parse HEAD`"
test -f ~/geocouch.good || (echo $current > ~/geocouch.good)
good="`cat ~/geocouch.good`"
echo -n "geocouch is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/kv_engine
current="`git rev-parse HEAD`"
test -f ~/kv_engine.good || (echo $current > ~/kv_engine.good)
good="`cat ~/kv_engine.good`"
echo -n "kv_engine is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE/testrunner
current="`git rev-parse HEAD`"
test -f ~/testrunner.good || (echo $current > ~/testrunner.good)
good="`cat ~/testrunner.good`"
echo -n "testrunner is at ${current:0:7}, changes since last good build: "
if [ "$current" != "$good" ]; then
  echo
  git log --pretty='format: %h %s' $good..HEAD
  echo
else
  echo "none"
fi

cd $WORKSPACE
PRODUCT_VERSION=${RELEASE}-${BLD_NUM}-rel
rm -f *.rpm *.deb
rm -rf ~/rpmbuild
rm -rf $WORKSPACE/voltron/build/deb
rm -rf $WORKSPACE/install/*
find goproj godeps -name \*.a -print0 | xargs -0 rm -f

cd $WORKSPACE
mkdir -p build
cd build

echo "Building main product"
cmake -D CMAKE_INSTALL_PREFIX=$WORKSPACE/install \
      -D CMAKE_PREFIX_PATH=$WORKSPACE/install \
      -D CMAKE_BUILD_TYPE=RelWithDebInfo \
      -D PRODUCT_VERSION=${PRODUCT_VERSION} \
      -D BUILD_ENTERPRISE=TRUE \
      -D CB_DOWNLOAD_DEPS=1 \
      -D SNAPPY_OPTION=Disable \
      .. 1>>$WORKSPACE/make.log 2>&1
test $? -eq 0 || error_exit "Main build"

make -j8 install 1>>$WORKSPACE/make.log 2>&1
test $? -eq 0 || error_exit "Making Install"

cd $WORKSPACE
repo manifest -r > current.xml
repo manifest -r > manifest.xml

echo "Actual Versions:" >> $WORKSPACE/versions.cfg
cd $WORKSPACE
repo forall -c 'echo "$REPO_PROJECT `git log --oneline HEAD...HEAD^`"' 2>/dev/null 1>>$WORKSPACE/versions.cfg

chmod a+r /var/www/*