#!/bin/bash -x

service aesmd stop
/sbin/modprobe -r graphene-sgx
/sbin/modprobe -r isgx
mkdir -p "/lib/modules/"`uname -r`"/kernel/drivers/intel/sgx"
cp linux-sgx-driver/isgx.ko graphene-sgx-driver/graphene-sgx.ko "/lib/modules/"`uname -r`"/kernel/drivers/intel/sgx"
cat /etc/modules | grep -Fxq isgx || echo isgx >> /etc/modules
cat /etc/modules | grep -Fxq graphene-sgx || echo graphene-sgx >> /etc/modules
/sbin/depmod
/sbin/modprobe isgx
/sbin/modprobe graphene-sgx
service aesmd start
if [ "`sysctl -n vm.mmap_min_addr`" != 0 ]; then
	cat /etc/sysctl.conf | grep -Fxq 'vm.mmap_min_addr = 0' || \
		echo -e "\n# For Graphene-SGX\nvm.mmap_min_addr = 0" >> /etc/sysctl.conf
	sysctl -p
fi