FROM phusion/baseimage:0.11
CMD ["/sbin/my_init"]

RUN DEBIAN_FRONTEND=noninteractive apt-get update -q -y && apt-get upgrade -q -y
RUN DEBIAN_FRONTEND=noninteractive apt-get -q -y install vim net-tools telnet iputils-ping
RUN DEBIAN_FRONTEND=noninteractive apt-get -q -y install openvpn
RUN DEBIAN_FRONTEND=noninteractive apt-get -q -y install curl build-essential git autoconf libtool apt-utils
RUN DEBIAN_FRONTEND=noninteractive apt-get -q -y install pkg-config libdbd-sqlite3 libdbi0-dev libtalloc-dev libpcsclite-dev libsctp-dev libssl-dev libgnutls28-dev libsqlite3-dev sqlite3

# oRTP
RUN cd /root && curl -O -L http://download.savannah.gnu.org/releases/linphone/ortp/sources/ortp-0.22.0.tar.gz \
    && tar xvzf ortp-0.22.0.tar.gz \
    && cd ortp-0.22.0 \
    && ./configure && make && make install \
    && ldconfig \
    && cd ..

# libosmocore
RUN cd /root && git clone git://git.osmocom.org/libosmocore.git \
    && cd libosmocore \
    && git checkout 7ab5fc1f3b373fadc644448442b985c12597f8f0 \
    && autoreconf -i \
    && ./configure && make && make install \
    && ldconfig \
    && cd ..

# libosmo-abis
RUN cd /root && git clone git://git.osmocom.org/libosmo-abis.git \
    && cd libosmo-abis \
    && git checkout de5758d307f6794b4126c6cf5251a5c5a8b9f193 \
    && autoreconf -i \
    && ./configure && make && make install \
    && ldconfig \
    && cd ..

# libosmo-netif
RUN cd /root && git clone git://git.osmocom.org/libosmo-netif.git \
    && cd libosmo-netif \
    && git checkout a555a1fca28f6ba1c5c877bb3d94f2b77396ed64 \
    && autoreconf -i \
    && ./configure && make && make install \
    && ldconfig \
    && cd ..

# libosmo-sccp
RUN cd /root && git clone git://git.osmocom.org/libosmo-sccp.git \
    && cd libosmo-sccp \
    && git checkout 5371a212937ad5eca33699bad3be8c101ab10cee \
    && autoreconf -i \
    && ./configure && make && make install \
    && ldconfig \
    && cd ..

# Osmo-MGW
RUN cd /root && git clone git://git.osmocom.org/osmo-mgw.git \
    && cd osmo-mgw/ \
    && git checkout 8dbc9ed4082363c05fe31c5d2b977dabe34e23e9 \
    && autoreconf -i \
    && ./configure && make && make install \
    && ldconfig \
    && cd ..

# Osmo-BSC
RUN cd /root && git clone git://git.osmocom.org/osmo-bsc.git \
    && cd osmo-bsc/ \
    && git checkout 6f379baa9bef487f10439e2f7fc973473b4248ab \
    && autoreconf -i \
    && ./configure && make && make install \
    && ldconfig \
    && cd ..

# Osmo-HLR
RUN cd /root && git clone git://git.osmocom.org/osmo-hlr.git \
    && cd osmo-hlr/ \
    && git checkout e66e525e0904c234a17416aeee0393779d6e1bf3 \
    && autoreconf -i \
    && ./configure && make && make install \
    && ldconfig \
    && cd ../..

# Osmo-MSC
RUN cd /root && git clone git://git.osmocom.org/osmo-msc.git \
    && cd osmo-msc/ \
    && git checkout 1263bc801749af648cf4fae790ddbdd348d87e69 \
    && autoreconf -i \
    && ./configure && make && make install \
    && ldconfig \
    && cd ../..

# Osmo-BTS
RUN cd /root && git clone git://git.osmocom.org/osmo-bts.git \
    && cd osmo-bts/ \
    && git checkout ecfb83d3cc1199bf07e5a97e758b060f1d1beb74 \
    && autoreconf -i \
    && ./configure && make && make install \
    && ldconfig \
    && cd ../..

# Copy configs
ADD configs/osmocom /etc/osmocom/

ADD configs/openvpn /etc/openvpn/
ADD ca/keys/ca.crt /etc/openvpn/
ADD ca/keys/dh.pem /etc/openvpn/
ADD ca/keys/server.pem /etc/openvpn/
ADD ca/keys/server.key /etc/openvpn/
RUN chmod +x /etc/openvpn/up.sh

RUN mkdir -p /var/lib/osmocom
COPY misc/hlr.db /var/lib/osmocom/hlr.db

# Copy startup scripts
RUN mkdir /etc/service/osmo-bsc
COPY services/osmo-bsc /etc/service/osmo-bsc/run
RUN chmod +x /etc/service/osmo-bsc/run

RUN mkdir /etc/service/osmo-bts
COPY services/osmo-bts /etc/service/osmo-bts/run
RUN chmod +x /etc/service/osmo-bts/run

RUN mkdir /etc/service/osmo-hlr
COPY services/osmo-hlr /etc/service/osmo-hlr/run
RUN chmod +x /etc/service/osmo-hlr/run

RUN mkdir /etc/service/osmo-mgw
COPY services/osmo-mgw /etc/service/osmo-mgw/run
RUN chmod +x /etc/service/osmo-mgw/run

RUN mkdir /etc/service/osmo-msc
COPY services/osmo-msc /etc/service/osmo-msc/run
RUN chmod +x /etc/service/osmo-msc/run

RUN mkdir /etc/service/osmo-stp
COPY services/osmo-stp /etc/service/osmo-stp/run
RUN chmod +x /etc/service/osmo-stp/run

RUN mkdir /etc/service/openvpn
COPY services/openvpn /etc/service/openvpn/run
RUN chmod +x /etc/service/openvpn/run

# Manual startup
#osmo-hlr -c /etc/osmocom/osmo-hlr.cfg
#osmo-mgw -c /etc/osmocom/osmo-mgw.cfg
#osmo-stp -c /etc/osmocom/osmo-stp.cfg
#osmo-msc -c /etc/osmocom/osmo-msc.cfg
#osmo-bsc -c /etc/osmocom/osmo-bsc.cfg
#osmo-bts-virtual -c /etc/osmocom/osmo-bts.cfg
