#!/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
locker=/afs/sipb.mit.edu/project/gcc-6plus/src


#xz
gmp=6.1.2

#xz
mpfr=3.1.5

#gz
mpc=1.0.3

# isl stands for Integer Set Library.  The version of fsf is older (0.16.1) than the one at inria (0.17.1), but we use the recommended fsf version.  There is no cryptographic signature.

#bz2
#isl=0.16.1
isl=0.18

#bz2
gcc=7.1.0

lxc-create -t download -n $container --dir /scratch/kenta/kenta2/$container -- -d ubuntu -r trusty -a amd64
lxc-start -n $container -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 - -C $locker sources.list | $incontainer tar xf - -C /etc/apt
$incontainer apt-get update

# gnat-4.8 does not install gnatgcc

udir=/tmp/gcc

$inlocal mkdir $udir

tar cf - user.sh | $inlocal tar xf - -C $udir

$incontainer apt-get install --no-install-recommends -y g++ libmpc-dev make zlib1g-dev gnat
# libc6-dev-i386 needed to build cross compiler ("multilib")
# linux-libc-dev needed for asm/errno.h (but is included in gcc-multilib)
# texinfo for makeinfo
# autogen and patch needed to patch Makefile.def
# zlib1g-dev needed for --with-system-zlib

xzcat $locker/tarballs/gmp-$gmp.tar.xz | $inlocal tar xf - -C $udir
xzcat $locker/tarballs/mpfr-$mpfr.tar.xz | $inlocal tar xf - -C $udir
$inlocal tar xzf - -C $udir < $locker/tarballs/mpc-$mpc.tar.gz
$inlocal tar xjf - -C $udir < $locker/tarballs/isl-$isl.tar.bz2
$inlocal tar xjf - -C $udir < $locker/tarballs/gcc-$gcc.tar.bz2


nice -19 time $inlocal bash $udir/user.sh
