Bootstrap: docker
From: nvidia/cuda:8.0-cudnn5-devel-ubuntu16.04

%setup
	#Runs on host
	#The path to the image is $APPTAINER_ROOTFS

%post
	#Post setup script

	#Use bash as default shell
	echo "\n #Using bash as default shell \n" >> /environment
	echo 'SHELL=/bin/bash' >> /environment

	#Make environment file executable
	chmod +x /environment

	#Default mount paths
	mkdir /scratch /data /shared /fastdata

	#Nvidia Library mount paths
	mkdir /nvlib /nvbin

	#Add nvidia driver paths
	echo "\n #Nvidia driver paths \n" >> /environment
	echo 'export PATH="/nvbin:$PATH"' >> /environment
	echo 'export LD_LIBRARY_PATH="/nvlib:$LD_LIBRARY_PATH"' >> /environment


	#Add CUDA paths
	echo "\n #Cuda paths \n" >> /environment
	echo 'export CPATH="/usr/local/cuda/include:$CPATH"' >> /environment
	echo 'export PATH="/usr/local/cuda/bin:$PATH"' >> /environment
	echo 'export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"' >> /environment
	echo 'export CUDA_HOME="/usr/local/cuda"' >> /environment

  #Updating and getting required packages
  apt-get update
  apt-get install -y wget git cmake libreadline-dev

  #Creates a build directory
  BUILD_ROOT="/build"

  mkdir $BUILD_ROOT
  cd $BUILD_ROOT

  git clone https://github.com/xianyi/OpenBLAS.git openblas
  cd openblas
  make -j8
  make install

  #Back to build root
  cd $BUILD_ROOT

  #Download and install torch
  git clone https://github.com/torch/distro.git /usr/local/torch --recursive
  cd /usr/local/torch; bash install-deps;
  ./install.sh


	#Export torch paths
	echo "\n #Torch paths \n" >> /environment
	echo 'export LUA_PATH="/usr/local/torch/install/share/lua/5.1/?.lua"' >> /environment
	echo 'export LUA_PATH="/usr/local/torch/install/share/lua/5.1/?/init.lua;$LUA_PATH"' >> /environment
	echo 'export LUA_PATH="./?.lua;$LUA_PATH"' >> /environment
	echo 'export LUA_PATH="/usr/local/torch/install/share/luajit-2.1.0-beta1/?.lua;$LUA_PATH"' >> /environment
	echo 'export LUA_PATH="/usr/local/share/lua/5.1/?.lua;$LUA_PATH"' >> /environment
	echo 'export LUA_PATH="/usr/local/share/lua/5.1/?/init.lua;$LUA_PATH"' >> /environment

	echo 'export LUA_CPATH="/usr/local/torch/install/lib/lua/5.1/?.so"' >> /environment
	echo 'export LUA_CPATH="./?.so;$LUA_CPATH"' >> /environment
	echo 'export LUA_CPATH="/usr/local/lib/lua/5.1/?.so;$LUA_CPATH"' >> /environment
	echo 'export LUA_CPATH="/usr/local/lib/lua/5.1/loadall.so;$LUA_CPATH"' >> /environment

	echo 'export PATH=/usr/local/torch/install/bin:$PATH' >> /environment
	echo 'export LD_LIBRARY_PATH=/usr/local/torch/install/lib:$LD_LIBRARY_PATH' >> /environment
	echo 'export DYLD_LIBRARY_PATH=/usr/local/torch/install/lib:$DYLD_LIBRARY_PATH' >> /environment
	echo 'export LUA_CPATH="/usr/local/torch/install/lib/?.so;$LUA_CPATH"' >> /environment




%runscript
	#Executes with the apptainer run command
	#delete this section to use existing docker ENTRYPOINT command


%test
	#Test that script is a success
