#!/bin/sh

DEFINES='-DUINDEX_TYPE="unsigned int" -DUINDEX_MAX=UINT_MAX \
	-DEINDEX_TYPE="unsigned short" -DEINDEX_MAX=USHRT_MAX'
DEBUG=-O2

apk update
apk upgrade

git -C /container/dgd pull
make -C /container/dgd/src DEFINES+="$DEFINES" DEBUG="$DEBUG" install
git -C /container/lpc-ext pull
make -C /container/lpc-ext/src DEFINES="$DEFINES" DEBUG="$DEBUG" ZLIB=1.2.12 jit zlib crypto
EXT=`grep ^EXT /container/lpc-ext/src/Makefile | sed s/EXT=//`

sed < /container/server.dgd > /container/config				\
	-e 's!^directory.*$!directory = "/container/src";!'		\
	-e 's!^swap_file.*$!swap_file = "/container/state/swap";!'	\
	-e 's!^dump_file.*$!dump_file = "/container/state/snapshot";!'	\
	-e 's!^ed_tmpfile.*$!ed_tmpfile = "/container/state/ed";!'	\
	-e 's!^hotboot.*$!hotboot = ({ "/container/dgd/bin/dgd", "/container/config", "/container/state/snapshot", "/container/state/snapshot.old" });!'\
	-e 's!^modules.*$!modules = ([ "/container/lpc-ext/jit.'$EXT'" : "/container", "/container/lpc-ext/zlib.'$EXT'" : "", "/container/lpc-ext/crypto.'$EXT'" : "" ]);!'
