FROM debian:latest

## Upgrade packages on system
RUN apt-get update

## Install build tools
RUN apt-get install -y \
	g++\
	gcc\
	make\
	patch

## Install package dependencies
RUN apt-get install -y \
	curl\
	libc-dev\
	libcurl4-openssl-dev\
	libedit-dev\
	libjansson-dev\
	libogg-dev\
	libresample1-dev\
	libspeex-dev\
	libsqlite3-dev\
	libsrtp2-dev\
	libssl-dev\
	libvorbis-dev\
	libxml2-dev\
	libxslt-dev\
	ncurses-dev\
	uuid-dev

## Install GONK specific dependencies
RUN apt-get install -y fail2ban iptables net-tools cron

## Copy over source and other scripts
COPY resources /build

## Move and unpack source to /build
RUN mkdir /build/asterisk-current
RUN tar -xzf /build/asterisk-current.tar.gz --strip-components 1 -C /build/asterisk-current
RUN rm /build/asterisk-current.tar.gz

## Configure asterisk source for build
WORKDIR /build/asterisk-current
RUN ./configure
RUN mv /build/menuselect.makeopts /build/asterisk-current/
RUN make -j$(cat /proc/cpuinfo | grep processor | wc -l)
RUN make install


## Cleanup
WORKDIR /
RUN cp /build/reload.sh /usr/bin/reload
RUN chmod +x /usr/bin/reload
RUN cp /build/gonk.sh /usr/bin/gonk
RUN chmod +x /usr/bin/gonk
RUN rm -rf /build
RUN apt-get remove -y --purge g++ gcc make patch
RUN apt-get autoremove -y && apt-get clean -y  && apt-get autoclean -y

ENTRYPOINT [ "gonk" ]