[buildout]
extends =
  ../../component/vm-img/debian.cfg
parts =
  debuild-rina-tools
  test-rina
download-cache = download-cache

[vm-run-base]
smp = 4
# estimate the of RAM needed depending on the number CPUs
mem = 2048
apt-sources =
apt-update =
  for x in ${:apt-sources}
  do echo deb [trusted=yes] file:`map $x` ./
  done |sudo sh -c "cat >>/etc/apt/sources.list
  apt-get update"

[debuild-rina-base]
<= vm-run-base
mount.rina = ${irati-stack:location}
cd-source =
  git clone -snc core.sparseCheckout=true /mnt/rina rina
  cd rina
  echo /${:component} > .git/info/sparse-checkout
  git reset --hard
  cd ${:component}
# following lines are for debchange
  export NAME=`git show -s --format=%cn`
  export EMAIL=`git show -s --format=%ce`
  dist=${:suite}
  [ "$dist" ] && dist=`lsb_release -sc`/$dist || dist=UNRELEASED
prepare =
build-deps =
  mk-build-deps -irs sudo -t 'apt-get -y'
build =
  dpkg-buildpackage -uc -b -jauto
finalize =
  df
  cd ..
  mv *.changes *.deb $PARTDIR
  cd $PARTDIR
  dpkg-scanpackages -m . /dev/null |gzip --best >Packages.gz
  r=`apt-ftparchive release .`
  echo "$r" > Release
command =
  ${:apt-update}
  ${:cd-source}
  ( ${:prepare}
    ${:build-deps}
    ${:build}
  ) 2>&1 |tee $PARTDIR/build.log
  ${:finalize}

[debuild-rina-kernel]
<= debuild-rina-base
component = linux
prepare =
  debian/dch-snapshot --force-distribution -D $dist
  sudo apt-get -y install kernel-wedge quilt python-six
  QUILT_PATCHES=$PWD/debian/patches QUILT_PC=.pc quilt push --quiltrc - -a -q --fuzz=0
  debian/rules debian/rules.gen || :
build =
  arch=`dpkg-architecture -qDEB_HOST_ARCH`
  fakeroot make -j${:smp} -f debian/rules.gen binary-arch_$${arch}_none binary-libc-dev_$${arch}
  dpkg-genchanges -b -UBinary -UDescription \
    >../linux_`dpkg-parsechangelog -S Version`_$${arch}.changes

[debuild-librina]
<= debuild-rina-base
component = librina
apt-sources = ${debuild-rina-kernel:location}
prepare =
  debian/rules DIST=$dist

[debuild-rinad]
<= debuild-librina
component = rinad
apt-sources = ${debuild-librina:location}

[rina-tools-deps]
apt-sources = ${debuild-librina:location} ${debuild-rinad:location}

[debuild-rina-tools]
<= debuild-librina
  rina-tools-deps
component = rina-tools
# check that the built packages install
finalize =
  sudo dpkg -i ../*.deb
  ${debuild-librina:finalize}

[test-rina]
<= vm-run-base
  rina-tools-deps
mount.download-cache = ${buildout:download-cache}
mount.rina = ${irati-stack:location}
mount.slapos = ${:_profile_base_location_}/../..
mount.slapos.package = ${slapos.package-repository:location}
commands = install test
install =
  ${:apt-update}
  sudo apt-get -y install librinad-dev rinad
  mkdir software
  cd software
  cat <<EOF >buildout.cfg
  [buildout]
  extends = /mnt/slapos/software/hellorina/software.cfg
  develop = /mnt/slapos
  download-cache = /mnt/download-cache
  [slapos-cookbook-develop]
  recipe =
  setup =
  [irati-stack]
  repository = /mnt/rina
  shared = true
  [versions]
  slapos.cookbook =
  EOF
  MAKEFLAGS=-j${:smp} python -S /mnt/buildout/bin/buildout

  arch=`dpkg-architecture -qDEB_HOST_ARCH`
  sudo sh -c "/mnt/slapos.package/playbook/roles/rina/gen-ipcm-conf
  systemctl enable ipcm-re6st
  dpkg -i `map ${debuild-rina-kernel:location}`/linux-image-*-$${arch}_*.deb"
  reboot
test = ( set -x
# TODO: more tests
  grep re6st/1// /sys/rina/ipcps/1/name
  ) 2>&1 |tee $PARTDIR/result