#!/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

#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

#lets try the inria version, dl bz2 version even though xz exists
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
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 - -C $locker/src 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

$inlocal tar xzf - -C /tmp < ~/Public/g.tar.gz

tar cf - lxc02a.sh lxc02b.sh lxc02c.sh | $inlocal tar xf - -C /tmp

pushd $locker/src
tar cf - Makefile.def.patch | $inlocal tar xf - -C /tmp
popd

$incontainer apt-get install --no-install-recommends -y libmpc3 binutils libc6-dev xz-utils make linux-libc-dev texinfo autogen libc6-dev-i386 patch zlib1g-dev
# libc6-dev-i386 needed to build cross compiler ("multilib")
# linux-libc-dev needed for asm/errno.h
# texinfo for makeinfo
# autogen and patch needed to patch Makefile.def
# zlib1g-dev needed for --with-system-zlib

# gcc-multilib package accomplishes this, but has a million dependencies
$incontainer ln -s x86_64-linux-gnu/asm /usr/include

$inlocal tar xJf - -C /tmp < /mit/gcc-6plus/src/tarballs/gmp-$gmp.tar.xz
$inlocal tar xJf - -C /tmp < /mit/gcc-6plus/src/tarballs/mpfr-$mpfr.tar.xz
$inlocal tar xzf - -C /tmp < /mit/gcc-6plus/src/tarballs/mpc-$mpc.tar.gz
$inlocal tar xjf - -C /tmp < /mit/gcc-6plus/src/tarballs/isl-$isl.tar.bz2
$inlocal tar xjf - -C /tmp < /mit/gcc-6plus/src/tarballs/gcc-$gcc.tar.bz2

exit
nice -19 time $inlocal bash /tmp/lxc02a.sh
