---
name: "zcash-1.1.2"
enable_cache: true
distro: "debian"
suites:
- "jessie"
architectures:
- "amd64"
packages:
- "autoconf"
- "automake"
- "bsdmainutils"
- "binutils-gold"
- "ca-certificates"
- "faketime"
- "g++-multilib"
- "git-core"
- "libc6-dev"
- "libtool"
- "m4"
- "ncurses-dev"
- "pkg-config"
- "python"
- "unzip"
- "wget"
- "zlib1g-dev"
remotes:
- "url": "https://github.com/zcash/zcash.git"
  "dir": "zcash"
files: []
script: |
  WRAP_DIR=$HOME/wrapped
  HOSTS="x86_64-unknown-linux-gnu"
  CONFIGFLAGS="--enable-glibc-back-compat --enable-reduce-exports --disable-bench --enable-hardening"
  MAKEOPTS="V=1"
  FAKETIME_HOST_PROGS=""
  FAKETIME_PROGS="date ar ranlib nm strip objcopy"
  HOST_CFLAGS="-fwrapv -fno-strict-aliasing -Werror -g"
  HOST_CXXFLAGS="-fwrapv -fno-strict-aliasing -Werror -g"
  HOST_LDFLAGS=-static-libstdc++

  export QT_RCC_TEST=0
  export GZIP="-9n"
  export TAR_OPTIONS="--mtime="$REFERENCE_DATE\\\ $REFERENCE_TIME""
  export TZ="UTC"
  export BUILD_DIR=`pwd`
  mkdir -p ${WRAP_DIR}
  if test -n "$GBUILD_CACHE_ENABLED"; then
    export SOURCES_PATH=${GBUILD_COMMON_CACHE}
    export BASE_CACHE=${GBUILD_PACKAGE_CACHE}
    mkdir -p ${BASE_CACHE} ${SOURCES_PATH}
  fi

  function create_global_faketime_wrappers {
  for prog in ${FAKETIME_PROGS}; do
    echo '#!/bin/bash' > ${WRAP_DIR}/${prog}
    echo "REAL=\`which -a ${prog} | grep -v ${WRAP_DIR}/${prog} | head -1\`" >> ${WRAP_DIR}/${prog}
    echo 'export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1' >> ${WRAP_DIR}/${prog}
    echo "export FAKETIME=\"$1\"" >> ${WRAP_DIR}/${prog}
    echo "\$REAL \$@" >> $WRAP_DIR/${prog}
    chmod +x ${WRAP_DIR}/${prog}
  done
  }

  function create_per-host_faketime_wrappers {
  for i in $HOSTS; do
    for prog in ${FAKETIME_HOST_PROGS}; do
        echo '#!/bin/bash' > ${WRAP_DIR}/${i}-${prog}
        echo "REAL=\`which -a ${i}-${prog} | grep -v ${WRAP_DIR}/${i}-${prog} | head -1\`" >> ${WRAP_DIR}/${i}-${prog}
        echo 'export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1' >> ${WRAP_DIR}/${i}-${prog}
        echo "export FAKETIME=\"$1\"" >> ${WRAP_DIR}/${i}-${prog}
        echo "\$REAL \$@" >> $WRAP_DIR/${i}-${prog}
        chmod +x ${WRAP_DIR}/${i}-${prog}
    done
  done
  }

  export PATH=${WRAP_DIR}:${PATH}

  # Faketime for depends so intermediate results are comparable
  create_global_faketime_wrappers "2000-01-01 12:00:00"
  create_per-host_faketime_wrappers "2000-01-01 12:00:00"

  cd zcash
  BASEPREFIX=`pwd`/depends
  # Build dependencies for each host
  for i in $HOSTS; do
    NO_PROTON="x" make ${MAKEOPTS} -C ${BASEPREFIX} HOST="${i}"
  done

  # Faketime for binaries
  create_global_faketime_wrappers "${REFERENCE_DATETIME}"
  create_per-host_faketime_wrappers "${REFERENCE_DATETIME}"

  # Create the release tarball using (arbitrarily) the first host
  ./autogen.sh
  CONFIG_SITE=${BASEPREFIX}/`echo "${HOSTS}" | awk '{print $1;}'`/share/config.site ./configure --prefix=/
  make dist
  SOURCEDIST=`echo zcash-*.tar.gz`
  DISTNAME=`echo ${SOURCEDIST} | sed 's/.tar.*//'`
  # Correct tar file order
  mkdir -p temp
  pushd temp
  tar xf ../$SOURCEDIST
  find zcash* | sort | tar --no-recursion --mode='u+rw,go+r-w,a+X' --owner=0 --group=0 -c -T - | gzip -9n > ../$SOURCEDIST
  popd

  ORIGPATH="$PATH"
  # Extract the release tarball into a dir for each host and build
  for i in ${HOSTS}; do
    export PATH=${BASEPREFIX}/${i}/native/bin:${ORIGPATH}
    mkdir -p distsrc-${i}
    cd distsrc-${i}
    INSTALLPATH=`pwd`/installed/${DISTNAME}
    mkdir -p ${INSTALLPATH}
    tar --strip-components=1 -xf ../$SOURCEDIST

    CONFIG_SITE=${BASEPREFIX}/${i}/share/config.site ./configure --prefix=/ --disable-ccache --disable-maintainer-mode --disable-dependency-tracking ${CONFIGFLAGS} CFLAGS="${HOST_CFLAGS}" CXXFLAGS="${HOST_CXXFLAGS}" LDFLAGS="${HOST_LDFLAGS}"
    make ${MAKEOPTS}
    make ${MAKEOPTS} -C src check-security
    make install DESTDIR=${INSTALLPATH}
    cd installed
    find . -name "lib*.la" -delete
    find . -name "lib*.a" -delete
    rm -rf ${DISTNAME}/lib/pkgconfig
    find ${DISTNAME}/bin -type f -executable -exec objcopy --only-keep-debug {} {}.dbg \; -exec strip -s {} \; -exec objcopy --add-gnu-debuglink={}.dbg {} \;
    # Commented out while we don't build any libraries
    #find ${DISTNAME}/lib -type f -exec objcopy --only-keep-debug {} {}.dbg \; -exec strip -s {} \; -exec objcopy --add-gnu-debuglink={}.dbg {} \;
    find ${DISTNAME} -not -name "*.dbg" | sort | tar --no-recursion --mode='u+rw,go+r-w,a+X' --owner=0 --group=0 -c -T - | gzip -9n > ${OUTDIR}/${DISTNAME}-${i}.tar.gz
    find ${DISTNAME} -name "*.dbg" | sort | tar --no-recursion --mode='u+rw,go+r-w,a+X' --owner=0 --group=0 -c -T - | gzip -9n > ${OUTDIR}/${DISTNAME}-${i}-debug.tar.gz
    cd ../../
    rm -rf distsrc-${i}
  done
  mkdir -p $OUTDIR/src
  mv $SOURCEDIST $OUTDIR/src
  mv ${OUTDIR}/${DISTNAME}-x86_64-*-debug.tar.gz ${OUTDIR}/${DISTNAME}-linux64-debug.tar.gz
  mv ${OUTDIR}/${DISTNAME}-x86_64-*.tar.gz ${OUTDIR}/${DISTNAME}-linux64.tar.gz
