#!/bin/bash
# The main incremental 'continue where last left off' script.
# All the precursor stuff is in the subdirectories.

projectstring=main

is_main_script=1

if [ -e /var/run/reboot-required ]
then ls -l /var/run/reboot-required
fi

. common.sh

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


cuda_package=cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
cuda_key=7fa2af80.pub
nvcc_packages="cuda-toolkit-10-0 cuda-drivers"
# cuda-drivers a little overkill but gets libcuda.so
# cuda-toolkit creates the symlink /usr/local/cuda -> cuda-8.0

if [ -z "$1" ]
then echo "need snapshot to start" 1>&2
    exit 1
fi
if ! [ -r "$1" ]
then echo cannot read start tarball $1
     exit 1
fi
dir_start=$(dirname $1)
start_tarball=$(basename $1)

perl -we 'die unless @ARGV==2;$l=length$ARGV[0];$b=substr($ARGV[1],0,$l);die unless $ARGV[0]eq$b' ghc-$ghc_version-$stackage_version-$attempt_suffix $start_tarball

# tar of tar
inlocal mkdir $udir/start
tar cf - -C $dir_start -h $start_tarball | inlocal tar xf - -C $udir/start

# copy the packages.tar.lz file
packagestarlz=packages.tar.lz
if [ ${2-} ]
then tar cf - -C $(dirname $2) -h $(basename $2) | inlocal tar xf - -C $udir/start
if [ $(basename $2) != $packagestarlz ]
then inlocal mv $udir/start/$(basename $2) $udir/start/$packagestarlz
fi
fi

packages_install $(awk '/^#/{next}{print$1}' <<EOF
gnupg - for apt-key add
EOF
)

# put this early because failure seen:
# gpg: can't connect to the agent: IPC connect call failed

incontainer apt-key add - < $sources/$cuda_key

packages_install $(awk '/^#/{next}{print$1}' <<EOF
libgmp-dev
libdw-dev - ghc needs at least the dev
#dnsutils nslookup for this older cabal
gcc for many things
libc6-dev - need crti crtn -lm  -lc
plzip - we will be making and reading snapshots
wget - get stackage cabal.config
curl - cabal v2-update may be more efficient with this
time - especially for the idiom "nice time"
pkg-config - cabal-install uses this
apt-file - for the pkg-config file search
g++ - farmhash
libasound2-dev - PortMidi (but maybe we want liboss4-salsa-asound2)
zlib1g-dev - adler32 zlib and others
libbz2-dev - bzlib
#libcurl4-openssl-dev - curl.  gnutls and nss variants also available.  i vaguely recall some didn't work
libcurl4-gnutls-dev - curl.  gnutls and nss variants also available.  i vaguely recall some didn't work.  this one is compatible with libraptor2-dev
libgd-dev - gd
libpng-dev - gd
libjpeg-turbo8-dev - gd. also libjpeg62-dev available, and libjpeg8-dev and libjpeg9-dev
libfontconfig1-dev - gd
libfreetype6-dev - gd
libexpat1-dev - gd
libmpfr-dev - hmpfr
libgeoip-dev - hs-GeoIP
libtinfo-dev - ghc-parser
make - hlibsass
libmysqlclient-dev - mysql
libpq-dev - postgresql-libpq, PostgreSQL
libsndfile1-dev - hsndfile
liblmdb-dev - lmdb
liblzma-dev - lzma
libnfc-dev - nfc
libssl-dev - HsOpenSSL
libgl1-mesa-dev - OpenGLRaw (lots of other chices here)
libzip-dev - bindings-libzip
libblas-dev - blas-hs (other choices available, e.g libatlas-base-dev)
libmarkdown2-dev - discount
libexpat1-dev - hexpat
libudev-dev - hidapi
libadns1-dev - hsdns
libtagc0-dev - htaglib
libicu-dev - text-icu
libx11-dev - GLFW
libxi-dev - GLFW
libxrandr-dev - GLFW
libxxf86vm-dev - GLFW
libxcursor-dev - GLFW
libxinerama-dev - GLFW
libflac-dev - flac
liblapack-dev - hmatrix , other choices libatlas-base-dev
libmp3lame-dev - lame
libpcre3-dev - regrex-pcre
libfftw3-dev - vector-fftw
libcairo2-dev - Chart-cairo
r-base-core - H, inline-R
libfftw3-dev - JuicyPixels-scale-dct, fft
libxft-dev - X11-xft
libxau-dev - Xauth
libffi-dev - libffi and others
libxml2-dev - credential-store
libfreenect-dev - freenect
libatk1.0-dev - gi-atk
libglib2.0-dev - gi-glib
libgirepository1.0-dev - haskell-gi
libhidapi-dev - hid
libzmq3-dev (others libxs-compat-libzmq-dev libzmq-dev (too old) libzmq3-dev) - zeromq4-haskell
liboath-dev - liboath-hs
libsdl2-dev - sdl2 (ubuntu 14.04 2.0.2 is too old, need 2.0.4)
libsdl2-mixer-dev - sdl2-mixer
libsox-dev - soxlib
libpango1.0-dev - pango
libusb-1.0-0-dev - freenect
libgsl0-dev - hmatrix-gsl
libleveldb-dev - leveldb-haskell
libimlib2-dev - Imlib
libopenal-dev - OpenAL
ruby-dev - hruby (might need to set --rubyversion --rubylib --rubyinc -- YES it does (TODO) ;  also ruby-1.9.1-dev ruby-2.0-dev)
libruby - suprisingly ruby2.0-dev does not depend on this, and installs a broken symlink
libjack-jackd2-dev - jack (also available libjack-dev libjack-jackd2-dev)
default-jdk - jni (many many other choices choices), jvm.  the -jdk package provides jni.h, the 2 redirects later jre-headless package provides libjvm; depends default-jre , which provides the symlink /usr/lib/jvm/default-java
libncurses5-dev - wl-pprint-terminfo (this might not work because it also looks for cursed.h which is not in the package)
ninja-build - direct-rocksdb (still need to fix tar executable bit problem)
libalut-dev - ALUT
gradle - jvm-batching (pulls in a lot of things)
# moving on to morepack/00
libgtkglext1-dev - gtkglext
libglade2-dev - glade
libaosd-dev - aosd
libgtksourceview2.0-dev - gtksourceview2
libbabl-dev - babl
# gone libesd0-dev - bindings-EsounD
libaprutil1-dev - bindings-apt-util
libapr1-dev - bindings-apr
libaudiofile-dev - bindings-audiofile
libdirectfb-dev - bindings-directfb
libfann-dev - bindings-fann
libfluidsynth-dev - bindings-fluidsynth
libgts-dev - bindings-gts
libcddb2-dev - bindings-libcddb
libftdi-dev - bindings-libftdi
librrd-dev - bindings-librrd
#nettle-dev - nettle # need to figure out how to use the self-built one.  somehow it is installed in my manual test environment
portaudio19-dev - bindings-portaudio
libsane-dev - bindings-sane
libsqlite3-dev - bindings-sqlite3, HDBC-sqlite3
libgtk2.0-dev - bitspeak gtk-serialized-event
libogre-1.9-dev - hogre (also available libogre-1.8-dev)
libqt4-dev - clocked
libclutter-1.0-dev - clutterhs
libpython3-dev - cpython
libgraphicsmagick1-dev - hsmagick
libwebkitgtk-3.0-dev - webkitgtk3, webkitgtk3-javascriptcore
libdpkg-dev - dpkg
libdvdread-dev - dvdread
libenchant-dev - enchant
libavutil-dev - ffmpeg-light
libgconf2-dev - gconf
libdbusmenu-glib-dev - gi-dbusmenu
libdbusmenu-gtk3-dev - gi-dbusmenugtk3
libgtk-3-dev - gi-gdk, others
libgstreamer1.0-dev - gi-gst, others
libgstreamer-plugins-base1.0-dev - gi-gstaudio, others
libgtksourceview-3.0-dev - gi-gtksource
libnotify-dev - gi-notify
libpoppler-glib-dev - gi-poppler
libsecret-1-dev - gi-secret
libsoup2.4-dev - gi-soup
libgnome-keyring-dev - gnome-keyring
libgnomevfs2-dev - gnomevfs
libgsasl7-dev - gsasl
# gone libgstreamer0.10-dev - gstreamer
libgtkimageview-dev - gtkimageview
libappindicator-dev - happindicator
libappindicator3-dev - happindicator3
librsvg2-dev - svgcairo
libbladerf-dev - hlibBladeRf
libopencc-dev - hopencc
qtbase5-dev - hsqml
libqt4-dev - hsqml
libmagickcore-dev - imagemagick (also available graphicsmagick-libmagick-dev-compat)
coinor-libipopt-dev - ipopt-hs
libaugeas-dev - augeas (at least newest augeas requires this, though singles might prefer an older version)
python-dev - json-python, pyffi, pyfi
libglew-dev - ktx
libblkid-dev - linux-blkid
libkmod-dev - linux-kmod
libwebkitgtk-dev - webkit, webkit-javascriptcore
libfdk-aac-dev - mediabus-fdk-aac
libnm-glib-dev - nm
libopencv-dev - opencv
libraptor2-dev - redland
librubberband-dev - rubberband
libsoup-gnome2.4-dev - spike
libvte-dev - vte
libvte-2.91-dev - gi-vte
libqtwebkit-dev - wai-handler-webkit
libxfconf-0-dev - xfconf
# gone libxine-dev - xine (needs <2 so rejecting libxine2-dev)
# singles in morepack/00 discovered while compiling
libdb-dev - (also available: 5.1, 5.3) BerkeleyDB, berkeleydb
libdevil-dev - Codec-Image-DevIL
libfreetype6-dev - FreeTypeGL
unixodbc-dev - HDBC-odbc
libfuse-dev - HFuse
libode-dev - HODE
libjudy-dev - HsJudy
libperl-dev - HsPerl5
libkyotocabinet-dev - KyotoCabinet
libsasl2-dev - LDAP
libsnmp-dev - NetSNM
libcmph-dev - PerfectHash
libsdl1.2-dev - SDL
libsfml-dev - SFML
libcsfml-dev - SFML
libtheora-dev - Theora
libxmmsclient-glib-dev - XMMS
libalure-dev - alure
libdc1394-22-dev - bindings-dc1394
libgpgme11-dev - bindings-gpgme
libhamlib-dev - bindings-hamlib
libg15-dev - bindings-libg15
libv4l-dev - bindings-libv4l2
lxc-dev - bindings-lxc
libpotrace-dev - bindings-potrace
supercollider-dev - bindings-sc3
libmosquitto-dev - c-mosquitto
libcal3d12-dev - cal3d
libcrack2-dev - crack
libctemplate-dev - ctemplate
gridengine-drmaa-dev - drmaa (slurm, gridengine, pbs all available, google seems to very slightly think slurm is the most alive), but as of 18.04 gridengine has drmaa in its name
libcfitsio-dev - fitsio
libgenders0-dev - genders
libftgl-dev - FTGL
libmono-2.0-dev - Salsa
libgeos-dev - geos
libigraph0-dev - haskell-igraph
libopenmpi-dev - haskell-mpi (lam4-dev (does not work: missing MPI_ERR_ACCESS), libopenmpi, libmpich-dev all available, need include and probably library set)
libqrencode-dev - haskell-qrencode
libaspell-dev - haspell
libcwiid-dev - hcwiid (libbluetooth-dev also needed, but pulled as a dependecy by libcwiid)
libhdf5-dev - hdf5-lite (maybe also bindings-hdf5) (also available mpich2 and openmpi versions)
#last = heist-async
autoconf - fltkhs
libev-dev - hlibev
libfam-dev - hlibfam (also available: libgamin-dev)
libglpk-dev - hmatrix-glpk
libmagickwand-dev - hopfield
libpapi-dev - hpapi
libfltk1.3-dev - hs-fltk (both 1.1 and 1.3 are available; source README recommends 1.1 but we try newer anyway)
libsvm-dev - hslibsvm
libseccomp-dev - hsseccomp
libzephyr-dev - hszephyr
librdkafka-dev - hw-kafka-client
libzookeeper-mt-dev - hzk
libiw-dev - iwlib
liblapacke-dev - jalla
libatlas-base-dev - jalla
libarchive-dev - libarchive-conduit
#expect-dev - libexpect funny 18.04 does not complain when installing this purely virtual package
tcl-dev - libexpect
# gone libsystemd-daemon-dev - libsystemd-daemon (maybe libsystemd-dev?)
libxml2-dev - libxml
libmagic-dev - magic
libmpg123-dev - mpg123-bindings
libncursesw5-dev - ncurses
libnlopt-dev - nlopt-haskell
libnotmuch-dev - notmuch-haskell
libpam0g-dev - pam
libpcap0.8-dev - pcap
# gone libphash0-dev - phash
# gone libpoker-eval-dev - poker-eval
libacl1-dev - posix-acl
libproj-dev - proj4-hs-bindings
libqd-dev - qd
libqhull-dev - qhull-simple
librados-dev - rados-haskell
libtre-dev - regex-tre
librtlsdr-dev - rtlsdr
libssh2-1-dev - simplessh
libsnappy-dev - snappy
libstatgrab-dev - statgrab
libtokyocabinet-dev - tokyocabinet-haskell
libtokyotyrant-dev - tokyotyrant-haskell
libavdevice-dev - ffmpeg-light
# gone libgstreamer-plugins-base0.10-dev - gstreamer
qtdeclarative5-dev - hsqml
librdf0-dev - redland
automake - secp256k1 .  lets hope this provides bin/aclocal
libfcgi-dev - wai-handler-fastcgi
libxosd-dev - xosd
libxtst-dev - xtest
# this completes 00a and 00b
libsmpeg-dev - SDL-mpeg
libsamplerate0-dev - conduit-audio-samplerate
libobjc-4.8-dev - luka (4.7 also available)
libsdl-gfx1.2-dev - SDL-gfx
libsdl-image1.2-dev  - SDL-image (this is a guess, based on names, not looking deeply in config.log)
libsdl-mixer1.2-dev - SDL-mixer (this is a guess, based on names, not looking deeply in config.log)
libsdl-ttf2.0-dev - SDL-ttf (curiously, the only 2.0 available in Ubuntu 14.04)
git - git-repair
mpg321 - hmp3 (mpg123 could also be used)
#completes morepack/00
libpfs-dev - HDRUtils
libsvn-dev - HsSVN
libopenblas-dev - LATS
binutils-dev - bindings-bfd (the newer binutils-2.26-dev is also available)
libstemmer-dev - bindings-libstemmer
libmpdec-dev - bindings-mpdecimal deka
libc++-dev - cplusplus-th
libavahi-compat-libdnssd-dev - dnssd
libedit-dev - editline (we probably don't want libeditline-dev)
libcsound64-dev - hCsound
libbladerf-dev - hlibBladeRF
libnetcdf-dev - hnetcdf
libois-dev - hois
libxen-dev - hsXenCtrl
# libgfortran-4.8-dev - ipopt-hs move on to newest
libconfig-dev - libconfig
libkqueue-dev - kqueue
picosat - ll-picosat
libmecab-dev - mecab
libmtp-dev - mtp
# last - phraskell
libhunspell-dev - hunspell-hs
libxerces-c-dev - BerkeleyDBXML . this is v3; older v2 is available at libxerces-c2-dev. BerkeleyDBXML also wants dbxml which doesnt exist, so this is a bit pointless
libxqilla-dev - BerkeleyDBXML but pointless see note above
# clang-3.9 - cplusplus-th (check that llvm-3.9 gets pulled in) (now we assume 6.0 works)
# llvm-3.9 - it wasn't pulled in (now we assume 6.0 works)
golang-1.10-go - oden-go-packages . not sure this will work as the go binary gets installed in /usr/lib/go-1.6/bin/go.  for 18.04, we just chose the highest version
libgdbm-dev - JunkDB-driver-gdbm
gobjc - jsaddle-wkwebview
libwnck-3-dev - gi-wnck
# new additions for 18.04 , LTS 13
protobuf-compiler - proto-lens-protoc
llvm-6.0 - we assume something is going to want this
clang-6.0 - we assume something is going to want this (used by cplusplus-th, including a symlink created in buildtools-more)
libjavascriptcoregtk-4.0-dev - gi-javascriptcore
libsodium-dev - saltine
libsdl2-gfx-dev - sdl2-gfx
libsdl2-image-dev - sdl2-image
libsdl2-ttf-dev - sdl2-ttf
libsecp256k1-dev - secp256k1-haskell
libi2c-dev - h2c (not sure what file it actually needs) (I'm thinking there's a bug in the code where it needs to include linux/i2c.h and not i2c-dev.h)
libre2-dev - re2
librocksdb-dev - rocksdb-haskell
freetds-dev - odbc
cmake - direct-rocksdb
# more-packages
libarpack2-dev - arpack
libgit2-glib-1.0-dev - bdcs-api gi-ggit
libostree-dev - bdcs gi-ostree
libgegl-dev - gegl
libwebkit2gtk-4.0-dev - gi-webkit2 gi-webkit2webextension webkit2gtk3-javascriptcore
libgroonga-dev - haroonga
libbrotli-dev - hs-brotli
libsystemd-dev - libsystemd-journal
libtelnet-dev - libtelnet
libxine2-dev - xine , but see "gone" above.  we put this in to quell the install suggestion
llvm-7 - llvm-hs (lts 13.x)
llvm-8 - llvm-hs (lts 14.x)
ocl-icd-opencl-dev - OpenCL hopencl
libopenni2-dev - honi
libcapstone-dev - hapstone
libclang-6.0-dev - clang-pure # need 6.0 not 7 because the search for llvm-config does decimaled versions only (which is silly)
libclang-7-dev - clang-pure # let's leave this in there in case it manages to work someday
libcodec2-dev - bindings-codec2
tcl-expect-dev - libexpect
libkrb5-dev - gssapi
liblbfgsb-dev - l-bfgs-b
libnanomsg-dev - nanomsg-haskell nanomsg
libzfslinux-dev - libzfs
libphonenumber-dev - phone-numbers
libprimesieve-dev - primesieve
libtalloc-dev - notmuch
libtraildb-dev - traildb
libz3-dev - z3
gringo - clingo
libc++abi-dev - cplusplus-th Note there is a bug in cabal detecting the correct missing library.  it thinks c++ is missing though it's actually c++abi
libfoma-dev - foma
libsundials-dev - hmatrix-sundials
libb2-dev - hs-blake2
libgfortran-8-dev - ipopt-hs ubuntu 1804 is based primarily on gcc 7 but let's do newest because it is available
libtcod-dev - tcod-haskell
libvulkan-dev - vulkan
android-liblog-dev - android-activity also available is android-headers-{19..23}
libreadline-dev - readline
llvm-7-dev - llvm-ht (might need to adjust include flags)
doctest - language-elm
libwxgtk-media3.0-dev - wxc
man-db - fltkhs
libqdbm-dev - HsHyperEstraier
libbullet-dev - bullet
libuv1-dev - stdio
libvirt-dev - libvirt-hs
libantlr3c-dev - antlrc
libassimp-dev - assimp
swi-prolog-nox - hswip
libenet-dev - HGamer3D-Enet-Binding
moreutils - to have ts available for the rn function
libgraphene-1.0-dev - graphene-gobject
libpcre2-dev - libpcre2
libfplll-dev - fplll
libsingular4-dev - singular-factory
libgrib-api-dev - hgrib
libarrayfire-unified-dev - arrayfire
libgrpc-dev - grpc-haskell-core
libmodbus-dev - libmodbus
libzbar-dev - zbar
llvm-8-dev - llvm-hs
coop-computing-tools-dev - bindings-cctools
libflint-dev - singular-factory
libntl-dev - singular-factory
EOF
)

# tail -f ~/logs/main-on-201804-23d16h39m12-0400.log | perl /mit/ghc/src/random/explain-errors.pl| tee -a logs/explain-errors02.log

tar cf - -C $sources $cuda_package | inlocal tar xf - -C /tmp
incontainer dpkg -i /tmp/$cuda_package

incontainer patch -d /etc/apt/sources.list.d -p1 <<EOF
diff -Naur old/cuda.list new/cuda.list
--- old/cuda.list	2017-02-06 15:39:34.000000000 -0500
+++ new/cuda.list	2018-06-24 04:46:02.881553759 -0400
@@ -1 +1 @@
-deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /
+deb http://coreblue.bluespec.com:9999/cuda1804 /
EOF
incontainer apt-get update -q
#causes a whole lot of things -- desktop environment -- to get installed
if [ -z ${skip_cuda+skip_cuda} ]
then packages_install $nvcc_packages
fi
incontainer apt-file update

incontainer ip link set eth0 down
#convenience function
incontainer ln -s $udir/do-with-stack/start-interactive.sh /qqq

untar_built buildtools-more $buildtoolsmore_version

#plzip -dc $tarballs_done/nettle-$nettle_version.tar.lz | inlocal tar xf - -C $afspath
#inlocal ln -s $nettle_version $afspath/nettle/current

untar_built cabal2 $cabal2_version

untar_built buildtools-ghc $buildtoolsghc_version

#if [ -z ${skip_llvm+skip_llvm} ]
#    then
#    plzip -dc $tarballs_done/llvm5-$llvm5_version.tar.lz | inlocal tar xf - -C $afspath
#    inlocal ln -s $llvm5_version $afspath/llvm5/current
#fi

inlocal mkdir -p $udir/do-with-stack
cat <<EOF | inlocal tee -a $udir/vars.sh
ATHENA_SYS=$ATHENA_SYS
ghc_version=$ghc_version
stackage_version=$stackage_version
attempt_suffix=$attempt_suffix
skip_user=${skip_user-0}
impatient=${impatient-}
git_describe="$(git describe --always)"
default_profiling=$default_profiling
hackagepoint=$hackagepoint
ultimatedestination=$ultimatedestination
EOF


if [ -z ${skip_snapshot+skip_snapshot} ]
then if [ $container = ghcbuild-on-lxc1 ]
        # this is the one on btrfs so supports -s
    then lxc-stop -n $container
        #new name for the rest of the build
        container=ghcbuild-continue
        lxc-clone -s ghcbuild ghcbuild-continue
        #lxc-clone does not exist on lxc2, need to investigate further
        # todo: answer is lxc-copy
        lxc-start -n $container -o ~/logs/lxc-$projectstring-$datestring-restart.log -d
        sleep 5
    fi
fi
bash transfer-do-with-stack.sh $container

inlocal bash $udir/do-with-stack/user.sh

#copy out the latest snapshot tarball?

# do this test to avoid the container stopping when we manually kill the user script.
if [ -e $cdir_container$udir/finished ]
then lxc-stop -n $container
fi

date
true SUGGESTION
echo sudo time btrfs balance start --full-balance .
echo ""
true REMEMBER TO COPY tarballs
true $0 all done
