####################################################################
#                                                                  #
#                       Salut à Toi/Libervia                       #
# This Dockerfile build Libervia, the web frontend for Salut à Toi #
#   Salut à Toi is a multi-frontends multi-purposes XMPP client    #
#                                                                  #
####################################################################

FROM salutatoi/media:latest

MAINTAINER Goffi <goffi@goffi.org>

##############
# txJSON-RPC #
##############

WORKDIR /tmp/txjsonrpc

# wget and tar are not installed, so we use python
RUN python -c 'import urllib2,tarfile,cStringIO;tar=tarfile.open(fileobj=cStringIO.StringIO(urllib2.urlopen("https://pypi.python.org/packages/source/t/txJSON-RPC/txJSON-RPC-0.3.1.tar.gz").read()));tar.extractall()'

# we don't need setuptools, so we just install through python
RUN cd tx*; sed -i "s/from setuptools/from distutils.core/" setup.py; python setup.py install

WORKDIR /tmp

RUN rm -rf txjsonrpc

###########
# PYJAMAS #
###########

WORKDIR /usr/share

# as the situation with pyjamas is complicated, we get the archive from our own ftp
RUN python -c 'import urllib2,tarfile,cStringIO;tar=tarfile.open(fileobj=cStringIO.StringIO(urllib2.urlopen("http://ftp.goffi.org/pyjamas/pyjamas.tar.bz2").read()));tar.extractall()'

WORKDIR pyjamas

RUN python bootstrap.py

RUN ln -s /usr/share/pyjamas/bin/pyjsbuild /usr/local/bin/pyjsbuild

############
# LIBERVIA #
############

WORKDIR /tmp

RUN hg clone http://repos.goffi.org/libervia

WORKDIR libervia

RUN python setup.py install

WORKDIR /tmp

RUN rm -rf libervia

#####################
# FIRST LAUNCH TEST #
#####################

# this script check if libervia and admin accounts exist, and create them if necessary
# then it launch libervia
RUN echo '#!/usr/bin/env python2\n\
import os, sys, subprocess, string, random\n\
from sat.plugins import plugin_misc_account as account\n\
from sat.tools import config\n\
from sat_frontends.bridge import DBus\n\
def generate_pwd():\n\
    chars = string.letters + string.digits\n\
    length = 12\n\
    return "".join(random.choice(chars) for _ in range(length))\n\
sat=DBus.DBusBridgeFrontend()\n\
sat.getReady()\n\
admin_email = sat.getConfig(account.CONFIG_SECTION, "admin_email") or account.default_conf["admin_email"]\n\
for profile in ["libervia", "admin"]:\n\
    if not sat.getProfileName(profile):\n\
        print "{} profile doesn'\''t exists, creating it".format(profile)\n\
        print "registering {}@{}".format(profile, sat.getNewAccountDomain())\n\
        pwd = generate_pwd()\n\
        if profile == "libervia":\n\
            config.fixConfigOption("libervia", "passphrase", pwd)\n\
        elif profile == "admin":\n\
            with open("/home/sat/ADMIN_PWD", "w") as f:\n\
                f.write("%s\n" % pwd)\n\
        sat.registerSatAccount(admin_email, pwd, profile)\n\
os.execvp("libervia", ["libervia"] + sys.argv[1:])\n\
' > /usr/bin/libervia_cont_launch && chmod 555 /usr/bin/libervia_cont_launch

#########
# D-Bus #
#########

RUN cp /usr/local/bin/dbus_wrap /usr/local/bin/libervia_cont_launch && chmod 555 /usr/local/bin/libervia_cont_launch

#########
# Ports #
#########

# HTTP
EXPOSE 8080

# HTTPS
EXPOSE 8443

##########
# LAUNCH #
##########

USER sat

ENTRYPOINT ["libervia_cont_launch", "fg"]
CMD ["-t", "http"]