#!/bin/bash
set -x
set -e
set -o pipefail
set -u

#to destroy
# lxc-stop --name $container
# lxc-destroy --name $container

container=cbuild
inner=iuser

lxc-create -t download -n $container --dir /scratch/kenta2/$container -- -d ubuntu -r trusty -a amd64
rm -f ~/mylxc.log
lxc-start -n $container -o ~/mylxc.log -d
incontainer="lxc-attach -n $container --"
inlocal="$incontainer sudo -u $inner"
#setting UIDs to match the host does not work
$incontainer useradd -m $inner
#needed for networking to start
sleep 5
tar cf - sources.list | $incontainer tar xf - -C /etc/apt
$incontainer apt-get update

# gnat-4.8 does not install gnatgcc

if [ -n "${setuponly+x}" ]
then true exiting due to setuponly="$setuponly"
    exit 0
fi

time $incontainer apt-get build-dep -y --no-install-recommends gnat-4.6
# AFS kernel module does not install inside a container
$inlocal tar xjf - -C /tmp < /mit/gcc-6plus/src/tarballs/gcc-4.8.5.tar.bz2
tar cf - lxc1a.sh | $inlocal tar xf - -C /tmp
$inlocal bash /tmp/lxc1a.sh
$inlocal tar czf - -C /tmp g > ~/Public/g.tar.gz
