#!/bin/bash

set -eux 

apt-get install -y --force-yes wget git kal-scripts python

if test -z "${RELEASE:-}"; then
    if type -p lsb_release; then
        RELEASE=$(lsb_release -c -s)
    else
        RELEASE=$(cat apt/sources.list | grep ^deb | head -n 1 | awk '{print $3;}')
    fi
    export RELEASE
fi

# ## Get latest OpenVPN version (they don't have a lot of recent packets)
# wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg | apt-key add -
# echo "deb http://swupdate.openvpn.net/apt $RELEASE main" > /etc/apt/sources.list.d/swupdate.openvpn.net.list
# ## Update only this repo:
# apt-get update -o Dir::Etc::sourcelist="sources.list.d/swupdate.openvpn.net.list" \
#     -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0"
# apt-get -y --force-yes install openvpn

export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
dpkg -i deb/openvpn_*.deb || true
apt-get -o Dpkg::Options::="--force-confnew" install -f -y --force-yes


mkdir -p /var/run/openvpn /var/log/openvpn

##
## if using ``tun`` we will need this.
##

[ -d /dev/net ] ||
    mkdir -p /dev/net
[ -c /dev/net/tun ] ||
    mknod -m a+rw /dev/net/tun c 10 200


##
## installing obfsproxy latest version
##

mkdir -p /opt/apps

(
    apt-get install -y --force-yes python-setuptools python-twisted python-crypto python-yaml python-pyptlib
    cd /opt/apps &&
    git clone https://git.torproject.org/pluggable-transports/obfsproxy.git &&
    cd obfsproxy &&
    python setup.py install
)

## obfs4proxy does not work with OpenVPN for now.
# (
#     apt-get install --force-yes -y golang &&
#     cd /opt/apps &&
#     mkdir obfs4 &&
#     cd obfs4 &&
#     GOPATH=$PWD go get git.torproject.org/pluggable-transports/obfs4.git/obfs4proxy
#     ln -sf /opt/apps/obfs4/
# )

##
## Make sure the init script in good
##

(
    cp src/etc/init.d/openvpn /etc/init.d/openvpn
)