#!/bin/bash
# ubuntu trusty cabal cannot bootstrap installing latest cabal (failed to parse output of 'ghc-pkg dump')
set -x
set -e
set -o pipefail
set -u
datestring=$(date '+on-%Y%m%d%H-%M%S%z')

exec >& >(ts '%.s: ' > ~/logs/cabal1-$datestring.log)

date

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

container=ghcbuild
inner=iuser
locker=/afs/sipb.mit.edu/project/ghc/src

#this container location is for mkc
cdir=/scratch/kenta/kenta2

lxc-create -t download -n $container --dir $cdir/$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/random sources.list | $incontainer tar xf - -C /etc/apt
$incontainer apt-get update -q

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

udir=/home/$inner/i
$inlocal mkdir $udir
tar cf - user.sh | $inlocal tar xf - -C $udir

$incontainer apt-get install -q --no-install-recommends -y ghc cabal-install zlib1g-dev

nice time $inlocal bash $udir/user.sh

tar cf ~/tarballs/cabal1.tar.lz -C $cdir/$container/home/$inner -I 'plzip --best' cabal1

date
true $0 all done
